Skip to content
The Chat Archive
o3-2025-04-16· OpenAI LMArena Human Preference 140K

i need some help using yt-dlp, what's the best command line argument to only download…

  1. User

    i need some help using yt-dlp, what's the best command line argument to only download the best audio, preferring aac file but falling back to mp3 then webm. Downloaded aac files need to have ".m4a" extension, mp3 using ".mp3" extension, and webm files using the extension ".mka"

  2. Assistant

    Below is the simplest “one-liner” that fulfils every requirement you listed.

    yt-dlp                 \
       -f "ba[ext=m4a]/ba[ext=mp3]/ba[ext=webm]" \
       -o "%(title)s.%(ext)s"                    \
       --exec-if-ext webm "ffmpeg -loglevel error -i {} -c copy {.}.mka && rm {}" \
       <URL-of-the-video>
    

    What each part does

    • -f "ba[ext=m4a]/ba[ext=mp3]/ba[ext=webm]"
    • ba is short for bestaudio.
    • The [ext=…] filters force the container/codec we want.
    • The / characters create a fallback chain:
    1. first try AAC in an m4a container,
    2. if that does not exist fall back to mp3,
    3. finally fall back to WebM (opus/vorbis) audio.

    • -o "%(title)s.%(ext)s"
    Keeps the file’s extension as delivered by yt-dlp (m4a / mp3 / webm).
    We will change only the WebM files in the next step.

    • --exec-if-ext webm "ffmpeg -loglevel error -i {} -c copy {.}.mka && rm {}"
    Runs only when the downloaded file’s extension is webm.
    ‣ ffmpeg -c copy just re-muxes (no re-encoding) into a Matroska-audio
    container, producing .mka.
    ‣ The original .webm is removed after the .mka has been written.

    End result

    • AAC → Title.m4a
    • MP3 → Title.mp3
    • WebM/Opus → Title.mka

    One yt-dlp invocation, no transcoding, correct extension every time. ✅

    Older yt-dlp or different shells

    If your build predates --exec-if-ext, you can still do the same thing with a shell test:

    yt-dlp -f "ba[ext=m4a]/ba[ext=mp3]/ba[ext=webm]" \
           -o "%(title)s.%(ext)s"                    \
           --exec "sh -c '[[ \"{ext}\" == webm ]] && ffmpeg -i {} -c copy {.}.mka && rm {}'" \
           <URL>
    

    Windows PowerShell variant:

    yt-dlp -f "ba[ext=m4a]/ba[ext=mp3]/ba[ext=webm]" `
           -o "%(title)s.%(ext)s" `
           --exec-if-ext webm "ffmpeg -loglevel error -i \"{}\" -c copy \"{.}.mka\"; Remove-Item \"{}\"" `
           <URL>
    

    That’s all there is to it—happy downloading! 🎧

  3. User

    i don't think you need to remux .webm file when changing the extension to .mka

  4. Assistant

    You’re right—if all you want is the *.webm → .mka name-swap, you can simply rename the file; WebM is a restricted subset of Matroska, so most players / taggers that understand .mka will happily open the un-touched WebM bit-stream.
    Here’s the tweaked recipe that only renames, no remuxing:

    yt-dlp \
      -f "ba[ext=m4a]/ba[ext=mp3]/ba[ext=webm]" \
      -o "%(title)s.%(ext)s" \
      --exec-if-ext webm "bash -c 'mv -n \"{}\" \"{.}.mka\"'" \
      <URL>
    

    What changed?

    • --exec-if-ext webm ... mv … {.}.mka
    ‑ Runs only when the file has a .webm extension.
    ‑ mv -n simply renames <title>.webm → <title>.mka.
    ‑ No FFmpeg, no extra I/O.

    Everything else (the format selector chain and normal output template) stays the same:

    1. Best AAC in an m4a container if available.
    2. Otherwise best mp3.
    3. Otherwise best WebM audio, which ends up as mka thanks to the rename step.

    Resulting file names
    • AAC → Title.m4a
    • MP3 → Title.mp3
    • WebM → Title.mka (identical data, just a new extension)

    Extra trivia (optional)
    If you ever bump into software that complains about the “pure rename” trick, drop back to the remux command from the previous answer—but for most everyday use-cases the rename is perfectly fine. 🎧

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.