Skip to content

Instantly share code, notes, and snippets.

@lpenaud
Created March 17, 2024 16:19
Show Gist options
  • Select an option

  • Save lpenaud/3707035d6c1f20671c82bb9bfffd499c to your computer and use it in GitHub Desktop.

Select an option

Save lpenaud/3707035d6c1f20671c82bb9bfffd499c to your computer and use it in GitHub Desktop.
Split mp3
#!/bin/bash
if [ $# -ne 3 ]; then
printf "Usage: %s LIST INFILE OUTDIR\n" "${0}"
exit 1
fi
function add_seconds () {
echo $(( $1 + $2 ))
}
function add_minutes () {
echo $(( $1 + ($2 * 60) ))
}
function add_hours () {
echo $(( $1 + ($2 * 3600) ))
}
function parse_list () {
local line
local -i seconds
echo 0
while read -r line; do
if [[ "${line}" =~ ^0?([0-9]+):0?([0-9]+):0?([0-9]+)$ ]]; then
seconds="$(add_hours "${seconds}" "${BASH_REMATCH[1]}" )"
seconds="$(add_minutes "${seconds}" "${BASH_REMATCH[2]}" )"
seconds="$(add_seconds "${seconds}" "${BASH_REMATCH[3]}" )"
echo "${seconds}"
fi
done
}
# infile outdir ...timestamps
function split () {
local infile="${1}" outdir="${2}"
local -i start_index="${3}" end_index i=1
shift 3
while [ $# -ne 0 ]; do
end_index="${1}"
ffmpeg -ss "${start_index}" \
-to "${end_index}" \
-i "${infile}" \
-c copy \
"${outdir}/${i}.mp3"
start_index="${1}"
(( i++ ))
shift
done
}
declare -a timetamps
mapfile -t timetamps < <(parse_list < "${1}")
split "${2}" "${3}" "${timetamps[@]}"
00:02:51
00:02:42
00:04:14
00:01:12
00:01:56
00:02:09
00:02:48
00:05:17
00:02:39
00:02:55
00:04:21
00:01:56
00:02:41
00:02:26
00:03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment