Search code examples
cffmpegpopenlibavformat

Is it faster using libavformat over calling ffmpeg with popen?


I've got a chunk of video processing code in C that seems bottlenecked by the rate at which I can read bytes from the ffmpeg pipe.

I'm needing the decoding to rawvideo frames provided by ffmpeg or potentially libav*.

Is there a great overhead in fread calls to a popened subprocess ffmpeg compared to using the libav libraries?


Solution

  • There's a few aspects here. First of all, why does it seem that your application is bottlenecked by input from the ffmpeg pipe? The answer is probably quite simple: because ffmpeg takes much more CPU (and is thus the bottleneck of the combination of the two applications). If you run a | b, and a takes much more CPU, then from b's perspective, the input from the pipe is slow. That's logical, because a takes more time. There is no solution to this, ffmpeg is supposed to take more CPU, because (depending on what codec/resolution etc.), multimedia processing is a very CPU-intensive task.

    Second of all, would using the C API instead of a pipe help? That depends on what you're doing. If you're using ffmpeg for a fairly trivial task like reading a file from disk without decompression (e.g. uncompressed video/audio), then yes, the overhead of the pipe is fairly substantial. If ffmpeg decompressed H264/HEVC or VP9 video, you're unlikely to see gains from this move, or they'll at the very least be small (1% range), since most time is spent in decoding video. The copy/transfer of data is almost negligible. So in that case, the question is: is 1% worth it? That's up to you to decide.