Category: Tech

Downloading YouTube videos as MP3 files with youtube-dl, ffmpeg, and batch scripts

yotube-to-mp3-download

Tired of weird websites and extensions? Use this instead!

I sometimes download trailers to mash up or music to include in my YouTube videos. I used to do this very manually, sometimes using sketchy tools or websites, and it took ages.

I found youtube-dl and that made getting the videos for editing a LOT faster. I also realized that with ffmpeg then I could download videos for their audio and then convert it directly to an mp3 file.

So, for several months I’d first run youtube-dl and then ffmpeg from the Windows Command Line before finally realizing I could write a batch script to save a lot of time.

Here are how you can do the same (and have fun in the process!):

1. First, download and install youtube-dl from this github page. As the name implies, youtube-dl is great for downloading videos from YouTube. I recommend installing in your user directory so you don’t run into permissions issues (e.g., C:\Users\Jared in my case).

  • Note that when you double click the .exe installer, a Command Prompt window will pop up and then quickly disappear.

2. Next, you’ll set up ffmpeg. ffmpeg does wonders in video and audio file conversion; I use it in my video editing often (since for some reason Adobe Premier Pro is very finicky about imports).

  • To download the ffmpeg installer, go to the ffmpeg downloads page and click on the Windows logo, and then click one of the links under Windows- I recommend BtbN because they have a cleaner list of builds. The ffmpeg-master-latest-win64-gpl.zip build is what I’ve used.
  • Extract the .zip file to the same folder you used in Step 1 to install youtube-dl (i.e., your user directory).
  • After you extract the .zip file, rename the folder to just “ffmpeg” to keep things easier later on.
  • Take a look at the contents- when you open the ffmpeg folder, do you see a README file, “bin” folder, etc? If so, move on to the next bullet. If not, keep clicking into folders until you see them. Once you find them, cut and paste everything into the top-level ffmpeg folder.
  • Add the ffmpeg bin folder (e.g., C:\Users\Jared\ffmpeg\bin) to your system PATH environment variable by running Command Prompt as an Administrator and then entering the following line:
    • setx /m PATH “REPLACE_FOLDER_ADDRESS;%PATH%”
    • Note that you’d replace the REPLACE_FOLDER_ADDRESS in between the quotation marks with yours, so in my case it would be:
    • setx /m PATH “C:\Users\Jared\ffmpeg\bin;%PATH%”
  • Restart your computer.

3. Once you’ve restarted, open up a Notepad doc and paste in this code:

@echo off
:start
set /p videoid=Enter video ID:
cd YOUR_DIRECTORY_HERE
youtube-dl --extract-audio --audio-format mp3 --output "%%(title)s.%%(ext)s" http://www.youtube.com/watch?v=%videoid%
%SystemRoot%\explorer.exe YOUR_DIRECTORY_HERE
set choice=
set /p choice="Want to download another? Press 'y' and then Enter. If not, hit Enter: "
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='y' goto start
pause
  • Note that you need to replace the “YOUR_DIRECTORY_HERE” text on lines 4 and 6 to whatever directory you’ve been using, without the quotes. So in my case, line 4 would become: cd C:\Users\Jared

4. Save the file to the same folder where you have youtube-dl and ffmpeg installed (again, ideally your user directory as mentioned above). When you save the file, change the filetype dropdown from “.txt” to “All files” and then add “.bat” to the file name (e.g., youtube-to-mp3.bat)

5. Now right click on the batch file you just created and make a shortcut to it. Cut and paste that shortcut onto your desktop, or into your favorites bar, or wherever you want to easily access it.

Now when you double click the shortcut file it will run the batch file you created in Step 3 and ask for the ID of the video you want to get as an MP3 file. For example, if the video’s URL is https://www.youtube.com/watch?v=MMsmX6FgNXk then the ID is MMsmX6FgNXk

6. The mp3 file will be saved in the same directory you set in Step 4. The script will automatically open up that folder (saving you a few clicks) and then ask if you want to download another. If you do, type “y” and then Enter. If not, just hit Enter twice and it will close.

You now have a simple solution to easily download a YouTube video as an mp3 file. Enjoy!

Obligatory note: I don’t use this for illegal activity and you shouldn’t either. I’m sharing this for academic/research purposes showing an application of open source libraries with batch script process improvements.

Changelog:

  • 1.3 (7 July 2022) – Updated instructions to include ffmpeg download process changes, add more clarity on how to save the file in Step 4, as well as to let people know that the youtube-dl.exe installer is expected to open and close the CMD window quickly (thanks for the heads up Jon!)
  • 1.2 – Updated instructions to be more clear on ffmpeg installation
  • 1.1 – Added support for repeated downloads and to open the download folder location automatically
  • 1.0 – Basic downloading and converting logic