Keep ffmpeg render as constant speed ( 3x )

Viewed 5879

I'm trying to render a video. But I want render it as constant speed. Example :

ffmpeg -stream_loop -1 -i 113856_video_0.ts -c:v copy -c:a aac -bufsize 3600k -maxrate 1800k -rtbufsize 15M -metadata comment="screen113856" -preset ultrafast -g 50 -r 4 -sc_threshold 0 -crf 0 -threads 1 out.mp4

This ffmpeg will render video fine, out put like this

frame=77590 fps=113 q=-1.0 size= 596650kB time=00:51:43.67 bitrate=1574.8kbits/s speed=10.51x

You can see speed=10.51x , It's fast but but it take all of my CPU. If I use ffmpeg -re , it render as speed=1x , good for CPU but take time. Can I render video as specific speed ?

There is an option to use cpulimit

cpulimit -l 20 ffmpeg ...

Almost solves my case but : - some time it doesn't works, ffmpeg still take 100% CPU - for each video, I have to set the number CPU limit, (-l 20, 30) to get the specific speed. I want to do it automatically.

1 Answers

Here's a crude hack based on filters to emulate this behaviour. For it to be used, at least one audio/video stream must undergo encoding.

In your command, since only audio is being encoded, we'll co-opt that stream. If you are going to integrate this hack into an existing filtergraph with multiple outputs, insert it at the end of a filterchain likely to process its output the quickest, so as to reduce buffering.

ffmpeg -stream_loop -1 -i 113856_video_0.ts \
-af asetpts=PTS/3,arealtime,asetpts=PTS*3 
-c:v copy -c:a aac -metadata comment="screen113856" -threads 1 out.mp4

FFmpeg provides a set of filters: realtime (for video), and arealtime (for audio) that limit the passthrough of frames to further filters at real time. In practice, what this means is that frames with timestamps 1 second apart are conveyed 1 second apart or more (if prior filtering takes more time). So the hack is to collapse the timestamps to the speed you want and then restore them afterwards. Since you wanted 3x, timestamps are first reduced to 1/3rd and then restored after the filter.

This can only slow down processing, not speed it up.

Related