Using ffmpeg to change framerate

Ffmpeg

Ffmpeg Problem Overview


I am trying to convert a video clip (MP4, yuv420p) from 30 fps to 24 fps. The number of frames is correct so my output should change from 20 minutes at 30fps to 25 minutes at 24fps. Everything else should remain the same.

Try as I might everything I try with ffmpeg converts the frame rate but changes the number of frames to keep the same duration or changes the duration without altering the framerate.

So I have been typically trying things like;

ffmpeg -y -r 30 -i seeing_noaudio.mp4 -r 24 seeing.mp4

(I'm doing this on windows but normally would be on linux). That converts the framerate but drops frames so the total duration is unaltered.

Or I have tried

ffmpeg -y -i seeing_noaudio.mp4 -filter:v "setpts=1.25*PTS" seeing.mp4

Which changes the duration but not the framerate.

Surely I should be able to do this with a single ffmpeg command without having to reencode or even as some people suggested going back to the original raw frames.

Help please

Ffmpeg Solutions


Solution 1 - Ffmpeg

With re-encoding:

ffmpeg -y -i seeing_noaudio.mp4 -vf "setpts=1.25*PTS" -r 24 seeing.mp4

Without re-encoding:

First step - extract video to raw bitstream

ffmpeg -y -i seeing_noaudio.mp4 -c copy -f h264 seeing_noaudio.h264

Remux with new framerate

ffmpeg -y -r 24 -i seeing_noaudio.h264 -c copy seeing.mp4

Solution 2 - Ffmpeg

You may consider using fps filter. It won't change the video playback speed:

ffmpeg -i <input> -filter:v fps=fps=30 <output>

Worked nice for reducing fps from 59.6 to 30.

Solution 3 - Ffmpeg

Simply specify the desired framerate in "-r " option before the input file:

ffmpeg -y -r 24 -i seeing_noaudio.mp4 seeing.mp4

Options affect the next file AFTER them. "-r" before an input file forces to reinterpret its header as if the video was encoded at the given framerate. No recompression is necessary. There was a small utility avifrate.exe to patch avi file headers directly to change the framerate. ffmpeg command above essentially does the same, but has to copy the entire file.

Solution 4 - Ffmpeg

In general, to set a video's FPS to 24, almost always you can do:

With Audio and without re-encoding:

# Extract video stream
ffmpeg -y -i input_video.mp4 -c copy -f h264 output_raw_bitstream.h264
# Extract audio stream
ffmpeg -y -i input_video.mp4 -vn -acodec copy output_audio.aac
# Remux with new FPS 
ffmpeg -y -r 24 -i output_raw_bitstream.h264 -i output_audio.aac -c copy output.mp4

If you want to find the video format (H264 in this case), you can use FFprobe, like this

ffprobe -loglevel error -select_streams v -show_entries stream=codec_name -of default=nw=1:nk=1 input_video.mp4

which will output:

>h264

Read more in https://stackoverflow.com/questions/39869969/how-can-i-analyze-file-and-detect-if-the-file-is-in-h-264-video-format


With re-encoding:

ffmpeg -y -i input_video.mp4 -vf -r 24 output.mp4

Solution 5 - Ffmpeg

To the best of my knowledge you can't do this with ffmpeg without re-encoding. I had a 24fps file I wanted at 25fps to match some other material I was working with. I used the command ffmpeg -i inputfile -r 25 outputfile which worked perfectly with a webm,matroska input and resulted in an h264, matroska output utilizing encoder: Lavc56.60.100

You can accomplish the same thing at 6fps but as you noted the duration will not change (which in most cases is a good thing as otherwise you will lose audio sync). If this doesn't fit your requirements I suggest that you try this answer although my experience has been that it still re-encodes the output file.

For the best frame accuracy you are still better off decoding to raw streams as previously suggested. I use a script for this as reproduced below:

#!/bin/bash
#This script will decompress all files in the current directory, video to huffyuv and audio to PCM
#unsigned 8-bit and place the output #in an avi container to ease frame accurate editing.
for f in *
do
ffmpeg -i "$f" -c:v huffyuv -c:a pcm_u8 "$f".avi
done

Clearly this script expects all files in the current directory to be media files but can easily be changed to restrict processing to a specific extension of your choosing. Be aware that your file size will increase by a rather large factor when you decompress into raw streams.

Solution 6 - Ffmpeg

You can use this command and the video duration is still unaltered.

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

Solution 7 - Ffmpeg

You can try for example (to convert from 25 fps to 24 fps)

ffmpeg -itsscale 1.0416667 -i "your input file" -vcodec copy "output file"

the itsscale value of 1.0416667 is 25/24 as a float variable for ffmpeg (0.1234567 is the float values format - don't use 1.04166666666666666667 or a double value : note that you can't use the expression/formula "25/24" here) This will scale the frame rate times from 25 to 24 fps, keeping the same number of frames, but lengthening the video by 1.0416667 .

going from 23.976 to 24 or going from 29.97 to 30 this value would be 0.999)

If you have audio you would include an audio filter to rescale the audio without a pitch change by changing the tempo using the atempo settings, you should include a compressor and bit rate as well. For subtitles, you just need to include the -codec:s

so we would have

ffmpeg -itsscale 1.0416667 -i "input file" -filter:a atempo="24/25" -codec:a ac3 -b:a 640k -vcodec copy -codec:s copy "output file"

where I used the ac3 audio codec at a bitrate of 640K and the expression "24/25" which is allowed here :NOTE: the 24/25 is the inverse of the itsscale value of 25/24(=1.0416667)

Note: if you have more than 1 stream, (video or audio or subtitle) you may need to include the -map i:v:o (or 0:a:0 or 0:s:0 where 'i' is the input stream number and 'o' is the output stream number

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJ BrandView Question on Stackoverflow
Solution 1 - FfmpegGyanView Answer on Stackoverflow
Solution 2 - FfmpegkelinView Answer on Stackoverflow
Solution 3 - FfmpegMKaamaView Answer on Stackoverflow
Solution 4 - FfmpeggsamarasView Answer on Stackoverflow
Solution 5 - FfmpegElder GeekView Answer on Stackoverflow
Solution 6 - FfmpegTim LeView Answer on Stackoverflow
Solution 7 - FfmpegGeorgView Answer on Stackoverflow