How to minimize usage of CPU/memory by ffmpeg when recording video

I use FFmpeg for making video screen capture from Xvfb display.

Currently I invoke it with:

ffmpeg -y -r 15 -g 600 -s 1280x1024x24 -f x11grab -i :100 -vcodec libx264 /tmp/video.mov

As I record video from about 5 Xvfb sessions my CPU usage is very high and there are lags because of it. Also memory usage is about 300 MB for each of ffmpeg processes.

Which parameters for ffmpeg should I use to minimize computer resources usage (particularly CPU and memory) when making video screen capture?

Answer

1. Make a lossless RGB output first

ffmpeg -y -framerate 25 -video_size 1280x1024 -f x11grab -i :0.0 -c:v libx264rgb \
-crf 0 -preset ultrafast temp.mp4
  • The input is RGB, so using the encoder libx264rgb will avoid the potentially slow RGB to YUV conversion that would occur if you use plain libx264.

  • This uses the quickest x264 encoding preset: ultrafast.

  • The output will be lossless because -crf 0 is used.

2. Then re-encode it

The output from the first command will be huge, and most dumb players can’t handle RGB H.264 so you can re-encode it:

ffmpeg -i temp.mp4 -c:v libx264 -crf 23 -preset medium -vf format=yuv420p out.mp4
  • You can experiment with the -crf value to control output quality. A subjectively sane range is 18-28, where 18 is visually lossless or nearly so. Default is 23.

  • Use the slowest preset you have patience for: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow. Default is medium.

  • I added -vf format=yuv420p to ensure the output works with dumb players like QuickTime and Windows Media Player. You can omit this if you’re uploading it to YouTube or only playing it on VLC, mpv, MPlayer, or any other FFmpeg based player.

Also see

Attribution
Source : Link , Question Author : Andrei Botalov , Answer Author : llogan

Leave a Comment