How to force youtube-dl to save Opus-encoded audio as *.opus files

I just want to download both audio and video streams of this video. (WARNING: Maternal Insults ahead) I always barred by errors like this:

[youtube] hcQlNng606I: Downloading webpage
[youtube] hcQlNng606I: Downloading video info webpage
[youtube] hcQlNng606I: Extracting video information
[youtube] hcQlNng606I: Downloading MPD manifest
[download] YO MAMA! Star Wars Jokes-hcQlNng606I.webm has already been downloaded
[download] 100% of 4.78MiB

Why this happens?

Because after youtube-dl downloaded either the Opus audio or the VP9 video, it will be both saved as *.webm.

My first workaround for this is to download them on other directory and it is not (for me) very efficient.

So how do I force it to write Opus audio as *.opus instead of *.webm

Additional notes: Why do I like to download VP9/Opus formats on YouTube and combine it later on ffmpeg? VP9/Opus are better than H264/AAC.

0

2 Answers

If you are looking to keep both the audio file and the video file use -k as one of your arguments. Ex. : youtube-dl -k if you want to save your audio file as .opus use youtube-dl --audio-format opus

Some extra notes:
If you want to have the audio be the best you can get it use youtube-dl --audio-quality 0 if you want to have the best quality of audio and youtube-dl -x to extract the audio directly. If you want more info on all of this stuff just use youtube-dl -h it will give you the help message and the more you read the less confusing they get.

--audio-format opus always converts to opus, regardless the format of the downloaded file. So, that's typically not what you want. I wrote a python script to solve this problem. It was written assuming you only want the audio. It runs youtube-dl -if bestaudio <url>, then if any of the downloaded files are .webm, it assumes it's opus and uses ffmpeg to do a lossless container conversion to .opus. (The -i is for downloading whole playlists. There are often a few in the playlist that fail to download for whatever reason.)

#!/usr/bin/env python
# Youtube actually hosts audio-only opus tracks, but you can only get them
# in the webm container, which many music players, including quodlibet, don't
# know what to do with. This script downloads the track, then converts it with
# zero loss to the opus container using ffmpeg's `-acodec copy` feature.
from sys import *
from subprocess import call
from os.path import splitext
from os import remove, walk, listdir
from tempfile import TemporaryDirectory
from shutil import move
url = argv[1]
with TemporaryDirectory(prefix='yta-') as tempdir: call(['env', '-C', tempdir, 'youtube-dl', '-if', 'bestaudio', url]) for tempdir, dirs, files in walk(tempdir): for fn in files: path = tempdir+'/'+fn name, ext = splitext(path) if ext == '.webm': if call([ 'ffmpeg', '-hide_banner', '-i', path, '-acodec', 'copy', name+'.opus' ]) == 0: remove(path) for node in listdir(tempdir): move(tempdir+'/'+node, '.')

EDIT: I can see now, I misunderstood your question. The best way to avoid filename collisions between runs of youtube-dl is to change the output filename template. The default if --output is not specified is %(title)s-%(id)s.%(ext)s. What you should do is do --output %(title)s-%(id)s-audio.%(ext)s for the audio acquisition and --output %(title)s-%(id)s-video.%(ext)s for the video acquisition. See OUTPUT TEMPLATE in man youtube-dl for more. You can even nuke the %(title) etc. entirely and just say --output audio.webm etc. to make it easier for the ffmpeg stage of your script to find the source files.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like