FFmpeg Video Slow Motion: My Learning Notes and Practical Tips
Hello fellow video enthusiasts!
Have you ever encountered this situation: you want to use FFmpeg to slow down the playback speed of a video, maybe slow it down by half to see the details clearly, or follow along with something? Or, you have a bunch of sequentially shot images and want to combine them into a video, but you want it to play slowly instead of flashing by?
I've been pondering these questions recently and found that FFmpeg has some key points to handle, especially when setting the speed and processing image sequences. I browsed some discussions on Reddit (like these two: How to make a video slow down and Image sequence to video), combined with my own practice, and compiled this note.
This note doesn't talk about fancy "slow-motion effects", but rather realistically discusses how to use FFmpeg to slow down video playback speed. We will also specifically explore a common question: Does the frame rate (FPS) of the video change when using setpts
to slow it down? And how to achieve extending the playback time without changing the (file marked) FPS? Finally, we will also talk about when generating a video from images, how should the somewhat confusing frame rate (-r
) parameter be set correctly? Don't worry, it's all about basic and practical methods, applicable to Windows, Linux, and macOS. I hope it can help those of you with similar needs.
Come on, let's see how to make FFmpeg obediently "slow down"!
Behind the Scenes of Video Slow Motion: How is Time Stretched?
To make a video play in slow motion, the core is to extend the display time of each frame, increase the total duration of the entire video, and thus achieve the purpose of slowing down the playback speed. FFmpeg mainly achieves this by adjusting two things:
- Presentation Timestamp (PTS): Simply put, it tells the player at which point in the video this frame should be displayed.
- Frame Rate (FPS): How many frames are included in the video per second.
When I was looking at everyone's discussions, I found that the main sticking points were:
- How to accurately control the slow-motion multiple of the video?
- Will the FPS of the output video change after using
setpts
to slow down? - If the video is slow, how to handle the audio to keep it synchronized?
- When using images to synthesize a video, what are the key points of the input and output frame rate (
-r
) settings? - Are there any pitfalls when using FFmpeg under different operating systems?
Below, we will solve these problems one by one.
Method 1: Slow Down Existing Video (Basic Operation, Including FPS Explanation)
If you want to slow down the playback speed of an existing video file, the most direct method is to use the setpts
filter (used through the -vf
parameter). For example, to make the video play at half the original speed:
ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" output.mp4
Explanation:
-vf "setpts=2.0*PTS"
is the key. It tells FFmpeg to multiply the original display time point (PTS) of each frame by 2. In this way, the content that could be played in 1 second now takes 2 seconds to play, and the playback speed is naturally reduced to half of the original. It directly acts on the timestamp, stretching the time interval between frames.So, will the FPS change? How to extend the playback time without changing the FPS?
- Good question!
setpts
itself modifies the timestamp, it does not directly modify the FPS metadata recorded in the video stream (such as 30fps). - Usually (default behavior): When you only use
setpts
and do not use-r
to forcibly specify a new frame rate at the output end, FFmpeg will often keep the FPS marking of the output video file consistent with the input video. - The result is: You will get a video file with double the duration, which has the same total number of frames as the original video, and the FPS displayed in the file information is also the same as the original (e.g., still 30fps).
- So, this command
ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" output.mp4
itself has already (in most cases) achieved your goal: extending the playback time while keeping the file marked FPS unchanged. It achieves the slow-motion effect by making each frame display for a longer time, rather than by changing the frame rate standard of the video. - One thing to note: Although the FPS marked in the file has not changed, because the total duration has increased and the total number of frames has not changed, the number of new scenes seen per second is indeed reduced during actual viewing, which may feel less smooth than the original speed (but this is exactly the effect that slow motion wants).
- Good question!
Universality: This line of command is written the same on Windows, Linux, and macOS, which is very convenient.
Method 2: Handle Audio Synchronization (The Sound Must Also Slow Down)
It's usually not enough to just slow down the picture. If the video has sound, the sound must also slow down, otherwise, the sound and picture will be out of sync. At this time, you need to add the atempo
audio filter (used through the -af
parameter):
ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" -af "atempo=0.5" output.mp4
- Explanation:
-af "atempo=0.5"
means to reduce the playback speed of the audio to 0.5 times (i.e., half) of the original. In this way, the sound and picture can be kept consistent.- Additional Explanation: The speed factor of the
atempo
filter must be between 0.5 and 100.0. If you want to be slower than 0.5 times (such as 0.25 times), you need to use it continuously, such as-af "atempo=0.5,atempo=0.5"
.
- Additional Explanation: The speed factor of the
- Universality: Similarly, this command is also universal across platforms.
Method 3: Create Slow Motion Video from Image Sequence (Focus on Understanding -r
!)
This scenario is also very common: you have a series of pictures (such as img1.jpg
, img2.jpg
, ...), and you want to make them into a video, and you want this video to play relatively slowly. Command example:
ffmpeg -r 1 -i img%d.jpg -vf "setpts=2.0*PTS" -r 30 -c:v libx264 -pix_fmt yuv420p output.mp4
The understanding of the two -r
parameters here is the key:
First
-r
(input-r 1
):- Specifies how long each picture represents by default when reading input images.
-r 1
means that each picture occupies 1 second (input basic frame rate 1fps). 10 pictures are 10 seconds of basic material duration. - It sets the time base for the input material.
- Specifies how long each picture represents by default when reading input images.
Second
-r
(output-r 30
):- Specifies what frame rate the final generated video file should be played at.
-r 30
means outputting a 30fps video. - It determines the specifications and smoothness of the output video. FFmpeg will calculate the total number of frames needed based on the total duration adjusted by
setpts
(e.g., 10 pictures, input-r 1
, target duration of 20 seconds aftersetpts=2.0
) and this output frame rate (30fps), which is 20 * 30 = 600 frames. It will repeat the original pictures (becausesetpts
only changes the timestamp) to fill these 600 frames, ensuring that the output video meets the 30fps standard.
- Specifies what frame rate the final generated video file should be played at.
Working together: Input
-r
sets the basic duration ->setpts
adjusts the duration -> Output-r
sets the final specifications and frame number filling method.Cross-platform Notes: Windows recommends using
%d
to match digital sequence file names; Linux/macOS can use*
but%d
is safer.
Method 4: Balance Processing Speed and Output Quality
Video processing is time-consuming, and parameters can be used to weigh the trade-offs:
- Speed up:
-preset ultrafast
/fast
etc. - Control quality:
-crf
value (e.g.,-crf 20
).
Comprehensive example:
ffmpeg -i input.mp4 -vf "setpts=2.0*PTS" -af "atempo=0.5" -c:v libx264 -preset fast -crf 20 -c:a aac output.mp4
- Hardware Acceleration: Consider using
-c:v h264_nvenc
(Nvidia) or-c:v h264_videotoolbox
(Apple) to speed up.
Okay, this time we have deeply explored the method of FFmpeg to achieve video "slow motion" and clarified the relationship between setpts
and FPS. Key points review:
- Existing video slow motion:
- Use
-vf "setpts=N*PTS"
(N>1) to lengthen the video time. - This command usually keeps the FPS marking of the output file consistent with the input, while extending the playback time, which meets the needs of "only slow down, do not change (mark) FPS".
- Don't forget to use
-af "atempo=1/N"
(note the 0.5x speed limit) to handle audio synchronization.
- Use
- Image sequence synthesis slow motion video:
- Input
-r
determines the time value of the image. - Output
-r
sets the playback frame rate of the final video (recommended 25 or 30). setpts
is used to further adjust the overall playback speed.
- Input
- Cross-platform: Core commands are universal, pay attention to the file name pattern.
- Efficiency and quality: Use
-preset
and-crf
to adjust.