Search code examples
videoffmpegframeframe-raterate

How can I change a video frame rate with FFmpeg, lossless and keeping the same total number of frames?


I've been searching for an answer here on Stack Overflow and googling everywhere... even though it seems like it should be a very simple command line to me, I just can't find an answer anywhere.

I would like to change the frame rate of a video from 23.976fps to 24fps with FFmpeg, lossless and keeping the total number of frames.

To make it simpler:

Let's say I have a 25fps video with a total lenght of 100 frames.

How can I change it's frame rate to 50fps, with FFmpeg, lossless and keeping the same total lenght of 100 frames?

This was so far the best solution I came across with (which can be found here):

Extract the frames as rawvideo:

ffmpeg -i input.mov -f rawvideo -b 50000000 -pix_fmt yuv420p -vcodec rawvideo -s 1920x1080 -y temp.raw

Recreate the video with new framerate:

ffmpeg -f rawvideo -b 50000000 -pix_fmt yuv420p -r 24 -s 1920x1080 -i temp.raw -y output.mov

Note 1: I had to remove "-b 50000000" when recreating the video with the new frame rate, in order to get it to work properly.

It did exactly what I intended it to do, but I'm still wondering if there is any simpler way to do this? I've tried to pipe them together in one line only, as suggested in the same post, but couldn't get it to work.

Note 2: Even though it does exactly what I wanted it to do, I've just later realized there is quality loss using this method, which I would prefer to avoid.

Thanks everyone in advance!


Solution

  • If your video codec is H.264/5, then this two-step method works, and is lossless.

    #1 Demux to raw bitstream

    ffmpeg -i in.mov -c copy in.264
    

    #2 Remux with new framerate

    ffmpeg -r 24 -i in.264 -c copy out.mov
    

    For other codecs, if there's a raw bitstream format available, then it can be done without transcoding.

    If transcoding is fine, the following single step works:

    ffmpeg -r 24 -i in.mov -vsync 0 -c:v <codecname> out.mov 
    

    Of course, you'll want to specify parameters like bitrate..etc to control quality.