user3360601
user3360601

Reputation: 347

How can I replace only one data frame from stream without rewriting whole video file ? (FFmpeg)

I created two streams: for input video file and for output. But even if I want to change only one (first) video frame I have to use av_read_frame and write_frame for each frame in loop just to place them from one file to another.

How can I change only one frame and then flush somehow other frames to the output without loop av_read_frame , write_frame?

Upvotes: 0

Views: 296

Answers (1)

Mick
Mick

Reputation: 25491

What you ask seems a logical question, if you think of the frames in a video as a sort of linked list or array - you simply want to replace one element in the array or list without having to change everything else, I think?

The problem is that video streams are typically not as simply arranged as this.

For instance, the encoding often uses previous and even following frames as a reference for the current frame. As an example, every 10th frame might be encoded fully and the frames in between be simply encoded as deltas to these reference frames.

Also, the video containers, e.g. mp4, often use offsets to point to the various elements within them. Hence, if you dive inside and change the size of a particular frame by replacing it with another one, then the offsets will not be correct anymore.

If you are doing this for a single frame in a large video file, and you know roughly where the frame is, then you may find it more effective to split the video file, into a long section before the bit you want to change, a short section containing the frame you want to change, and a long section afterwards. You can apply your change just on the short section and then concatenate the videos at the end.

If you do something like this, you have to use a proper utility to split and concatenate the video, for the same reasons as above - i.e. to make sure the split videos have the right header info, offsets etc.

ffmpeg supports these type of functions - for example you can see the concatenation documentation here:

Upvotes: 2

Related Questions