2

I'm using ffmpeg from a debian machine on my local network. This machine is listening from incoming streams:

$ ffmpeg -i udp://192.168.1.3:8080 /path/to/out.avi

And from my macbook, sending the video in avi format:

$ ffmpeg -f <input> -i "1" -f avi udp://192.168.1.3:8080

The above commands work, but I'm hoping some ffmpeg guru's might be able to offer some optimization tips and tricks for minimizing the CPU load on my macbook while maintaining the video output quality.

click to see image

Any optimization arguments like bitrate (I don't really know what this is), file format alternatives (something better than avi?), resolution, protocol (udp alternatives?) -- anything at all that might keep the CPU usage to an absolute minimum without completely ruining the quality of the video output.

For a clear cut question:

How can I minimize the CPU/MEM usage on the macbook without destroying the video output quality?

Here I tried using the -threads argument with -f mpegts format but CPU usage was still at around 45%.

$ cat ffmpeg-20180529-103103.log

ffmpeg started on 2018-05-29 at 10:31:03
Report written to "ffmpeg-20180529-103103.log"
Command line:
./ffmpeg -threads 1 -f avfoundation -i 1 -f mpegts -report udp://192.168.1.33:8080
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
  built with Apple LLVM version 9.1.0 (clang-902.0.39.1)
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-appkit --enable-avfoundation --enable-coreimage --enable-audiotoolbox
  libavutil      56. 14.100 / 56. 14.100
  libavcodec     58. 18.100 / 58. 18.100
  libavformat    58. 12.100 / 58. 12.100
  libavdevice    58.  3.100 / 58.  3.100
  libavfilter     7. 16.100 /  7. 16.100
  libswscale      5.  1.100 /  5.  1.100
  libswresample   3.  1.100 /  3.  1.100
  libpostproc    55.  1.100 / 55.  1.100
Splitting the commandline.
Reading option '-threads' ... matched as AVOption 'threads' with argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'avfoundation'.
Reading option '-i' ... matched as input url with argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'mpegts'.
Reading option '-report' ... matched as option 'report' (generate a report) with argument '1'.
Reading option 'udp://192.168.1.33:8080' ... matched as output url.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option report (generate a report) with argument 1.
Successfully parsed a group of options.
Parsing a group of options: input url 1.
Applying option f (force format) with argument avfoundation.
Successfully parsed a group of options.
Opening an input file: 1.
[avfoundation @ 0x7f9f56800000] '<AVCaptureScreenInput: 0x7f9f55532240>' opened
[avfoundation @ 0x7f9f56800000] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7f9f56800000] Supported pixel formats:
[avfoundation @ 0x7f9f56800000]   uyvy422
[avfoundation @ 0x7f9f56800000]   yuyv422
[avfoundation @ 0x7f9f56800000]   nv12
[avfoundation @ 0x7f9f56800000]   0rgb
[avfoundation @ 0x7f9f56800000]   bgr0
[avfoundation @ 0x7f9f56800000] Overriding selected pixel format to use uyvy422 instead.
[avfoundation @ 0x7f9f56800000] Probe buffer size limit of 5000000 bytes reached
[avfoundation @ 0x7f9f56800000] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, avfoundation, from '1':
  Duration: N/A, start: 11870.909500, bitrate: N/A
    Stream #0:0, 2, 1/1000000: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1440x900, 1000k tbr, 1000k tbn, 1000k tbc
Successfully opened the file.
Parsing a group of options: output url udp://192.168.1.33:8080.
Applying option f (force format) with argument mpegts.
Successfully parsed a group of options.
Opening an output file: udp://192.168.1.33:8080.
[udp @ 0x7f9f55551c80] No default whitelist set
Successfully opened the file.
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg2video (native))
Press [q] to stop, [?] for help
cur_dts is invalid (this is harmless if it occurs once at the start per stream)
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
detected 4 logical cores
[graph 0 input from stream 0:0 @ 0x7f9f55551d00] Setting 'video_size' to value '1440x900'
[graph 0 input from stream 0:0 @ 0x7f9f55551d00] Setting 'pix_fmt' to value '15'
[graph 0 input from stream 0:0 @ 0x7f9f55551d00] Setting 'time_base' to value '1/1000000'
[graph 0 input from stream 0:0 @ 0x7f9f55551d00] Setting 'pixel_aspect' to value '0/1'
[graph 0 input from stream 0:0 @ 0x7f9f55551d00] Setting 'sws_param' to value 'flags=2'
[graph 0 input from stream 0:0 @ 0x7f9f55551d00] Setting 'frame_rate' to value '1000000/1'
[graph 0 input from stream 0:0 @ 0x7f9f55551d00] w:1440 h:900 pixfmt:uyvy422 tb:1/1000000 fr:1000000/1 sar:0/1 sws_param:flags=2
[format @ 0x7f9f555522c0] Setting 'pix_fmts' to value 'yuv420p|yuv422p'
[auto_scaler_0 @ 0x7f9f55552a80] Setting 'flags' to value 'bicubic'
[auto_scaler_0 @ 0x7f9f55552a80] w:iw h:ih flags:'bicubic' interl:0
[format @ 0x7f9f555522c0] auto-inserting filter 'auto_scaler_0' between the filter 'Parsed_null_0' and the filter 'format'
[AVFilterGraph @ 0x7f9f55729240] query_formats: 4 queried, 2 merged, 1 already done, 0 delayed
[auto_scaler_0 @ 0x7f9f55552a80] picking yuv422p out of 2 ref:uyvy422 alpha:0
[auto_scaler_0 @ 0x7f9f55552a80] w:1440 h:900 fmt:uyvy422 sar:0/1 -> w:1440 h:900 fmt:yuv422p sar:0/1 flags:0x4
[mpeg2video @ 0x7f9f56071a00] intra_quant_bias = 96 inter_quant_bias = 0
[mpegts @ 0x7f9f5606f600] muxrate VBR, pcr every 24 pkts, sdt every 200, pat/pmt every 40 pkts
Output #0, mpegts, to 'udp://192.168.1.33:8080':
  Metadata:
    encoder         : Lavf58.12.100
    Stream #0:0, 0, 1/90000: Video: mpeg2video (4:2:2), yuv422p, 1440x900, q=2-31, 200 kb/s, 240 fps, 90k tbn, 240 tbc
    Metadata:
      encoder         : Lavc58.18.100 mpeg2video
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
Clipping frame in rate conversion by 0.000008
cur_dts is invalid (this is harmless if it occurs once at the start per stream)
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918trate=8818.0kbits/s speed=1.01x    
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918trate=5789.9kbits/s speed=0.996x    
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918

---[ a lot of these rawvideo lines; omitted for superuser ]---

[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918trate=3320.8kbits/s speed=0.999x    
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7f9f5606f000] PACKET SIZE: 2626592, STRIDE: 2918
frame=  744 fps= 30 q=31.0 Lsize=    9909kB time=00:00:24.73 bitrate=3282.0kbits/s speed=0.999x    
video:9113kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 8.732401%
Input file #0 (1):
  Input stream #0:0 (video): 744 packets read (1954184448 bytes); 744 frames decoded; 
  Total: 744 packets (1954184448 bytes) demuxed
Output file #0 (udp://192.168.1.33:8080):
  Output stream #0:0 (video): 744 frames encoded; 744 packets muxed (9332015 bytes); 
  Total: 744 packets (9332015 bytes) muxed
744 frames successfully decoded, 0 decoding errors
[AVIOContext @ 0x7f9f5543ef80] Statistics: 0 seeks, 7244 writeouts

Using the -f avi format again, and removed the -threads arg, still CPU usage was unaffected at 45%.

$ cat ffmpeg-20180529-105147.log

ffmpeg started on 2018-05-29 at 10:51:47
Report written to "ffmpeg-20180529-105147.log"
Command line:
./ffmpeg -f avfoundation -i 1 -f avi -report udp://192.168.1.33:8080
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
  built with Apple LLVM version 9.1.0 (clang-902.0.39.1)
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-appkit --enable-avfoundation --enable-coreimage --enable-audiotoolbox
  libavutil      56. 14.100 / 56. 14.100
  libavcodec     58. 18.100 / 58. 18.100
  libavformat    58. 12.100 / 58. 12.100
  libavdevice    58.  3.100 / 58.  3.100
  libavfilter     7. 16.100 /  7. 16.100
  libswscale      5.  1.100 /  5.  1.100
  libswresample   3.  1.100 /  3.  1.100
  libpostproc    55.  1.100 / 55.  1.100
Splitting the commandline.
Reading option '-f' ... matched as option 'f' (force format) with argument 'avfoundation'.
Reading option '-i' ... matched as input url with argument '1'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'avi'.
Reading option '-report' ... matched as option 'report' (generate a report) with argument '1'.
Reading option 'udp://192.168.1.33:8080' ... matched as output url.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option report (generate a report) with argument 1.
Successfully parsed a group of options.
Parsing a group of options: input url 1.
Applying option f (force format) with argument avfoundation.
Successfully parsed a group of options.
Opening an input file: 1.
[avfoundation @ 0x7fd46a80fe00] '<AVCaptureScreenInput: 0x7fd46a52e640>' opened
[avfoundation @ 0x7fd46a80fe00] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7fd46a80fe00] Supported pixel formats:
[avfoundation @ 0x7fd46a80fe00]   uyvy422
[avfoundation @ 0x7fd46a80fe00]   yuyv422
[avfoundation @ 0x7fd46a80fe00]   nv12
[avfoundation @ 0x7fd46a80fe00]   0rgb
[avfoundation @ 0x7fd46a80fe00]   bgr0
[avfoundation @ 0x7fd46a80fe00] Overriding selected pixel format to use uyvy422 instead.
[avfoundation @ 0x7fd46a80fe00] Probe buffer size limit of 5000000 bytes reached
[avfoundation @ 0x7fd46a80fe00] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, avfoundation, from '1':
  Duration: N/A, start: 13115.322833, bitrate: N/A
    Stream #0:0, 2, 1/1000000: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1440x900, 1000k tbr, 1000k tbn, 1000k tbc
Successfully opened the file.
Parsing a group of options: output url udp://192.168.1.33:8080.
Applying option f (force format) with argument avi.
Successfully parsed a group of options.
Opening an output file: udp://192.168.1.33:8080.
[udp @ 0x7fd46a4319c0] No default whitelist set
Successfully opened the file.
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg4 (native))
Press [q] to stop, [?] for help
cur_dts is invalid (this is harmless if it occurs once at the start per stream)
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
detected 4 logical cores
[graph 0 input from stream 0:0 @ 0x7fd46a463080] Setting 'video_size' to value '1440x900'
[graph 0 input from stream 0:0 @ 0x7fd46a463080] Setting 'pix_fmt' to value '15'
[graph 0 input from stream 0:0 @ 0x7fd46a463080] Setting 'time_base' to value '1/1000000'
[graph 0 input from stream 0:0 @ 0x7fd46a463080] Setting 'pixel_aspect' to value '0/1'
[graph 0 input from stream 0:0 @ 0x7fd46a463080] Setting 'sws_param' to value 'flags=2'
[graph 0 input from stream 0:0 @ 0x7fd46a463080] Setting 'frame_rate' to value '1000000/1'
[graph 0 input from stream 0:0 @ 0x7fd46a463080] w:1440 h:900 pixfmt:uyvy422 tb:1/1000000 fr:1000000/1 sar:0/1 sws_param:flags=2
[format @ 0x7fd46a4645c0] Setting 'pix_fmts' to value 'yuv420p'
[auto_scaler_0 @ 0x7fd46a464c00] Setting 'flags' to value 'bicubic'
[auto_scaler_0 @ 0x7fd46a464c00] w:iw h:ih flags:'bicubic' interl:0
[format @ 0x7fd46a4645c0] auto-inserting filter 'auto_scaler_0' between the filter 'Parsed_null_0' and the filter 'format'
[AVFilterGraph @ 0x7fd46a42f2c0] query_formats: 4 queried, 2 merged, 1 already done, 0 delayed
[auto_scaler_0 @ 0x7fd46a464c00] w:1440 h:900 fmt:uyvy422 sar:0/1 -> w:1440 h:900 fmt:yuv420p sar:0/1 flags:0x4
[avi @ 0x7fd46b81aa00] Frame rate very high for a muxer not efficiently supporting it.
Please consider specifying a lower framerate, a different muxer or -vsync 2
[mpeg4 @ 0x7fd46b977200] intra_quant_bias = 0 inter_quant_bias = -64
[avi @ 0x7fd46b81aa00] reserve_index_space:0 master_index_max_size:256
[avi @ 0x7fd46b81aa00] duration_est:36000.000, filesize_est:0.9GiB, master_index_max_size:256
Output #0, avi, to 'udp://192.168.1.33:8080':
  Metadata:
    ISFT            : Lavf58.12.100
    Stream #0:0, 0, 1/600: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 1440x900, q=2-31, 200 kb/s, 65535 fps, 600 tbn, 65535 tbc
    Metadata:
      encoder         : Lavc58.18.100 mpeg4
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
Clipping frame in rate conversion by 0.000008
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918trate=6100.5kbits/s speed=1.16x    
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918

---[ a lot of these rawvideo lines; omitted for superuser ]---

[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918trate=2806.4kbits/s speed=1.01x    
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
[rawvideo @ 0x7fd46a840800] PACKET SIZE: 2626592, STRIDE: 2918
frame=  340 fps= 30 q=31.0 Lsize=    3879kB time=00:00:11.33 bitrate=2803.6kbits/s speed=   1x    
video:3825kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.425499%
Input file #0 (1):
  Input stream #0:0 (video): 340 packets read (893041280 bytes); 340 frames decoded; 
  Total: 340 packets (893041280 bytes) demuxed
Output file #0 (udp://192.168.1.33:8080):
  Output stream #0:0 (video): 340 frames encoded; 340 packets muxed (3916524 bytes); 
  Total: 340 packets (3916524 bytes) muxed
340 frames successfully decoded, 0 decoding errors
[AVIOContext @ 0x7fd46a56ffc0] Statistics: 0 seeks, 2923 writeouts

Using -s 1024x576 seems to help bring down the CPU usage but the text in the video output starts to become almost unreadable.

5
  • Run the Mac command for a short while with -report added and share the logfile. Commented May 29, 2018 at 11:58
  • @Gyan Hey, I've updated the question to include the -report logs Commented May 29, 2018 at 18:01
  • Go with ./ffmpeg -f avfoundation -i 1 -pix_fmt yuv420p -c:v libx264 -preset faster -threads 1 -f mpegts udp://192.168.1.33:8080 Commented May 29, 2018 at 18:08
  • @Gyan Thank you, this helped bring the CPU usage down 5%-15%. Would you be able to provide a brief explanation of what the following args mean: -pix_fmt yuv420p -c:v libx264 -preset faster -f mpegts . If you put the explanation in an answer, I'll accept it. Commented May 29, 2018 at 18:46
  • I'll add it tomorrow. Commented May 29, 2018 at 19:23

1 Answer 1

0

Try

ffmpeg -f avfoundation -i 1 -pix_fmt yuv420p -c:v libx264 -preset faster -threads 1 -f mpegts udp://192.168.1.33:8080

-f mpegts sets the output file format. mpegts accepts many video codecs and is the de facto format for transmission of digital broadcasts.

When outputting to mpegts, ffmpeg will select its native MPEG-2 encoder. The MPEG-2 video codec is 20+ years old. Standard codec nowadays is H264. Standard encoder within ffmpeg for that is libx264. That needs to be explicitly set here, so -c:v libx264. It can be skipped if the output format has x264 as its default encoder. preset faster is a mode of x264 which dials down some of the time-consuming and resource-expensive methods used while compressing.

-pix_fmt yuv420p sets the output pixel format. This is the only pixel format supported by web players and many hardware/software players as well.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.