Sunday, April 16, 2017

youtube-dl for Muxing Streams

Let's say we need to download videos from youtube with the highest audio and video quality. Sometimes, the combination is not available with most downloaders. But youtube-dl can download separate audio and video streams and mux them together. Here are some commands to do that.
# List all available streams for a video
➜ youtube-dl -F ""
[youtube] abcd1234: Downloading webpage
[youtube] abcd1234: Downloading video info webpage
[youtube] abcd1234: Extracting video information
[youtube] abcd1234: Downloading js player en_US-vfl5-0t5t
[youtube] abcd1234: Downloading js player en_US-vfl5-0t5t
[info] Available formats for abcd1234:
format code extension resolution note
249 webm audio only DASH audio 56k , opus @ 50k, 1.52MiB
250 webm audio only DASH audio 84k , opus @ 70k, 2.10MiB
171 webm audio only DASH audio 126k , vorbis@128k, 3.36MiB
140 m4a audio only DASH audio 127k , m4a_dash container, mp4a.40.2@128k, 3.69MiB
251 webm audio only DASH audio 161k , opus @160k, 4.03MiB
278 webm 256x144 144p 101k , webm container, vp9, 13fps, video only, 2.41MiB
160 mp4 256x144 144p 112k , avc1.4d400c, 25fps, video only, 3.19MiB
242 webm 426x240 240p 140k , vp9, 25fps, video only, 2.34MiB
243 webm 640x360 360p 234k , vp9, 25fps, video only, 3.98MiB
133 mp4 426x240 240p 248k , avc1.4d4015, 25fps, video only, 7.13MiB
134 mp4 640x360 360p 254k , avc1.4d401e, 25fps, video only, 6.49MiB
244 webm 854x480 480p 355k , vp9, 25fps, video only, 6.22MiB
135 mp4 854x480 480p 559k , avc1.4d401e, 25fps, video only, 13.74MiB
247 webm 1280x720 720p 601k , vp9, 25fps, video only, 11.70MiB
136 mp4 1280x720 720p 1171k , avc1.4d401f, 25fps, video only, 28.41MiB
248 webm 1920x1080 1080p 1220k , vp9, 25fps, video only, 23.32MiB
137 mp4 1920x1080 1080p 2258k , avc1.640028, 25fps, video only, 57.07MiB
17 3gp 176x144 small , mp4v.20.3, mp4a.40.2@ 24k
36 3gp 320x180 small , mp4v.20.3, mp4a.40.2
43 webm 640x360 medium , vp8.0, vorbis@128k
18 mp4 640x360 medium , avc1.42001E, mp4a.40.2@ 96k
22 mp4 1280x720 hd720 , avc1.64001F, mp4a.40.2@192k (best)

We can see that the highest quality video is 137 with 1080p 2258k and audio is 251 with 161k. But these are video only and audio only streams. Let's combine them.
➜ youtube-dl -f 137+251 ""
It will pick a compatible format when merging. If mp4 doesn't work it gets converted into mkv mostly.

Automatically choose best video and audio: -f bestvideo+bestaudio
Download subtitle if the video has one already uploaded: --write-srt --sub-lang en