Skip to content

Instantly share code, notes, and snippets.

@raspi
Last active March 24, 2021 13:52
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 raspi/49248acd2b93fb2f5b3f1744e5da3041 to your computer and use it in GitHub Desktop.
Save raspi/49248acd2b93fb2f5b3f1744e5da3041 to your computer and use it in GitHub Desktop.
Cut a part of a video with FFMPEG
#!/bin/bash
# Cut a part of a video with FFMPEG
if [[ $# -eq 0 ]] ; then
echo "Usage:"
echo " $0 <filename> <start position timestamp> <duration> [additional params]"
echo ""
echo "Example:"
echo " Cut 50 seconds starting from timestamp 01:23:45 from file myvid.mp4"
echo " $0 myvid.mp4 01:23:45 00:00:50"
echo ""
exit 0
fi
if [[ $# -lt 3 ]] ; then
echo "No enough parameters"
exit 1
fi
# File name to read from
FNAME=$1
if [[ ! -f "$FNAME" ]]; then
echo "Not a file: $FNAME"
exit 1
fi
# Timestamp
TSTAMP=$2
# Duration
DURATION=$3
shift
shift
shift
PARAMS="-hide_banner -avoid_negative_ts make_zero -async 1 -strict -2"
PARAMS="$PARAMS -c copy"
#echo "$PARAMS"
if [[ "$PREVIEW" == "1" ]]; then
ffplay -ss $TSTAMP -t $DURATION $PARAMS -volume 20 -loop 0 "$@" "$FNAME"
exit 0
fi
NEWNAME=${FNAME%.*}-cut-ts${TSTAMP//:/}-d${DURATION//:/}.${FNAME##*.}
ffmpeg -ss $TSTAMP -i "$FNAME" -to $DURATION $PARAMS "$@" "$NEWNAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment