Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Split single mp3 of whole CD

I had a single mp3 recording of an entire CD that I wished to split into the individual tracks. Thankfully, people smarter than me have already solved this problem on the Internet.

The first order of business was to find the track times. Thankfully, musicbrainz.org delivered TOC info. It looks like this but I wasn't able to figure how to download a cddb-style TOC file, but that's OK. I used Firefox's CTRL+click-and-drag functionality to select just a column of the start times of the tracks, and pasted them in a file.

I ended up having to subtract lots of seconds, because my recording was somehow skewed. I also remember that ffmpeg can only be so precise with seconds (due to how compression works?) so that affected things to. So my track times ended up a little different than the TOC I used.

File ./times

00:02:21
00:04:32
00:08:32
00:12:25
00:15:12
00:19:14
00:22:53
00:25:52
00:29:58
00:35:03
00:39:27
00:42:48
00:46:54
00:48:54
00:53:33
00:58:17
01:02:44

Notice how I had to add the end time of the last track! Armed with these times, I used a small script from Unix.SE, of course.

split.sh (Source)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/bash
# Adapted from https://unix.stackexchange.com/questions/659789/automating-the-splitting-of-a-large-mp3-file-with-ffmpeg-into-multiple-files-in
x="00:00:00"
z=0
filename=$(basename -- "$2")
ext="${filename##*.}"
filename="${filename%.*}"
initcmd="ffmpeg  -nostdin -hide_banner -loglevel error -i $2"
while read y ; do
   initcmd+=" -ss $x -to $y -c copy $filename$z.$ext"
   let "z=z+1"
   x=$y 
done < $1
${initcmd}

Then I ran the script:

./split.sh ./times album.mp3

I had to subtract lots of seconds, smeared across the whole track. I suspect the whole recording was the tiniest bit fast, but I also remember that ffmpeg plays fast and loose with relative time specifications.

But now, I had an mp3 file for each track! Then I used puddletag and its "Tag Sources" window to load the musicbrainz information for these songs.

Comments