i need some help using yt-dlp, what's the best command line argument to only download…
- 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"
- 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]"
•bais 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 iswebm.
‣ffmpeg -c copyjust 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.mkaOne 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! 🎧
- User
i don't think you need to remux .webm file when changing the extension to .mka
- 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.webmextension.
‑mv -nsimply renames<title>.webm→<title>.mka.
‑ No FFmpeg, no extra I/O.Everything else (the format selector chain and normal output template) stays the same:
- Best AAC in an m4a container if available.
- Otherwise best mp3.
- 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. 🎧
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.