On Thursday, 18 April 2019 03:43:05 BST Walter Dnes wrote:
>   This thread has piqued my interest in youtube-dl.  I'm trying to get
> best audio and video together in a 1920x1080 video for Youtube URL
> https://www.youtube.com/watch?v=A9tKncAdlHQ
> 
> My config file is...
> 
> -f bestvideo[width<=1920]+bestaudio
> --merge-output-format mp4

Best audio available may not be compatible with an mp4 container without some 
manual conversion - see below.


> --no-mtime
> --no-playlist
> -o ~/ytdl/%(title)s.%(ext)s
> --restrict-filenames
> 
>   If I use "-f bestvideo[width<=1920]" I get a 1920x1080 video...with no
> sound.

Yes, because this is what you asked - video stream only.


> If I use "-f best[width<=1920]" I get a 1280x720 video with
> sound.  

Yes, this will download a *single* container file which contains both video 
and audio pre-muxed streams, with the best quality video size of less or equal 
to 1920px in width.  Youtube has a 720p single file available containing both 
audio and video, so this is selected.


> That works, but I want the best of both worlds.  With
> "-f bestvideo[width<=1920]+bestaudio", ffmpeg fails...
> 
> [i660][waltdnes][~/ytdl] youtube-dl
> https://www.youtube.com/watch?v=A9tKncAdlHQ [youtube] A9tKncAdlHQ:
> Downloading webpage
> [youtube] Downloading just video A9tKncAdlHQ because of --no-playlist
> [download] Destination:
> /home/waltdnes/ytdl/Double_Slit_Experiment_explained_by_Jim_Al-Khalili.f137
> .mp4 [download] 100% of 31.55MiB in 00:17
> [download] Destination:
> /home/waltdnes/ytdl/Double_Slit_Experiment_explained_by_Jim_Al-Khalili.f251
> .webm [download] 100% of 6.65MiB in 00:03
> [ffmpeg] Merging formats into
> "/home/waltdnes/ytdl/Double_Slit_Experiment_explained_by_Jim_Al-Khalili.mp4
> " ERROR:   Stream #1:0 -> #0:1 (copy)
> 
>   Any idea what's wrong?

Looking at the above output and also taking a look at the formats available 
for download:

$ youtube-dl -F 'https://www.youtube.com/watch?v=A9tKncAdlHQ'
[youtube] A9tKncAdlHQ: Downloading webpage
[youtube] A9tKncAdlHQ: Downloading video info webpage
[info] Available formats for A9tKncAdlHQ:
format code  extension  resolution note
249  webm  audio only DASH audio   54k , opus @ 50k, 3.24MiB
250  webm  audio only DASH audio   71k , opus @ 70k, 3.62MiB
140  m4a   audio only DASH audio  128k , m4a_dash container, mp4a.40.2@128k, 
8.29MiB
171  webm  audio only DASH audio  133k , vorbis@128k, 6.09MiB
251  webm  audio only DASH audio  137k , opus @160k, 6.65MiB
278  webm  256x144    144p   96k , webm container, vp9, 25fps, video only, 
4.42MiB
160  mp4   256x144    144p  112k , avc1.4d400c, 25fps, video only, 2.09MiB
242  webm  426x240    240p  224k , vp9, 25fps, video only, 5.26MiB
133  mp4   426x240    240p  268k , avc1.4d4015, 25fps, video only, 4.72MiB
243  webm  640x360    360p  404k , vp9, 25fps, video only, 11.51MiB
134  mp4   640x360    360p  616k , avc1.4d401e, 25fps, video only, 9.64MiB
244  webm  854x480    480p  678k , vp9, 25fps, video only, 20.51MiB
135  mp4   854x480    480p 1186k , avc1.4d401e, 25fps, video only, 15.17MiB
247  webm  1280x720   720p 1328k , vp9, 25fps, video only, 26.90MiB
136  mp4   1280x720   720p 2037k , avc1.4d401f, 25fps, video only, 22.90MiB
248  webm  1920x1080  1080p 2349k , vp9, 25fps, video only, 34.44MiB
137  mp4   1920x1080  1080p 2710k , avc1.640028, 25fps, video only, 31.55MiB
18   mp4   640x360    medium , avc1.42001E, mp4a.40.2@ 96k, 23.30MiB
43   webm  640x360    medium , vp8.0, vorbis@128k, 28.60MiB
22   mp4   1280x720   hd720 , avc1.64001F, mp4a.40.2@192k (best)

we can see you downloaded formats .f137 and .f251.  The 137 file is a 1080p 
video only file within an mp4 container.  The 251 file is an opus audio file 
within a webm container.

If you do not specify an output format for youtube-dl to transcode the video 
and audio into, the application will use a matroska container:

[ffmpeg] Merging formats into "Double Slit Experiment explained! by Jim Al-
Khalili-A9tKncAdlHQ.mkv"

which will retain the best video with the size you requested as h264 *and* the 
best audio it could find as opus.  This will adhere to your request for a best 
video and best audio.

I do not think it would be logical for youtube-dl to transcode the audio from 
opus to aac, which is a suitable format for an mp4 container.  For this you 
would need to specify manually the bitrate to be used by m4a and potentially 
other parameters to suit your preferences and then wait for ffmpeg to 
transcode the audio, which on a large download would take a while.  It would 
be more logical to specify a suitable audio format for the container you 
intend to use upfront and ytdl would fetch this without any expensive post-
processing.  I suppose the devs could introduce a simple test to flag 
alternative audio formats available for download, which would be compatible 
with an mp4 container, instead of blindly downloading the opus stream and then 
ffmpeg failing to copy it into mp4.  I assume the ytdl extractor logic is not 
linked into the post-processing ffmpeg logic (two separate stages) and this is 
why it errors out in the fashion you experienced.

On a practical level, if the mkv container is not suitable for you, you could 
instead restrict the audio format to m4a and then ytdl will download the .f140 
file rather than .f251 and mux video and audio into an mp4 container - but 
arguably this won't represent the 'best' audio.

-- 
Regards,
Mick

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to