Skip to content

Instantly share code, notes, and snippets.

@ivanskodje
Last active November 6, 2024 04:21
Show Gist options
  • Save ivanskodje/5e6f4f7d883cd1124d6d0680b51c4cd9 to your computer and use it in GitHub Desktop.
Save ivanskodje/5e6f4f7d883cd1124d6d0680b51c4cd9 to your computer and use it in GitHub Desktop.
youtube-dl for downloading music from YouTube

Downloading Music Playlists from YouTube

Disclaimer

Use this knowledge at your own risk.

youtube-dl for Windows

youtube-dl will allow you to download entire youtube playlists and store them as MP3s. This will also allow you to download individual MP3s.

1. Download the youtube-dl.exe

You can get it here from their official web-site. Place it somewhere familiar (Recommending C:/youtube-dl/youtube-dl.exe)

2. Download and install ffmpeg

You can get it here from FFMPEGs web-site.

3. Unzip the ffmpeg file

Rename the unzipped folder to "ffmpeg" and place it somewhere familiar (Recommending C:/ffmpeg/).

4. Add the C:/ffmpeg/bin/ and C:/youtube-dl/ folder to PATH

Search for Environment and choose Edit the system environment variables option.
Press the Environment Variables... button.
Double-click the Path variable, in the top section.
Select New and add C:/ffmpeg/bin/.
Do the same for C:/youtube-dl/.

The contents of the folders will now be accessible directly without having to specify the path, the next time you open powershell/cmd.

5. Create a bat script file

We need to create bat script that will store all the information we need to quickly and easily download music from YouTube. Create a new file and name it 'youtube-mp3.bat'. Put it in the same folder as youtube-dl.exe (from step 1).

6. Paste in the script

for %%a in (%*) do (
.\youtube-dl.exe ^
--extract-audio ^
--audio-format mp3 ^
--output "Music/YouTube/%%(playlist_title)s/%%(playlist_index)s - %%(title)s.%%(ext)s" ^
%%a
)

  1. Remember to add your own windows username in YOUR-USERNAME-HERE.
  2. If you wish to change the formatting of the output files, you can learn more about it here.
  3. If it cannot find ffmpeg, then you most likely did not correctly add C:\ffmpeg\bin\ to PATH environment. Search for a solution.

7. Download music from YouTube!

In order to start downloading music, you need to open up a command prompt or Windows PowerShell. In Explorer, navigate to the folder with the youtube-mp3.bat script. Hold left-shift and right mouse click in the folder (Not directly on an item). Select 'Open PowerShell Window here', or the 'Command Prompt' equivalent.

To download youtube music, run:

.\youtube-mp3.bat [URL-TO-YOUTUBE-PLAYLIST]

  1. Keep in mind that it must be an URL to the playlist itself, and NOT a video in a playlist!

For example:

.\youtube-mp3.bat https://www.youtube.com/playlist?list=PLYPeGyDTfTSuyzSADAeZiuR28F1PkXERE

You can also add multiple URLs and playlists, separated only by a space:

.\youtube-mp3.bat [URL1] [URL2] [URL3]

For example:

.\youtube-mp3.bat https://www.youtube.com/playlist?list=PLYPeGyDTfTSuyzSADAeZiuR28F1PkXERE https://www.youtube.com/watch?v=qRC4Vk6kisY https://www.youtube.com/playlist?list=PLthjj8uzMeTVDDlXYmxOqN5Uvfj2OXpmg

If you open up a text editor, you can prepare all your URL in a similar fashion:

.\youtube-mp3.bat ^
[URL1] ^
[URL2] ^
[URL3]


youtube-dl for Linux

1. Download youtube-dl

You can get it here from their official web-site. Place it somewhere familiar (Recommending /usr/local/bin/youtube-dl)

2. Install ffmpeg

On Ubuntu/Linux Mint based systems, open the terminal and run: sudo apt-get install ffmpeg -y

3. Create a bash script

We need to create a bash script that will store all the information we need to quickly and easily download music from YouTube.

sudo touch /usr/local/bin/youtube-mp3 Adding it in /usr/local/bin will allow you to access it without having to specify a path.

4. Edit the file

In this example we will be using nano. Feel free to use whatever editor you want, however.

sudo nano youtube-mp3 If nano does not work for you, you can install it by running: sudo apt-get install nano -y.

5. Paste in the script

#!/bin/bash
for i in "$@";
do
youtube-dl --extract-audio \
--audio-format mp3 \
--output "~/Music/%(playlist_title)s/%(playlist_index)s - %(title)s.%(ext)s" \
"$i"
done

  1. Feel free to change the path however you wish. ~/ will be within your users home directory.
  2. If you wish to change the formatting of the output files, you can learn more about it here.
  3. If you have any issues, feel free to comment about it.

6. Download music from YouTube!

In order to start downloading music, simply run in the terminal: youtube-mp3 <link to youtube-video or playlist>.

To download youtube music, run:

youtube-mp3 [URL-TO-YOUTUBE-PLAYLIST]

  1. Keep in mind that it must be an URL to the playlist itself, and NOT a video in a playlist!

For example:

youtube-mp3 https://www.youtube.com/playlist?list=PLYPeGyDTfTSuyzSADAeZiuR28F1PkXERE

You can also add multiple URLs and playlists, separated only by a space:

youtube-mp3 [URL1] [URL2] [URL3]

For example:

youtube-mp3 https://www.youtube.com/playlist?list=PLYPeGyDTfTSuyzSADAeZiuR28F1PkXERE https://www.youtube.com/watch?v=qRC4Vk6kisY https://www.youtube.com/playlist?list=PLthjj8uzMeTVDDlXYmxOqN5Uvfj2OXpmg

If you open up a text editor, you can prepare all your URL in a similar fashion:

youtube-mp3 /
[URL1] /
[URL2] /
[URL3]

@ivanskodje
Copy link
Author

Worth mentioning that if you add the /youtube-dl/ folder to path environment (similar to ffmpeg folder), you can run youtube-mp3.bat from anywhere. (But you might have to remove the ./ infront inside the script.

@revelutions
Copy link

I don't see ffmpeg used in step 6?

@lucaspcamargo
Copy link

I don't see ffmpeg used in step 6?

youtube-dl itself will make use of it as needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment