Created
May 16, 2022 10:20
-
-
Save OjaswinM/37a90941e7e7d26415aa6aa938665e25 to your computer and use it in GitHub Desktop.
Trim a video range out 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 | |
if ! [ "$#" = "3" ] | |
then | |
echo "Usage: $0 [start-time] [end-time] [file-name]" | |
echo | |
echo "Trim out the file from start-time to end-time. | |
exit 1 | |
fi | |
START=$(echo $1 | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }') | |
END=$(echo $2 | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }') | |
FILENAME=$3 | |
DURATION=$(ffmpeg -i $FILENAME 2>&1 | grep Duration | awk '{ print $2 }') | |
DURATION=$(echo ${DURATION::-3}) | |
DURATION=$(echo $DURATION | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }') | |
RANGE=$(( $DURATION - $END -1 )) | |
echo $START $END $DURATION $RANGE $FILENAME | |
ffmpeg -t $START -i $FILENAME -c copy tmpA-$FILENAME | |
ffmpeg -ss $END -t $DURATION -i $FILENAME -c copy tmpB-$FILENAME | |
echo "file '$PWD/tmpA-$FILENAME'" > tmp-files | |
echo "file '$PWD/tmpB-$FILENAME'" >> tmp-files | |
ffmpeg -f concat -safe 0 -i tmp-files -c copy trim-$FILENAME | |
rm tmp-files tmpA-$FILENAME tmpB-$FILENAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment