Skip to content

Instantly share code, notes, and snippets.

@aperezdc
Created May 15, 2013 08:45
Show Gist options
  • Save aperezdc/5582510 to your computer and use it in GitHub Desktop.
Save aperezdc/5582510 to your computer and use it in GitHub Desktop.
Splits a single audio file and a corresponding .cue file describing the tracks from it (as generated by some CD ripping/recording tools) into MP3 files, cutting the input file in the time point specified in the .cue file. Usage: ./cue-and-audio-to-mp3s input.wav < input.cue
#! /bin/bash
set -e
indexes=( )
titles=( )
read_indexes () {
local -a line
local tmp
local in_file=false
while read -ra line ; do
if [[ ${line[0]} = FILE ]] ; then
in_file=true
continue
fi
${in_file} || continue
if [[ ${line[0]} = INDEX && ${line[1]} = 01 ]] ; then
indexes=( "${indexes[@]}" "${line[2]}" )
elif [[ ${line[0]} = TITLE ]] ; then
unset 'line[0]'
tmp=${line[*]}
titles=( "${titles[@]}" "${tmp:1:-1}" )
fi
done
}
strip_lzeros () {
local v=$1
while [[ ${v:0:1} = 0 ]] ; do
v=${v:1}
done
if [[ -z ${v} ]] ; then
v=0
fi
echo "${v}"
}
index_to_ts () {
local mm=$(strip_lzeros "${1:0:2}")
local ss=$(strip_lzeros "${1:3:2}")
local ff=$(strip_lzeros "${1:6:2}")
ff=$(( 100 * ff / 75 ))
echo "00:${mm}:${ss}.${ff}"
}
index_diff () {
local mm1=$(strip_lzeros "${1:0:2}")
local ss1=$(strip_lzeros "${1:3:2}")
local ff1=$(strip_lzeros "${1:6:2}")
ss1=$(( ss1 + mm1 * 60 ))
ff1=$(( ff1 + ss1 * 75 ))
local mm2=$(strip_lzeros "${2:0:2}")
local ss2=$(strip_lzeros "${2:3:2}")
local ff2=$(strip_lzeros "${2:6:2}")
ss2=$(( ss2 + mm2 * 60 ))
ff2=$(( ff2 + ss2 * 75 ))
local ffd=$(( ff2 - ff1 ))
echo $(( ffd / 75 ))
}
read_indexes
items=${#indexes[@]}
for (( i = 0 ; i < items ; i++ )) ; do
title=${titles[$i]}
printf -v ofile "%02i. %s.mp3" "$(( i + 1 ))" "${title:-Unknown title}"
startpos=${indexes[${i}]}
endpos=${indexes[$((i+1))]}
if [ "${endpos}" ] ; then
endpos=( -t "$(index_diff "${startpos}" "${endpos}")" )
else
endpos=( )
fi
startpos=( -ss "$(index_to_ts "${startpos}")" )
echo "${ofile}..."
ffmpeg -loglevel warning -y -i "$1" -ac 2 -ar 44100 -ab 192k -acodec libmp3lame -vn "${startpos[@]}" "${endpos[@]}" "${ofile}"
endpos=""
startpos=""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment