26

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

SneakyShrike
  • 723
  • 1
  • 10
  • 31

5 Answers5

52

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.

  • 1
    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. – SneakyShrike Jul 05 '17 at 16:36
  • 1
    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**. –  Jul 05 '17 at 16:58
  • 1
    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. – SneakyShrike Jul 05 '17 at 19:11
  • 2
    Ahh okay i understand now. Thanks for your patience and understanding. – SneakyShrike Jul 05 '17 at 22:58
  • 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? – SneakyShrike Jul 06 '17 at 09:50
  • 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. –  Jul 06 '17 at 10:04
  • 1
    I see, i suppose i can save on filespace by doing mp3 then. Thanks again. – SneakyShrike Jul 06 '17 at 10:22
  • 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. https://askubuntu.com/questions/84584/converting-mp4-to-mp3 – Alkanshel Jul 28 '23 at 18:07
13

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

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
2

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

Mr. Doge
  • 796
  • 5
  • 11
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>
robotik
  • 1,837
  • 1
  • 20
  • 26
1

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.