Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created March 6, 2020 17:41
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 tjluoma/7bc1ebb8e4716a61a577dfeb2be687bf to your computer and use it in GitHub Desktop.
Save tjluoma/7bc1ebb8e4716a61a577dfeb2be687bf to your computer and use it in GitHub Desktop.
take a .cue file and output a CSV showing timestamp and chapter title. See <https://talk.automators.fm/t/extract-chapters-from-mp3/6797>
#!/usr/bin/env zsh -f
# Purpose: take a .cue file and create a .csv showing timestamp first and chapter title second
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2020-03-06
NAME="$0:t:r"
if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
else
PATH="$HOME/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin"
fi
if [[ "$#" != "1" ]]
then
echo "$0 takes one argument, which must be a '.cue' file." >>/dev/stderr
exit 2
fi
FILE="$@"
[[ ! -f "$FILE" ]] && echo "$NAME: '$FILE' is not a file." >>/dev/stderr && exit 2
[[ ! -e "$FILE" ]] && echo "$NAME: '$FILE' does not exist." >>/dev/stderr && exit 2
EXT="$FILE:l:e"
[[ "$EXT" != "cue" ]] && echo "$NAME: '$FILE' does not end in '.cue'. I hope you know what you're doing." >>/dev/stderr
COUNT='1'
while
do
# make sure there is something to process
RESULT=($(awk "/TRACK/{i++}i==$COUNT" "$FILE"))
# if result is empty, we are either finished or we didn't find anything
if [[ "$RESULT" == "" ]]
then
if [[ "$COUNT" -gt "1" ]]
then
TOTAL=$(($COUNT - 1))
## if we found at least one, then we quit and assume we have done our job
## Comment out the next line if you don't want to know when the script is done
echo " $NAME: exiting after processing $TOTAL tracks" >>/dev/stderr
exit 0
else
echo "$NAME: did not find any TRACK info in '$FILE'. Is is a .cue file?" >>/dev/stderr
exit 1
fi
fi
TITLE=$(awk "/TRACK/{i++}i==$COUNT" "$FILE" | awk -F'"' '/ TITLE /{print $2}')
TIME=$(awk "/TRACK/{i++}i==$COUNT" "$FILE" | awk -F' ' '/ INDEX /{print $NF}')
## note that the TITLE is wrapped in " so hopefully if there is a comma in the title it will not break your CSV
## you could also use a TAB instead of a comma to separate them. The '\t' below will be replaced with a TAB:
##
## echo "${TIME}\t${TITLE}"
echo "${TIME} , \"${TITLE}\""
# increment the counter so we can move to the next track in the cue file
((COUNT++))
done
exit 0
#EOF
((COUNT++))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment