Skip to content

Instantly share code, notes, and snippets.

@nerdCopter
Last active November 14, 2024 17:23
Show Gist options
  • Save nerdCopter/808857ebfe867a61606e03cd8af62f8d to your computer and use it in GitHub Desktop.
Save nerdCopter/808857ebfe867a61606e03cd8af62f8d to your computer and use it in GitHub Desktop.
split (cut) video with MP4Box or ffmpeg as needed
#!/bin/bash
if [ $# -lt 3 ]
then
echo "Usage: ${0##*/} <filename> <fromtime [HH:]mm:ss> <totime [HH:]mm:ss> [suffix]"
echo " Ex: ${0##*/} INPUT.mp4 0:20 9:40 snip001"
echo " requires: gpac (MP4Box) and ffmpeg"
else
function to_sec() { echo "$1" | awk -F':' '{if (NF == 2) {print $1 * 60 + $2} else {print $1 * 60 * 60 + $2 * 60 + $3}}';}
filename=$(basename -- "${1}")
extension="${1##*.}"
filename="${1%.*}"
infile="${1}"
if [ "$4" == "" ]
then
outfile="${filename}_cut.${extension}"
else
outfile="${filename}_cut_${4}.${extension}"
fi
if [ "${extension,,}" == "avi" ] ; then ## if AVI case-insensitive
nice -n 19 ffmpeg -ss $(to_sec ${2}) -i ${infile} -c copy -t $( expr $(to_sec ${3}) - $(to_sec ${2}) ) ${outfile}
else
nice -n 19 MP4Box -splitz $(to_sec "${2}"):$(to_sec "${3}") "${infile}" -out "${outfile}"
fi
touch "${outfile}" -r "${infile}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment