Skip to content

Instantly share code, notes, and snippets.

@7aman
Last active July 12, 2019 11:09
Show Gist options
  • Save 7aman/d3b6931a8884614bcb843003d3138113 to your computer and use it in GitHub Desktop.
Save 7aman/d3b6931a8884614bcb843003d3138113 to your computer and use it in GitHub Desktop.
speed up or speed down whole or a part of a video #ffmpeg
#!/bin/sh
_usage="Usage:\n ffspeed input output speed\n ffspeed input output speed from\n ffspeed input output speed from to"
_version="0.0.1"
function display_usage() {
echo -e "\nffspeed version $_version"
echo -e "$_usage"
}
function all_speed {
FILE=$1
OUTFILE=$2
SPEED=$3
FPS=$(ffmpeg -i $FILE 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p")
ffmpeg -i $FILE \
-r $FPS*$SPEED -preset superfast -profile:v baseline \
-filter_complex "[0:v]setpts=(1/$SPEED)*PTS[v];[0:a]atempo=$SPEED[a]" -map "[v]" -map "[a]" $OUTFILE
}
function part_speed () {
FILE=$1
OUTFILE=$2
SPEED=$3
START=$4
END=$5
# if END is not provided set end of the file
if [ -z $5 ]
then
END=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $FILE)
fi
ffmpeg -i $FILE \
-filter_complex \
"[0:v]trim=0:$START,setpts=PTS-STARTPTS[v1];
[0:v]trim=$START:$END,setpts=(1/$SPEED)*(PTS-STARTPTS)[v2];
[0:v]trim=$END,setpts=PTS-STARTPTS[v3];
[0:a]atrim=0:$START,asetpts=PTS-STARTPTS[a1];
[0:a]atrim=$START:$END,asetpts=PTS-STARTPTS,atempo=$SPEED[a2];
[0:a]atrim=$END,asetpts=PTS-STARTPTS[a3];
[v1][a1][v2][a2][v3][a3]concat=n=3:v=1:a=1" \
-preset superfast -profile:v baseline $OUTFILE
}
if [[ "$*" == "--help" || "$*" == "-h" || $# -eq 0 ]]
then
display_usage
exit 0
elif [ $# -le 3 ]
then
echo -e "\nERROR: not enough arguments"
display_usage
exit 1
elif [ $# -eq 3 ]
then
all_speed $1 $2 $3
exit 0
elif [ $# -gt 3 ]
then
part_speed $1 $2 $3 $4 $5
exit 0
fi
@7aman
Copy link
Author

7aman commented Jul 12, 2019

wget -q https://gist.githubusercontent.com/7aman/d3b6931a8884614bcb843003d3138113/raw/97bd39df2ef237a550cb991aeaa027720031f705/ffspeed.sh
chmod u+x ffspeed.sh
# Usage
./ffspeed.sh input.mp4 output.mp4 3
# or
./ffspeed.sh input.mp4 output.mp4 3 120 240

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment