In this guide, we will see how to Modify video and audio speed using FFmpeg filters.
To change the speed of the video file, we can use the setpts (set presentation timestamp) filter.
Filter Description: Changes presentation timestamp (PTS) of input frames.
List of available variables which we can use in expression:
|FRAME_RATE||frame rate, only defined for a video with a constant frame|
|INTERLACED||tell is the current frame is interlaced|
|N||count of the input frame, starting from 0|
|NB_CONSUMED_SAMPLES||number of consumed samples, without the current frame (only audio)|
|NB_SAMPLES||number of samples in the current frame (only audio)|
|POS||original frame position in the file, or undefined if undefined for the current frame|
|PREV_INT||previous input time in seconds|
|PREV_INPTS||previous input PTS|
|PREV_OUTPTS||previous output PTS|
|PREV_OUTT||previous output time in seconds|
|PTS||presentation timestamp in input|
|SAMPLE_RATE||audio sample rate|
|STARTPTS||PTS of the first frame|
|STARTT||time in seconds of the first frame|
|T||time in seconds of the current frame|
Each video frame contains a header with a timestamp value, the difference between 2 frames in sequence is 1/fps, for instance if fps is 25, the difference is 0.04 second. to speed up the video this time difference must be smaller and for a lower speed it must be bigger. For example, to watch the video 3-times faster, the input timestamp is divided by 3 and the command is:
ffplay -i input.mpg -vf setpts=PTS/3
To watch the video in a 3/4 speed, the input timestamp is divided by 3/4 and we can use the command:
ffplay -i input.mpg -vf setpts=PTS/(3/4)
To change the tempo of the audio, we can use the special atempo filter.
Filter Description: Changes audio tempo – the speed of the audio stream.
Description of parameter:
tempo: float number from the range 0.5 – 2.0, values less than 1.0 slows down and values over 1.0 speed up the tempo, the default value is 1.0
for example to hear the input audio with 2-times faster speed, we can use the command:
ffplay -i film.mp3 -af atempo=2
To hear this audio in a half tempo, we can use the atempo=0.5 setting and if the speed change is not sufficient, the filter can be applied more times.
ffplay -i film.mp3 -af atempo=0.5
Slow down audio to 90% tempo:
ffplay -i film.mp3 -af atempo=0.9
To speed up audio to 140% tempo:
ffplay -i film.mp3 -af atempo=1.40