Ripping an album from youtube - CLI Style
With the advent of Spotify, Apple Music, Youtube, Pandora and many other streaming music services, the need to have local mp3 files doesn't crop up very often. However, my kids either have cheap mp3 players or use their 3ds's to play local mp3 files.
This post is a quick tip on ripping an album from youtube using a web browser and a few cli apps. Remember, most tasks don't need a bloated gui to be done efficiently.
Requirement
- A Web browser that can play youtube videos
- Youtube-dl
- ffmpeg
- Bash
Prep work
Install ffmpeg
Ubuntu
sudo apt-get install ffmpeg -y
Fedora
sudo yum install ffmpeg
Gentoo
sudo emerge ffmpeg
Install Youtube-DL
If your on a Debian or Ubuntu flavor of linux
sudo apt-get install youtube-dl -y
Fedora
sudo yum install youtub-dl
On my favourite, Gentoo
emerge --ask youtube-dl
Download the album
At this point you have all the tools you need to get the job done. Have a browse around on youtube to find the album you want an offline copy of and copy the url of the page. Then from a command prompt:
mkdir ~/tmp
cd ~/tmp
youtube-dl -x --audio-format mp3 https://youtube.com/fullurltovideo
Create a list file
While the audio file is downloading, your going to want to create a simple txt file which lists the tracks, titles and start and end timings. I simply fast forwarded through each track toward the end of the song and made note of the mintes and seconds. I created a file with each line representing a track in the album with the following details:
Track Number-Track title-Start duration-End duration
The durations are in the form of HH:MM:SS. Here is what my file looks like:
cat ~/tmp/list.txt
01-The Greatest Show-00:0:00-5:08
02-A Million Dreams-00:5:08-9:38
03-A Million Dreams Reprise-00:9:39-10:38
04-Come Alive-00:10:38-14:25
05-The Other Side-00:14:25-17:58
06-Never Enough-00:17:58-21:28
07-This Is Me-00:21:38-25:23
08-Rewrite the Stars-00:25:23-28:59
09-Tightrope-00:28:59-32:50
10-Never Enough (Reprise)-00:32:50-34:14
11-From Now On-00:34:14-40:12
Split the audio to seperate mp3's
Now that my file has finished downloading, I can convert the file into separate song files
Here is the little bash script I wrote to split the file based on the contents of the list.txt file
Note the -nostdin
parameter below is required to prevent ffmpeg from
consuming bytes from input which makes it go screwy
cat splitsong.sh
#!/bin/bash
while IFS=- read tr ti s e; do
FILENAME="${tr} - The Greatest Showman - ${ti}.mp3"
ffmpeg -nostdin \
-i "$2" -acodec copy \
-ss "$s" -to "$e" \
"${FILENAME}" < /dev/null
done <"$1"
I then execute the file like this:
chmod +x splitsong.sh
./splitsong.sh list.txt 'Some Sound Track List-qDZLSHY1ims.mp3'
And the whole thing is over in a matter of seconds.
Tags: bash-tips, mp3, ffmpeg, cli, scripting, youtube, music, linux