Hello,
I have a video stream that I’m compressing and writing to a video file. Is there a way to get the data post-compression but prior to writing the video file? Or is ffmpeg compressing and then writing without any opencv hooks in between?
Hello,
I have a video stream that I’m compressing and writing to a video file. Is there a way to get the data post-compression but prior to writing the video file? Or is ffmpeg compressing and then writing without any opencv hooks in between?
As far as I know, cv::VideoWriter doesn’t expose the encoded packets: it hands raw frames to the backend (FFmpeg, GStreamer, etc.) which encodes and muxes them internally, so there is no hook between the two steps. If you need the compressed data, the usual options are to encode yourself with FFmpeg/libav (or PyAV) and write the packets wherever you like, or to use a GStreamer pipeline with an appsink after the encoder element and open it via VideoWriter with CAP_GSTREAMER. Which one fits depends on whether you need H.264 packets or just an MJPEG-style stream.
For a concrete packet boundary, FFmpeg’s encode_video.c example sends an AVFrame with avcodec_send_frame(), then calls avcodec_receive_packet() in a loop until EAGAIN or EOF. Each returned AVPacket exposes data and size before the example writes those bytes and calls av_packet_unref(). At that point, your application can inspect or copy the compressed bytes rather than pass a frame to VideoWriter::write() and let the backend own the output path. The example writes an elementary stream, not a complete muxed container; timestamps and container handling need separate attention.