Youtube-dl – Download best audio/video, embed thumbnail, and convert to mp4

ffmpegpowershellpowershell-coreyoutube-dl

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.

Best Answer

  • 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
  • Related Question