Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Created September 14, 2022 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfroThundr3007730/73380630ea7ff9e3ec331e52445c8371 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/73380630ea7ff9e3ec331e52445c8371 to your computer and use it in GitHub Desktop.
Splits an audio file based on timestamps in a cuesheet
#!/bin/bash
# Splits an audio file based on timestamps in a cuesheet
split_music_compilation() {
[[ -f $1 ]] || { echo 'Audio file not found:' "$1"; return 1; }
[[ -f $2 ]] || { echo 'Cuesheet file not found:' "$2"; return 1; }
local source=$1 cuesheet=$2 count=0 pstamp='0:00' stamp title album
while read -r line; do
[[ $line =~ ([0-9]+:[0-9]+:[0-9]+).-.(.*).\[(.*)\] ]] && {
stamp=${BASH_REMATCH[1]}
title=${BASH_REMATCH[2]}
album=${BASH_REMATCH[3]}
[[ count -gt 0 ]] && nice -n 19 ionice -c 3 ffmpeg -i "$source" -ss "$pstamp" \
-t $(($(date -d "$stamp" +%s)-$(date -d "$pstamp" +%s))) -c copy -v 0 \
"$(printf '%02d - %s [%s].%s' "$count" "$ptitle" "$palbum" "${source##*.}")" &
((count++))
pstamp=$stamp
ptitle=$title
palbum=$album
}
done < "$cuesheet"
nice -n 19 ionice -c 3 ffmpeg -i "$source" -ss "$pstamp" -c copy -v 0 \
"$(printf '%02d - %s [%s].%s' "$count" "$title" "$album" "${source##*.}")"
return 0
}
@AfroThundr3007730
Copy link
Author

The accompanying cue sheet should be in the format below:

00:00 - Track Title [Album Title]
01:30 - Next Track [Album Title]
...

The regex can be adapted to handle other name formats.

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