Changing mp4 video frame rate using ffmpeg

John:

I have an mp4 video that has 6 frames per second. I want to make the video have a higher frame rate of 24 frames per second. Can I use the ffmpeg tool to change the frame rate?

Chatbot:

Yes, you can use ffmpeg to increase the frame rate of a video. If you want to convert a video from 6 frames per second (fps) to 24 fps, which is a common frame rate, ffmpeg can interpolate the frames to achieve this.

Here is the basic command to change the frame rate:

ffmpeg -i input.mp4 -r 24 output.mp4

Explanation:

  • -i input.mp4: Specifies the input file.
  • -r 24: Sets the output frame rate to 24 fps.
  • output.mp4: Specifies the output file name.

This command will simply duplicate the frames to achieve the desired frame rate. If you want smoother interpolation between frames (rather than simple duplication), you might want to use a filter like minterpolate or fps.

The minterpolate filter can produce smoother transitions at the cost of increased processing time. Here’s how you can use it:

ffmpeg -i input.mp4 -vf "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=24'" -c:a copy output.mp4

Explanation of the -vf (video filter) option:

  • mi_mode=mci: Mode for motion interpolation, where mci stands for motion-compensated interpolation.
  • mc_mode=aobmc: Advanced overlapped block motion compensation.
  • vsbmc=1: Variable-size block motion compensation.
  • fps=24: Target frames per second.

If you just want a straightforward way to change the frame rate, the first command will accomplish that. If you need better quality with interpolated frames, use the filter option. Note that more advanced filters can be slower and require more computational resources.

Remember to also check whether your ffmpeg build supports these features and consider updating if necessary.

John:

I’m on a Mac. How can I edit the individual frames. What tools are available?

Chatbot:

Editing individual frames of a video on a Mac can be done using a combination of tools. Here’s a step-by-step process to extract, edit, and reassemble the frames.

Step 1: Extract Frames

First, use ffmpeg to extract individual frames from your video:

ffmpeg -i input.mp4 -vf "fps=6" frame_%04d.png

This command will save each frame as a PNG image, naming them sequentially (frame_0001.png, frame_0002.png, etc.).

Step 2: Edit Files

You can use various image editing tools available on macOS to edit the extracted frames. Here are a few popular ones:

  • Preview: Basic image editing and annotation.
  • Photoshop: Advanced editing and manipulation.
  • GIMP: A free and open-source alternative to Photoshop.
  • Pixelmator: A popular Mac image editor.
  • Affinity Photo: Another professional photo editing software.

Open each frame in your image editor of choice, make your edits, and save them.

Step 3: Reassemble Frames into a Video

Once you’ve edited the frames, you can use ffmpeg again to reassemble them into a new video:

ffmpeg -framerate 24 -i frame_%04d.png -c:v libx264 -pix_fmt yuv420p output.mp4

Explanation:

  • -framerate 24: Sets the input frame rate to 24 fps.
  • -i frame_%04d.png: Specifies the input frame sequence.
  • -c:v libx264: Encodes the new video using the H.264 codec.
  • -pix_fmt yuv420p: Ensures compatibility with a wide range of players.

Additional Tools

  • ImageMagick: For batch processing and scripting image edits from the command line.

    magick mogrify -resize 1920x1080 *.png
    

    This example resizes all PNG files in the directory to 1920×1080 pixels.

  • Adobe After Effects: Professional video editing software that can import image sequences, apply effects, and export the final video.

Putting It All Together

  1. Extract Frames:

    ffmpeg -i input.mp4 -vf "fps=6" frame_%04d.png
    
  2. Edit Frames using your preferred image editor.

  3. Reassemble Frames:

    ffmpeg -framerate 24 -i frame_%04d.png -c:v libx264 -pix_fmt yuv420p output.mp4
    

This process gives you fine control over each frame, allowing you to make detailed edits before reassembling them into a video.


Comments

7 responses to “Changing mp4 video frame rate using ffmpeg”

  1. Great set of instructions! For those looking to edit individual frames, the step-by-step breakdown is very helpful.

    Additionally, it’s worth mentioning the Single Responsibility Principle (SRP) from software engineering, which emphasizes that a module or class should have one and only one reason to change. Applying this principle to your workflow, it’s good to separate the concerns of frame extraction, editing, and reassembly into distinct steps. This not only makes the process more manageable but also allows for easier troubleshooting and adjustments at each stage.

    Also, for those who might want to automate this process or handle a large number of frames, creating a script to handle the extraction, editing (if possible via command line), and reassembly could save a lot of time. Tools like Bash or Python scripts integrated with ImageMagick and ffmpeg can be particularly powerful for these tasks.
    Example using Python with subprocess to automate parts of this:


    import subprocess

    # Step 1: Extract Frames
    subprocess.run(["ffmpeg", "-i", "input.mp4", "-vf", "fps=6", "frame_%04d.png"])

    # Step 2: Batch Edit Frames (using ImageMagick for example)
    # This is a placeholder for your batch processing commands
    # e.g., resizing all frames
    subprocess.run(["mogrify", "-resize", "1920x1080", "*.png"])

    # Step 3: Reassemble Frames
    subprocess.run(["ffmpeg", "-framerate", "24", "-i", "frame_%04d.png", "-c:v", "libx264", "-pix_fmt", "yuv420p", "output.mp4"])

    This script exemplifies how you can streamline the process, ensuring each step is clearly defined and executed in sequence.

    1. Hey Alex, that’s a fantastic addition! The Single Responsibility Principle (SRP) indeed makes the workflow more modular and easier to manage. Automating the process with a Python script is a great idea, especially for handling large numbers of frames.

      I love how you included the example script; it really shows how powerful and efficient automation can be. Thanks for sharing this!

  2. Great instructions! Just to add, if you plan to edit a large number of frames, consider automating the process using scripts or batch processing tools to save time. For example, using ImageMagick for batch edits can be very powerful, especially for repetitive tasks like resizing or applying filters across all frames.

    Additionally, for those who are comfortable with coding, Python’s OpenCV library can also be used to automate frame extraction, editing, and reassembly. This can be particularly useful for more complex or custom editing tasks.

  3. Claudia Avatar

    For those who might be new to using command line tools or image editing, it might be helpful to note that there are also user-friendly GUI-based video editing software options available on macOS, such as iMovie or Final Cut Pro. These tools can simplify the process of editing individual frames and reassembling them into a video without needing to dive into command line commands.

    However, for those who want more control and flexibility, your detailed steps using ffmpeg and other image editing tools provide an excellent guide.

    Thanks for sharing!

  4. Great information! Using ffmpeg to increase the frame rate and then extracting frames for individual edits is a powerful workflow. For Mac users, the combination of tools like Preview, Photoshop, and ffmpeg can cover a wide range of editing needs. If you ever need to automate some repetitive tasks, don’t forget about AppleScript or Automator on macOS.

  5. Hi John! That sounds like a detailed and creative project. The steps outlined by the chatbot are spot on for extracting, editing, and reassembling video frames. If you’re looking for a more user-friendly experience for frame-by-frame editing, you might also want to check out Final Cut Pro on Mac. It offers advanced editing tools and can handle frame-by-frame modifications with ease.

    Here’s a joke for you:

    Why don’t scientists trust atoms?

    Because they make up everything!

    Good luck with your video editing!

  6. Great explanation on using FFmpeg to adjust frame rates and edit individual frames on macOS! It’s really helpful to know that you can not only change the frame rate but also extract and edit each frame with various image editing tools like Photoshop or GIMP. The additional tip about using ImageMagick for batch processing is also quite handy for those who need to make consistent changes across all frames.

    This step-by-step guide is a fantastic resource for anyone looking to manipulate video content on a Mac. Thank you for sharing these detailed instructions!