4

I've poured through the youtube-dl documentation. I've found what I thought was exactly what I wanted...but I've run into a snag, and I'm curious to see if any of the more advanced options of youtube-dl could help solve my problem. For some of the more advanced options, there isn't much documentation, and I'm still learning Python...so going through the source code has been an endeavor. haha.

The Goal:

  • Download the best video + best audio (default to "best" if combo not available)
  • Embed the thumbnail
  • Include metadata info in file
  • Output final file as MP4 - cannot be MKV

My current command:

youtube-dl -f "bestvideo+bestaudio[ext=m4a]/best" --embed-thumbnail --add-metadata [url]

The problem:

In cases where the "bestvideo" is a webm file...it detects that WEBM + M4A are incompatible and will be merged into an MKV file rather than MP4 (like it normally would). Since this step is performed prior to the thumbnail embedding via AtomicParsley...An error is thrown, because MKV are not supported for thumbnail embedding.

UPDATE: I later remembered the above statement is not accurate. What was actually happening was that WEBM video was working fine, but it was WEBM audio that was causing the problem. YouTube uses Opus encoding for WEBM audio, which when youtube-dl tried merging the two files, it would end up having to use an MKV container rather than MP4 because ffmpeg doesn't safely allow WEBM/Opus within an MP4 container. In order to force this conversion you must add "--strict 2" to ffmpeg (can be passed in via a youtube-dl parameter). My solution was to exclude WEBM audio formats by using "bestaudio[ext=m4a]". Once I did this, all I simply needed to do was add "--merge-output-format mp4" to my youtube-dl command (as recommended by the marked answer).

However...if the video only webm file were first converted to MP4, then none of this would be a problem.

So...does anyone know, if there is a way to tell youtube-dl that when it downloads the video file, to immediately convert it to a given type (MP4) before doing anything else?

AKA this:

  1. [youtube-dl] download thumbnail
  2. [youtube-dl] download video file (webm/mp4)
  3. [ffmpeg] if not mp4, convert video file to mp4 -- extra step that I want to happen
  4. [youtube-dl] download audio file (m4a)
  5. [ffmpeg] merge files to mp4
  6. [ffmpeg] add metadata
  7. [atomicparsley] embed thumbnail

If not...then I think I might just go the route of writing this all myself in powershell. Like I could have youtube-dl download the audio, video, metadata and thumbnail to file, and then write a powershell script that does all I need to do. Maybe have youtube-dl call the powershell script after each iteration.

4

Add the option --merge-output-format mp4 to your command:

youtube-dl --merge-output-format mp4 -f "bestvideo+bestaudio[ext=m4a]/best" --embed-thumbnail --add-metadata [url]

From the manual:

--merge-output-format FORMAT If a merge is required (e.g. bestvideo+bestaudio), output to given container format. One of mkv, mp4, ogg, webm, flv. Ignored if no merge is required


To make this work with webm audio formats:

youtube-dl --merge-output-format mp4 --postprocessor-args "-strict experimental" -f "bestvideo+bestaudio/best" --embed-thumbnail --add-metadata [url]
  • Note that as opus in an mp4 container format is "experimental", some players may have issues playing back such files
6
  • Hmm...I've been using that and it wasn't working. BUT, I just realized it wasn't working because that was before I limited it to only m4a audio. (webm audio on YouTube uses Opus encoding, and apparently ffmpeg doesn't like that and then it just sits there frozen). The only caveat I see to using this is in cases where it defaults over to "best" format...which could potentially end up being an mkv....however, it may be easier to deal with a handful of MKV records...VS writing a bunch of powershell do handle this. I'll give it a shot, thanks! – Chad Baldwin Feb 22 '20 at 4:44
  • webm in an mp4 container is experimental, so ffmpeg requires the option -strict -2. & I've never come across a site which gives an mkv container format as the "best" format with youtube-dl – user1141630 Feb 22 '20 at 4:50
  • Yeah, I tried using that ffmpeg option, but found the video had problems jumping around (no clue if related)...every so often it would skip a few seconds ahead....not sure what was happening, so I figured if i'm using an "experimental" feature, then removing that might help solve the problem. I re-downloaded the video using non-webm files, and then combined those, and the problem went away. So that's what led me down this whole path. – Chad Baldwin Feb 22 '20 at 4:55
  • What video player software do you use? It all works fine for me (using mpv with vaapi hwaccel) – user1141630 Feb 22 '20 at 4:58
  • Was using plex. It worked fine when I tested the video in VLC...but when viewing the video on Plex, that's where the problem showed up. So it could be a bug with plex and nothing to do with youtube-dl or ffmpeg at all. – Chad Baldwin Feb 22 '20 at 4:59
0

You can list the formats and see which is best for you

youtube-dl -F [youtube_video_address]

or you can try for the best quality .mp4 video available using the format attribute -f string

youtube-dl -f 'best[ext=mp4]' [youtube_video_address]

or you can decide you want a particular quality. The problem with that is that it might not be available. Here is a simple table of formats I've compiled (I may have missed some):

          ?  144p 240p 270p 360p 480p 520p 720p 1920p 1080p 3072p
       ┏━━==━====━====━====━====━====━====━====━=====━=====━=====━┓
   flv ╏   -   -    5    6   34   35    -  120     -     -     -  ┃
   3gp ╏  13  17   36    -    -    -    -    -     -     -     -  ┃
   mp4 ╏   -   -   83   18   18        85   22     -    37    38  ┃
       ┃     160*            82*            84*  299*  137*       ┃
       ┃          133*      134* 135*      136*                   ┃
  webm ╏   -   -    -    -   43   44    -   45     -    46     -  ┃
       ┃     278* 242*      243* 244*      247*        248*       ┃
       ┃                  100/101*         102*      * = no sound ┃
       ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
  m4a  sound only: 139 (low), 140 (med), 141 (hi)
  webm sound only: 249(opus50k), 250(opus70k), 171(vorbis128k), 251(opus160k)

Format 22 is usually the highest quality .mp4 format available.

youtube-dl -f 22 [youtube_video_address]

I've never encountered a video that isn't available in format 18 though that is often not suitable for videos that include text on a screen, such as programming tutorials.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.