Batch script to convert, youtube-dl webm file to mp3 with ffmpeg
Asked Answered
S

5

32

I'm using youtube-dl to extract the best possible audio quality from youtube videos. However the best quality audio usually turns out to be the webm format, which isn't useful. I would like to write a batch script that converts the webm file to an mp3 with ffmpeg. I've already tried using this guide on reddit to do it, but it doesn't seem to work. It appears to create an empty mp3 file that displays an error when i try to play it and the meta data is also completly blank.

Here is the batch script:

for %%a in ("Downloaded\*.*") do %CD%\ffmpeg\bin\ffmpeg.exe -i "%%a" -vn -ar 44100 -ac 2 -ab 192k -f mp3 "Converted\%%~na.mp3" pause

I'll also give an explanation of how the whole thing should work.

The idea is, is that you use youtube-dl to extract the best possible audio, then put that file into the Downloaded folder (see pic below) and then you run the mp3 script (which uses commands from ffmpeg) to convert the webm file in the Downloaded folder to a mp3 file, and place it in the Converted folder. The mp3 script is the code above. But it doesn't seem to work properly.

I'm not very familiar with batch scripting, nor with ffmpeg so any help would be appreicated.

Here is the picture to complement the explanation part.

enter image description here

Soul answered 5/7, 2017 at 12:53 Comment(0)
D
61

youtube-dl already contains this functionality - just specify that you want mp3:

youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=BaW_jenozKc

Replace https://www.youtube.com/watch?v=BaW_jenozKc with your actual URL.

Deathblow answered 5/7, 2017 at 14:47 Comment(9)
I'm aware of that, however I'd prefer it so that i get the best audio first as webm then convert to mp3 afterwards. That way I know i'm getting the best possible quality. Getting an mp3 directly might be worse quality than if I i got webm then converted. If you see what i mean. Unless you know otherwise of course.Soul
YouTube does not even serve mp3s. You seem to be confusing the --audio-format and the -f option. The former determines what output you want, the latter what format you want to download. By default, youtube-dl will already pick the highest quality. If you want you can add -f bestaudio/best to force youtube-dl to only download the best audio-only stream. This will not improve quality though unless YouTube decides to add mp3 support, because highest quality is already the default behavior of youtube-dl.Deathblow
Okay, so doing this: youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=BaW_jenozKc would get the best audio quality from youtube, then turn it into an mp3 file? The issue i'm worried about is that the best quality is usually webm which i have no use for. So my thought was to extract webm then convert it to mp3 or any other supported format with as little quality loss as possible from the original webm file.Soul
Ahh okay i understand now. Thanks for your patience and understanding.Soul
Sorry to bother you again, but i'm wondering if i did flac rather than mp3, then i would get better quality? Flac is lossless i think where as mp3 is compressed a lot more. Or would it make no difference?Soul
Yes, flac will be better quality, as good as the original. I very much doubt that the difference is perceivable to most humans though. In blind studies with top-end equipment, even sound experts struggle to distinguish 128KBit/s mp3 and lossless audio.Deathblow
I see, i suppose i can save on filespace by doing mp3 then. Thanks again.Soul
Using the successor to youtube-dl, yt-dlp, mp3 format isn't an option anymore as of 2022 (or maybe it's a song-per-song basis). Now you need to do -f bestaudio[ext=m4a] (see other answer) and then use ffmpeg to convert to mp3. askubuntu.com/questions/84584/converting-mp4-to-mp3Jacob
This method is simple but it may downgrade the audio quality. Found a file originally 138kbps but downgraded to 109kbps using this method.Marijane
E
19

You can convert through this script:

for FILE in *.webm; do
    echo -e "Processing video '\e[32m$FILE\e[0m'";
    ffmpeg -i "${FILE}" -vn -ab 128k -ar 44100 -y "${FILE%.webm}.mp3";
done;

Save it into a .sh file and execute it. It will automatically convert all the .webm into .mp3

Embitter answered 25/3, 2021 at 11:15 Comment(3)
You haven't seen the batch-file tag?Uhl
@Reino, no, I haven't. Do you think my answer wasn't pertinent?Embitter
Instead of shell, this should be done using Makefile and make -- so as to be able to use all available processing cores in the machine converting in parallel.Bechuanaland
L
5

If you added -f bestaudio option to the command youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=BaW_jenozKc , it would first download the best audio from youtube, and then convert it to mp3 without needing an ffmpeg bash script.

In this case the command would be

youtube-dl -f bestaudio -x --audio-format mp3 https://www.youtube.com/watch?v=BaW_jenozKc

Hope this helps.

Lynnettelynnworth answered 28/12, 2022 at 18:41 Comment(1)
wow that bestaudio thing seems very useful!Camise
E
3

in my use case, .ogg was supported

ffmpeg -i "input.webm" -vn -c:a copy "output.ogg"

without reencoding: -c:a copy

.webm files indeed can contain vorbis audio, but it can also contain opus audio. Also an ogg file can contain both audio formats. One can transfer the audio without conversion to an .ogg file:

https://askubuntu.com/questions/1258842/how-to-save-the-audio-from-webm-to-ogg-without-transcoding-using-ffmpeg#1258910

Empurple answered 12/7, 2021 at 16:43 Comment(1)
Saved me an hour of waiting for ffmpeg to re-encode, thanks!Achromatic
G
1

webm either uses vorbis or opus audio codec, both are far superior to mp3 in quality. aac audio is available on youtube, which is somewhere between opus and vorbis in quality and is heavily supported by media players and gadgets. quality wise, re-encoding lossy audio to another lossy audio is not recommended, especially if one of those autio formats is mp3.

i'd grab that aac if i were you.

as per this answer:

youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata <Video-URL>
Gob answered 5/9, 2018 at 23:53 Comment(3)
I usually use the m4a format, I'm not sure if that's AAC or not though.Soul
m4a is an aac stream in an mp4 container. 'apple aac' is actually an m4a file with an aac extension :OGob
I see, i just see and use m4a option when i extract youtube videos with youtube-dl.Soul

© 2022 - 2025 — McMap. All rights reserved.