youtube-dl
youtube-dl copied to clipboard
How to normalize audio when downloading video (not extract mp3)
Please follow the guide below
- You will be asked some questions and requested to provide some information, please read them carefully and answer honestly
- Put an
x
into all the boxes [ ] relevant to your issue (like that [x]) - Use Preview tab to see how your issue will actually look like
Make sure you are using the latest version: run youtube-dl --version
and ensure your version is 2016.10.25. If it's not read this FAQ entry and update. Issues with outdated version will be rejected.
- [x] I've verified and I assure that I'm running youtube-dl 2016.10.25
Before submitting an issue make sure you have:
- [s] At least skimmed through README and most notably FAQ and BUGS sections
- [x] Searched the bugtracker for similar issues including closed ones
What is the purpose of your issue?
- [ ] Bug report (encountered problems with youtube-dl)
- [ ] Site support request (request for adding support for a new site)
- [x] Feature request (request for a new functionality)
- [x] Question
- [ ] Other
The following sections concretize particular purposed issues, you can erase any section (the contents between triple ---) not applicable to your issue
Description of your issue, suggested solution and other information
I would like to use youtube-dl to download video's, normalizing the audio in the process. I understand how to extract an MP3 from the video stream and normalize that, but I would like to have the Video and normalized audio in the container. It seams plausible this could be accomplished during the download, I just don't know how. If not, it seems like it would be easy to incorporate. The reason this is needed is for downloading recordings of conference sessions, such as Blackhat 2016 briefings, and Defcon sessions. They tend to have microphone volumes all over the place, and when the mic is handed off to someone else you get the hell scared out of your because it's so loud. Help would be greatly appreciated. I'm posting this on behalf of myself, and 3 others in need of the same process. Thank you for your time.
Are there programs with audio normalization algorithms? From Wikipedia, seems it requires some sort of signal processing. That's far beyond the scope of youtube-dl. If such a program requires only a filename, --exec
can be used for offline processing.
Yes, ffmpeg does it. The youtube-dl documentation explains how to do it, but only when you stip the mp3 from a video file. I don't want to strip the audio, I just want to normalize it and keep it in the video container or reinsert it in the video container. "normalization" is nothing more than audio compression.
Yes, ffmpeg does it.
Could you share your command?
I'm having trouble finding the information I read earlier. I will keep looking. I do know of another way to do it if the audio is piped to mp3gain and the following is ran:
mp3gain -r -T *.mp3
I'm wondering if youtube-dl could somehow pipe the video to the ffmpeg-normalize script (which can handle mkv, mp4, and avi files). I wish I could find the information I found earlier. This should be enough to get us moving in that direction.
OK, I'm finding some of the information I found earlier. Although ffmpeg-normalize probably does a better job, here is what's built into ffmpeg as is:
Built-in Normalization Filters
Current ffmpeg has two filters that can be directly used for normalization – although they are already quite advanced, so they do not simply apply gain. Here they are:
- loudnorm: loudness normalization according to EBU R128. You can set an integrated loudness target, a loudness range target, or maximum true peak. This is recommended for publishing audio and video.
- dynaudnorm: “intelligent” loudness normalization without clipping, which applies normalization dynamically over windowed portions of the file. This may change the characteristics of the sound, so it should be applied with caution.
If we want a “simple” RMS-based normalization to 0 dBFS, that is what ffmpeg-normalize does.
if youtube-dl could somehow pipe the video to the ffmpeg-normalize script
That's possible in general. Like this: youtube-dl <the URL> -o - | <ffmpeg or other programs>
I will have to test this. I will post anything and everything I find here. Do you have any more specifics to offer on the ffmpeg side of the pipe? Thank you.
Pipe protocol may be useful.
in ffmpeg it's -filter:a loudnorm
.
So it would be cool to have something like youtube-dl --prefer-ffmpeg --normalize
It seems like youtube returns perceptualLoudnessDb, which can be passed to a ffmpeg audio filter like alimiter to achieve the same effect
I just wanted to contribute some information; documentation I came across while researching how to do this manually. There is a script you can use to automate this called ffmpeg-normalize
. This may be a good utility to look at, for inspiration on how you want to implement such a task into youtube-dl.
Automatization with ffmpeg-normalize To automate the normalization processes with ffmpeg without having to manually perform two passes, and run normalization on multiple files (including video), you can also use the ffmpeg-normalize Python program via pip install ffmpeg-normalize.
The script defaults to EBU R128 normalization with two passes, but peak and RMS normalization are also supported.
For details, run ffmpeg-normalize -h or see the README file.
I noticed there's an option to supply extra parameters to the postprocessor so you can do the normalization easily with --prefer-ffmpeg --postprocessor-args "-filter:a loudnorm"
I noticed there's an option to supply extra parameters to the postprocessor so you can do the normalization easily with
--prefer-ffmpeg --postprocessor-args "-filter:a loudnorm"
That was a good tip but unfortunately :
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -i "file:myfile.m4a" -c copy -f mp4 "-filter:a" loudnorm "file:myfile.temp.m4a"
Filtergraph 'loudnorm' was defined for audio output stream 0:0 but codec copy was selected.
ERROR: Filtering and streamcopy cannot be used together.
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -i "file:myfile.m4a" -c copy -f mp4 "-filter:a" loudnorm "file:myfile.temp.m4a" Filtergraph 'loudnorm' was defined for audio output stream 0:0 but codec copy was selected. ERROR: Filtering and streamcopy cannot be used together.
As the error points out filters can't be applied if the audio is copied directly from the source, you'll want to define an audio format for conversion eg. --audio-format mp3 --audio-quality 0 --prefer-ffmpeg --postprocessor-args "-filter:a loudnorm"
should do the trick.
thank you for your quick answer.
This is the cmdline i used
youtube-dl -v -x --audio-format flac --audio-quality 0 --prefer-ffmpeg --postprocessor-args "-filter:a loudnorm" %1
@mbehm same error comes up while using your command when the audio file downloaded is m4a
. When it is webm
, no issue at all. I tried it with this link: https://www.youtube.com/watch?v=qFDP9egTwfM
Although I am also using the latest youtube-dl binary for windows (2021.12.17) and ffmpeg (2022-07-28), this still comes up:
> youtube-dl -v -x --audio-format mp3 --audio-quality 0 --prefer-ffmpeg --postprocessor-args "-filter:a loudnorm" https://www.youtube.com/watch?v=qFDP9egTwfM
[youtube] qFDP9egTwfM: Downloading webpage
[download] Netsky - Rio (Official Video) ft. Digital Farm Animals.m4a has already been downloaded
[download] 100% of 3.63MiB
[ffmpeg] Correcting container in "Netsky - Rio (Official Video) ft. Digital Farm Animals.m4a"
ERROR: Filtering and streamcopy cannot be used together.
EDIT:
A quick workaround for this would be to skip the separate ffmpeg "fixup" call by adding --fixup warn
or --fixup never
to the command. However this would mean that the ending file would still be DASH
type.
EDIT 2: To resolve this, I forked the repository and added a separate boolean argument so as to ignore the command line arguments on fixup ffmpeg calls. This means there is no need to skip fixup and the audio normalization happens anyway when converting the file. I also compiled windows exe versions of this update and added them as a release to my repository.
As this update is breaking a feature basically (someone wanting ffmpeg arguments without conversion), I will not be creating a pull request. However, if there are any ground breaking changes to youtube in the future, I will be sure to be syncing my repository for anyone wanting to use it.
Solution 1: apply normalisation once the file has been downloaded; to minimise re-encoding, download the audio in native format and use a suitable ffmpeg (or other) command to get the desired normalised format.
Solution 2: back-port yt-dlp's more flexible system, if you can understand it, for placing user-specified arguments in downloader and postprocessor command lines.
Closing pending a PR for (2) above.