Skip to content

Instantly share code, notes, and snippets.

@tyndyll
Created September 25, 2015 14:46
Show Gist options
  • Save tyndyll/056765daac781de9855d to your computer and use it in GitHub Desktop.
Save tyndyll/056765daac781de9855d to your computer and use it in GitHub Desktop.
Given an audio file, break it into chunks
#!/bin/bash
# Copyright (c) 2013, Tyndyll
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.
function convert() {
$FFMPEG -i "$1" -ss $3 -t $4 $FFMPEG_OPTIONS "$2.$TYPE"
}
function help() {
echo "`basename $0` [options] <podcast_file>
-c Clobber an existing output directory. Allow podcast parts to be overwritten
-f Options to be passed to FFMPEG (default: $FFMPEG_OPTIONS)
-h Print this help file
-l Define the length in seconds into which parts should be cut into (default: $CHUNKLENGTH)
-m Output as MP3
-o Output directory
" 1>&2
exit 2;
}
declare -i CHUNKLENGTH=300
FFMPEG_OPTIONS="-ar 44100 -loglevel error"
CLOBBER=0
OUTPUT=
TYPE=wav
while getopts ":cf:hl:mo:" opt; do
case "${opt}" in
c)
CLOBBER=1
;;
f)
FFMPEG_OPTIONS=$OPTARG
;;
h)
help
;;
l)
CHUNKLENGTH=$OPTARG
if [[ $CHUNKLENGTH -eq 0 || $CHUNKLENGTH -gt 600 ]]; then
echo "Part length must be a number lower than 600" 1>&2
exit 2
fi
;;
m)
TYPE=mp3
;;
o)
BASENAME=$OPTARG
;;
u)
URL=$OPTARG
;;
*)
echo "Invalid option: -$OPTARG" >&2
help
;;
esac
done
shift $((OPTIND-1))
FFMPEG=`which ffmpeg`
if [ $? -ne 0 ]; then
echo "Cannot find ffmpeg. Exiting"
exit 127
fi
MP3INFO=`which mp3info`
if [ $? -ne 0 ]; then
echo "Cannot find mp3info. Exiting"
exit 127
fi
INPUTFILE=$1
if [ ! -e "$INPUTFILE" ]; then
echo "Cannot find input file. Exiting"
help
fi
if [[ -z $BASENAME ]]; then
BASENAME=`basename "$1" | sed -e 's/\.mp3$//'`
fi
if [[ -e "$BASENAME" ]]; then
if [[ $CLOBBER -eq 0 ]]; then
echo "Cannot output to $BASENAME. Directory exists. Exiting"
exit 3
fi
else
mkdir "$BASENAME"
fi
FFMPEG_OPTIONS="-y $FFMPEG_OPTIONS"
declare -i TOTALTIME=`$MP3INFO -p "%S" "$1"`
if [[ $? -ne 0 || $TOTALTIME -le 0 ]]; then
echo "Cannot determine overall file length. Exiting"
exit 3
fi
declare -i POS=0
declare -i PART=1
while [[ $TOTALTIME -gt 0 ]]; do
OUTPUTFILE=`printf "%s/%s%02d" "$BASENAME" "$BASENAME" $PART`
if [[ $TOTALTIME -lt $CHUNKLENGTH ]]; then
CHUNKLENGTH=$TOTALTIME
fi
echo "Converting part $PART, from $POS for $CHUNKLENGTH seconds"
convert "$1" "$OUTPUTFILE" $POS $CHUNKLENGTH
POS+=$CHUNKLENGTH
PART+=1
TOTALTIME=$(($TOTALTIME-$CHUNKLENGTH))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment