Trim an audio or video file using ffmpeg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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