简体   繁体   中英

FFmpeg: how to use C++ code to change the frame rate of a video file?

I know it would be easier to use FFmpeg commands to change the frame rate of a video file. But anyway, if I want to do it in C++ code, and use FFmpeg libraries, how could I do it?

I think I should've be able to find out the clue in the source code. Just before proceeding, I hope there would be some good introductions or examples.

This is fairly easy, all you need is to modify the time_base of the video stream. For simple container formats such as AVI you only need to do it in the header. If you insist to do it via ffmpeg API, you'd need to loop through all the frames in the input stream, and copy them to the output stream.

The above assumes you only want to change the FPS (ie slow down or speed up the video) without dropping frames. However if you want to keep the video playback at the original speed while changing the FPS, you'd need to recode the video, ie decode and encode each frame, while inserting extra frames or removing some frames. You cannot simply remove the frames from the video - for example when converting from 30FPS to 15FPS you cannot simply remove every 2nd frame, since it may be a keyframe and it will break all frames after it until the next keyframe is encountered. Same way, you cannot simply duplicate a frame when upping the FPS, as P frames apply only to the frame before it, so duplicating it would break your video. For this which I'd suggest to look at my Karaoke Lyric Editor source code, notably video decoding and video encoding .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM