Skip to content

Instantly share code, notes, and snippets.

@kowalcj0
Created September 8, 2018 22:37
Show Gist options
  • Save kowalcj0/8cf8ce7d6adb2176b21bed794609ad3d to your computer and use it in GitHub Desktop.
Save kowalcj0/8cf8ce7d6adb2176b21bed794609ad3d to your computer and use it in GitHub Desktop.
Trim videos losslessly with ffmpeg
alias trim=trim
function trim() {
if [ "$#" != "3" ]; then
echo "Please provide all 3 arguments: 'filename start duration'"
return 1
fi
video="${1}"
filename="${1%.*}"
extension="${1##*.}"
start="${2}"
duration="${3}"
if [ ! -f "${video}" ]
then
echo "$0: File '${video}' not found."
fi
case ${start} in
''|*[!0-9]*)
echo "start has to be a number"
return 1
;;
*)
esac
case ${duration} in
''|*[!0-9]*)
echo "duration has to be a number"
return 1
;;
*)
esac
end=$(echo ${start} + ${duration} | bc)
output="${filename}-trimmed-from-${start}s-to-${end}s.${extension}"
echo "Trimming video from ${start}s till ${end}s..."
ffmpeg -nostdin -hide_banner -loglevel quiet -ss ${start} -t ${duration} -i "${video}" -c copy "${output}"
echo "Trimmed video saved as ${output}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment