Skip to content

Instantly share code, notes, and snippets.

@tiberiosantos
Created May 27, 2017 20:30
Show Gist options
  • Select an option

  • Save tiberiosantos/a338b9119c43aa03fff0fac4d86e32e5 to your computer and use it in GitHub Desktop.

Select an option

Save tiberiosantos/a338b9119c43aa03fff0fac4d86e32e5 to your computer and use it in GitHub Desktop.
Split an audio file into tracks from a text file with titles and its times
#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) 2017 Tiberio A. Santos
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# Script to split an audio file into tracks from a text file with titles and its times
# The text file has following format and creates a directory with the audio files:
#
# Any Track Name 00:00
# Any Track Name 01:00
# Any Track Name 02:00
# Any Track Name 00:03:00
# Any Track Name 00:04:00
# Any Track Name 01:00:00
#
#
# Dependencies: bash, gawk, ffmpeg
red="\033[01;31m"
green="\033[01;32m"
normal="\033[00m"
usage() {
echo -e "
${green}Audio Splitter${normal} - Split an audio file into tracks from a text file with titles and its times.
Usage: ${green}$0${normal} ${green}-i${normal} [audio file] ${green}-t${normal} [tracks file]"
}
validate_params() {
if [[ -z $input || -z $tracks ]]; then
echo -e "${red}Invalid parameters!${normal}"
usage;exit 2
fi
}
extract() {
titles=()
times=()
time2seconds='
{
if (NF==2) {
print ($1*60)+$2
} else if (NF==3) {
print ($1*3600)+($2*60)+$3
}
}
'
get_title='{ gsub(/\s(([0-9]+:)?[0-9]+:[0-9]+)/, ""); print }'
get_time='{ match($0, /(([0-9]+:)?[0-9]+:[0-9]+)/, a); print a[1] }'
end=0
dest="${input%.*}"
readarray tracks < "$@"
for t in "${tracks[@]}"; do
titles+=("$(echo -n "$t" | awk "$get_title")")
times+=("$(echo -n "$t" | awk "$get_time")")
done
mkdir -p "$dest"
for t in "${!titles[@]}"; do
t1=$(echo -n ${times[t]} | awk -F: "$time2seconds")
t2=$(echo -n ${times[t+1]} | awk -F: "$time2seconds")
if [[ $[t+1] == ${#titles[@]} ]]; then
ffmpeg -i "$input" -c:a copy -ss $t1 "$dest"/"${titles[t]}.${input##*.}"
else
tt=$[t2-1-t1+t1]
ffmpeg -i "$input" -c:a copy -ss $t1 -to $tt "$dest"/"${titles[t]}.${input##*.}"
fi
done
}
while getopts i:t: OPT; do
case "$OPT" in
i) input="$OPTARG";;
t) tracks=true;extract "$OPTARG";;
*)
validate_params
esac
done
shift $((OPTIND-1))
validate_params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment