Skip to content

Instantly share code, notes, and snippets.

@mslinn
Last active January 23, 2022 18:06
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 mslinn/15a1308aa2ca04a418f404e42c7f32e0 to your computer and use it in GitHub Desktop.
Save mslinn/15a1308aa2ca04a418f404e42c7f32e0 to your computer and use it in GitHub Desktop.
Trim an audio or video file using ffmpeg
#!/bin/bash
function help {
echo "$(basename $0) - Trim an audio or video file using ffmpeg
Works with all formats supported by ffmpeg.
Seeks to the nearest frame positions by re-encoding.
Does not overwrite pre-existing output files.
Reduces file size procduced by OBS Studio by >80%.
Usage:
$(basename $0) "dir/file.mkv" START [END]
START and END have the format [HH:[MM:]]SS[.XXX]
END defaults to end of audio/video file
Examples:
$(basename $0) 'dir/file.mkv' 15 # Crop from 15.0 seconds to end
$(basename $0) 'dir/file.mkv' 3.25 9 # Crop from 3.25 seconds to 9 seconds
"
exit 1
}
if [ -z "$2" ]; then help; fi
if [ "$1" == -f ]; then
shift
OVERWRITE=-y
else
OVERWRITE=-n
fi
FNAME="$1"
ORIGINAL_FN=${FNAME%.*}
EXT=${FNAME##*.}
COPY_FNAME="${ORIGINAL_FN}.crop.$EXT"
START="$2"
if [ "$3" ]; then
TO="-to $3"
else
unset TO
fi
ffmpeg \
"$OVERWRITE" \
-i "$FNAME" \
-ss "$START" $TO \
-acodec copy \
"$COPY_FNAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment