31. Collecting mp3s

[status: content-mostly-written]

NOTE: before having anyone work on this project you should make sure that the videos you are downloading from youtube are all “OK” to be downloaded. Check the copyright on the videos and youtube’s terms for them.

31.1. Purpose: turn audio from youtube into mp3s

Take some URLs with songs. I will show some that are licensed with an appropriate Creative Commons license, which means you will not get in trouble if you download and reuse this music.

31.2. preparation/prerequisites

$ sudo apt-get install python3-pip
$ pip3 install --user --upgrade youtube-dl
  • the programs easytag and id3info

$ sudo apt-get install easytag libid3-tools
  • ffmpeg for converting formats

$ sudo apt-get install ffmpeg
  • the media player vlc

$ sudo apt-get install vlc
  • a music player. Your system probably come with rhythmbox; another is “clementine”. You can install them with:

$ sudo apt-get install rhythmbox

$ sudo apt-get install clementine

31.3. Get the video

Grab the video with:

$ youtube-dl -t "https://www.youtube.com/watch?v=FHbwpfxARb0"

you can see any files that might have been downloaded with:

$ ls -sh

then canonicalize and clean up the file name a bit with something like:

$ mv Hartmut\ Lindemann\ Plays\ Paganini\ -\ Sonata\ per\ la\ Gran\ Viola-FHbwpfxARb0.mp4 Hartmut_Lindemann_Plays_Paganini-Sonata_per_la_Gran_Viola.mp4

(Note that that “mv” command could use a lot of “TAB” for filename completion so as to nail the backslashes.)

Now that the filename does not have spaces it is much more manageable.

List again with:

$ ls -sh

31.4. Verify that it’s a good video file

You can now play this on your own computer (even disconnected from the network!):

$ vlc Hartmut_Lindemann_Plays_Paganini-Sonata_per_la_Gran_Viola.mp4

31.5. Extracting the audio portion

Now extract the mp3 audio from the video with:

$ ffmpeg -i Hartmut_Lindemann_Plays_Paganini-Sonata_per_la_Gran_Viola.mp4 Hartmut_Lindemann_Plays_Paganini-Sonata_per_la_Gran_Viola.mp3

And now a listing with

$ ls -sh

shows that you have the mp3 file.

31.6. Tagging the mp3 file

There is a format called “id3” which lets you put information about a piece into a music file. This allows music players to form a database of your music and to display information about when you play it.

Edit the file with

$ easytag Stevie-Wonder_Blowin-in-the-wind-Live.mp3

Set the track title and artist.

You can verify that the id3 tags are set with:

$ id3info Stevie-Wonder_Blowin-in-the-wind-Live.mp3

Now you can put it in your ~/Music/ directory and your music player will pick it up.

Note that you can use the –extract-audio option for youtube-dl to extract the audio immediately, but it might use an obscure format like “.opus”, so you’d still need to use ffmpeg to convert it to mp3.

31.7. A shortcut to the mp3

We wanted to see the details of how we can convert files with ffmpeg, but here is a short cut using some of youtube-dl’s options:

youtube-dl -t -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 'https://www.youtube.com/watch?v=FHbwpfxARb0'

# the next one is considered best practice for title extraction:
youtube-dl -o "%(title)s.%(ext)s" -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 'https://www.youtube.com/watch?v=3SL0oRcD7t0'

See this answer on askubuntu.com: