Skip to content

Instantly share code, notes, and snippets.

@kairusds
Last active August 1, 2023 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kairusds/f99c05471ac0327e84c504624a9f6a86 to your computer and use it in GitHub Desktop.
Save kairusds/f99c05471ac0327e84c504624a9f6a86 to your computer and use it in GitHub Desktop.
Trim videos with ffmpeg

Use "hh:mm:ss.mss" as the format for the timestamps if you don't want to enable fix_keyframes so that the trim will be instant. For example:

fftrim video.mp4 "01:00:03.000" "03:00:00.000"
#!/bin/bash
if [ $# -lt 3 ]; then
echo "Usage: fftrim <file path> <start timestamp> <end timestamp> [fix_keyframes: true|false] [output_extension=default]"
exit 1
fi
if [ "$4" = "true" ]; then
keyframe_arg="-async 1"
else
keyframe_arg="-c copy"
fi
epoch_secs=$(date +%s)
filepath="$1"
ext=${filepath: -3}
filename=${filepath::-4}
if [ -n "$5" ]; then
ext="$5"
fi
ffmpeg -i "$filepath" -ss "$2" -to "$3" $keyframe_arg "$filename.trimmed_$epoch_secs.$ext"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment