Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: November 13, 2024
Changing the video framerate is common in editing and production, especially when optimizing videos for different platforms or devices. FFmpeg is an open-source multimedia framework that enables us to record, convert, and stream audio and video files across various formats and codecs. It’s well-suited for this task, offering flexibility for lossless framerate adjustments.
In this tutorial, we’ll learn more about framerate, why changing it might be necessary, and how to do it using FFmpeg while preserving the video quality.
Before diving into the technical aspects, it’s essential to understand what the framerate designates.
The framerate, usually measured in frames per second (fps), indicates how many individual frames are contained in one second of a given video. Common framerates include 24 fps for films, 30 fps for television broadcasts, and 60 fps for high-definition content like gaming and sports. Each framerate serves a different purpose and can significantly affect the fluidity and feel of the video.
When changing the video framerate, we may want to maintain the original quality while ensuring smooth playback. This is where lossless processing comes into play. Lossless framerate conversion means the video quality remains unchanged, avoiding artifacts or degradation.
The choice of framerate impacts the visual style and fluidity of the video. Playing a video designed for 24 fps at 60 fps may cause it to appear less smooth or choppy.
Before we continue, let’s understand the benefits of FFmpeg and install it.
FFmpeg is one of the most advanced tools for video processing due to its extensive capabilities and flexibility. It supports several formats and codecs, making it suitable for various multimedia tasks. FFmpeg also provides a command-line interface that can handle almost any video manipulation task.
Moreover, it’s open-source, which adds to its appeal. As a result, FFmpeg has a large community of users and contributors, ensuring that it remains up-to-date with the latest development technologies and standards.
So, let’s install FFmpeg via the local package manager on a system that doesn’t yet have it.
For Ubuntu and Debian-based distros, we can install FFmpeg using APT:
$ sudo apt-get install ffmpeg
For Fedora, we can use DNF:
$ sudo dnf install ffmpeg
Finally, for Arch Linux and its derivatives, we can employ Pacman:
$ sudo pacman -S ffmpeg
Now, let’s verify the command works as expected.
We can verify the installation by checking the version:
$ ffmpeg -version
We should see the version information displayed in the terminal.
Before we explore the methods, it’s important to note that changing the framerate can also affect audio synchronization. When the framerate is altered, the timing of the audio may not align perfectly with the video, which can lead to a noticeable lag or desynchronization. Therefore, it’s crucial to be mindful of this aspect and test the output to ensure that audio and video remain in sync.
In FFmpeg, we can adjust the framerate using several methods depending on the needs:
We’ll cover each method with examples and discuss when each approach is best used.
To change the framerate of a video, we can use the -r option to specify the target fps. However, if we want to change the framerate without quality loss, we should use the -r option cautiously.
Let’s look at how to convert a video from its current framerate to 30 fps:
$ ffmpeg -i input_video.mp4 -r 30 output_video.mp4
In this command, the -i option specifies the input video while the -r option sets the new framerate to 30 fps. Thus, output_video.mp4 represents the output file with an updated framerate.
By default, FFmpeg re-encodes the video to apply the new framerate. However, this may not always be ideal for quality preservation. We can avoid unnecessary re-encoding by using the -c copy option to copy the video and audio streams.
Changing the framerate can be problematic if the new fps value doesn’t align with the video’s keyframes. Keyframes are those frames that store the full image information. A mismatch can cause quality loss, particularly in complex videos.
In FFmpeg, we can ensure a smooth transition without altering keyframes by using the -vf (video filter) option with the fps filter:
$ ffmpeg -i input_video.mp4 -vf "fps=30" output_video.mp4
This command applies the fps filter to output the video at 30 fps without directly altering the keyframe structure. It helps in preventing quality loss.
The -c copy option is valuable to avoid re-encoding and keep the video as close to the original as possible. When we use the -c copy option, FFmpeg doesn’t modify the existing frames or the codec used in the video, making it a useful choice for lossless framerate change.
However, we can only use -c copy if the framerate change is compatible with the video format and codec:
$ ffmpeg -i input_video.mp4 -r 30 -c copy output_video.mp4
With -c copy, FFmpeg copies the stream directly without altering the video data. However, this only applies when the framerate change is compatible with the codec.
This method is ideal for changing playback fps in a way that video players recognize. It enables us to make adjustments without re-encoding or quality degradation.
For example, if we have a video at 30 fps, but want to convert it to 24 fps for a cinematic look:
$ ffmpeg -i input_video.mp4 -r 24 -c copy output_video.mp4
This command maintains the video encoding but changes the playback fps. While it doesn’t alter the original video data, some players may not handle playback changes well.
Converting a high-framerate video to a lower framerate is common for smoother playback across devices. We can use the -vf option to ensure keyframes align correctly:
$ ffmpeg -i input_video.mp4 -vf "fps=30" output_video.mp4
This command avoids quality loss by resampling frames while preserving the original encoding structure.
Finally, some videos use a variable framerate (VFR) for flexibility in streaming, yet, a constant framerate (CFR) may be preferable for editing. We can use the -vsync option to convert VFR to CFR:
$ ffmpeg -i input.mp4 -r 30 -vsync cfr output.mp4
This command applies a constant framerate without re-encoding the video stream.
In this article, we explored how to adjust video framerate losslessly using FFmpeg. We covered essential methods, including using the -r option for direct framerate changes, the fps filter for smooth transitions without keyframe loss, and the -c copy option to avoid re-encoding when possible.
Each approach enables us to modify the framerate based on compatibility, quality needs, and playback requirements.