Nono.MA

Download High-Quality YouTube Videos as MP4 with youtube-dl and yt-dlp

LAST UPDATED OCTOBER 11, 2022

Rough notes as I learn how to use youtube-dl and FFmpeg to download and encode high-resolution videos from YouTube.

Disclaimer

Note that my use of youtube-dl is experimental, and I mostly rely on it to download my own recordings when I stream and don't have the possibility of recording locally.

youtube-dl, yt-dlp, and similar tools are against YouTube's terms and conditions, and explicitly publishing content showing people how to download using these methods could get you a channel violation—what YouTube calls community strikes—which could eventually lead to the cancellation of your YouTube channel.


Downloading the highest resolution available from a Public or Unlisted YouTube video

youtube-dl \
  -f 271+140 \
  "https://youtu.be/n9ZW-Vq6pbk"
  • 271 is the code for the webm video format at 2560x1440
  • 140 is the code for the m4a audio format at 128k (44100Hz)

Downloading and Recoding to MP4

youtube-dl \
  -f 271+140 \
  --recode-video mp4 \
  "https://youtu.be/n9ZW-Vq6pbk"

See this post for reference

Convert MKV video to MP4 (at 2560x1440, for instance)

Note that if your video is not 2560x1440 it will be upsized (if smaller) or downscaled (if bigger). In this case, I'm re-encoding a video that is 2560x1440 respecting its size using h.264 video codec.

ffmpeg -i "my-video.mkv" -vf scale=2560:1440 -c:v libx264 "my-video.mkv.2560x1440.mp4"

Listing available YouTube video formats with youtube-dl

youtube-dl -F "https://youtu.be/XdOrU1rwBP4"

Will return the following:

[youtube] XdOrU1rwBP4: Downloading webpage
[info] Available formats for XdOrU1rwBP4:
format code  extension  resolution note
249          webm       audio only tiny   50k , opus @ 50k (48000Hz), 900.11KiB
250          webm       audio only tiny   72k , opus @ 70k (48000Hz), 1.27MiB
251          webm       audio only tiny  124k , opus @160k (48000Hz), 2.19MiB
140          m4a        audio only tiny  130k , m4a_dash container, mp4a.40.2@128k (44100Hz), 2.36MiB
278          webm       256x144    144p   61k , webm container, vp9, 30fps, video only, 559.52KiB
160          mp4        256x144    144p   71k , avc1.4d400c, 30fps, video only, 469.47KiB
242          webm       426x240    240p   90k , vp9, 30fps, video only, 642.75KiB
243          webm       640x360    360p  142k , vp9, 30fps, video only, 1.00MiB
133          mp4        426x240    240p  171k , avc1.4d4015, 30fps, video only, 942.27KiB
244          webm       854x480    480p  218k , vp9, 30fps, video only, 1.54MiB
134          mp4        640x360    360p  266k , avc1.4d401e, 30fps, video only, 1.65MiB
247          webm       1280x720   720p  360k , vp9, 30fps, video only, 2.99MiB
135          mp4        854x480    480p  386k , avc1.4d401f, 30fps, video only, 2.27MiB
248          webm       1920x1080  1080p  445k , vp9, 30fps, video only, 3.28MiB
136          mp4        1280x720   720p  511k , avc1.64001f, 30fps, video only, 3.86MiB
137          mp4        1920x1080  1080p  687k , avc1.640028, 30fps, video only, 6.62MiB
271          webm       2560x1440  1440p 1276k , vp9, 30fps, video only, 8.59MiB
18           mp4        640x360    360p  184k , avc1.42001E, 30fps, mp4a.40.2@ 96k (44100Hz), 3.37MiB
22           mp4        1280x720   720p  339k , avc1.64001F, 30fps, mp4a.40.2@192k (44100Hz) (best)

2021.01.08

From my test, once we download the highest resolution mkv video with youtube-dl we obtain the exact same result by using the --recode-video mp4 option (to auto-convert after downloading that mkv) or recoding to mp4 afterward with FFmpeg (which turns out to be what youtube-dl does behind the scenes).