[FFmpeg-user] Overlaying part of a video by an image file [quickly, without re-encoding the rest]

Gyan Doshi ffmpeg at gyani.pro
Sat Jul 25 08:29:06 EEST 2020

On 25-07-2020 01:38 am, Jim DeLaHunt wrote:
> On 2020-07-24 07:34, Marcello Mazzon wrote:
>> Hi;
>> for months I have been on a project where I am trying to overlay a few
>> seconds of a longer video by an image file, leaving the audio sequence
>> intact. For this, I posted an enquiry on stackexchange [*] to which I
>> received a reply by an official ffmpeg-consultant.
>> *
>> https://unix.stackexchange.com/questions/588667/ffmpeg-replace-part-of-a-video-by-a-jpg-file-for-5-seconds 
>> Unfortunately, we were incapable of coming up with a solution to
>> integrate the image file into the video without re-encoding the whole
>> of the video file but only the part where the image is being overlayed,
>> leaving the exact codec and parameters of the video intact.
>>>> Greetings from Germany;
>> Marcello
> Marcello:
> Welcome to the ffmpeg-users email list.
> I will make the comment here which I also put on the Stack-exchange 
> question: You apparently have additional requirements: that the 
> operation complete in just a few seconds, and that the operation does 
> not reencode the entire video sequence. You are more likely to get 
> relevant answers if you include those requirements in your question. 
> Note that these requirements may mean there is no easy way to satisfy 
> them using FFmpeg.
> In my experience, Gyan Doshi, who answered your question on Stack 
> Exchange, knows a lot about FFmpeg and is very willing to help. If 
> Gyan says that avoiding re-encoding the video is "not trivial", then 
> that likely means the task is quite difficult. However, maybe someone 
> will point out a clever use of FFmpeg which meets your requirements.
> Much is possible if you are willing to write an extension of some kind 
> to ffmpeg to operate on the video directly. However, it will be a lot 
> of work to learn enough about FFmpeg to be able to write this code, 
> and also substantial effort to write, test, and deploy the code. You 
> will have to decide if that effort (or hiring someone to make that 
> effort) is worth avoiding the video reencode.

As I mentioned in the comment at SE, this can be done via segmentation, 
modifying and re-encoding affected segments, then stitching old and 
re-encoded segments back together. The tricky part is satisfying a 
decoder to play both old and new segments. Some like our own H264 
decoder have no, or I should say, few issues doing this even if codec 
parameter sets don't perfectly match. But I expect hardware players and 
many old software players to be finicky. It can be done but it's not 
easily automatable, which I presume, is what the OP wants.


More information about the ffmpeg-user mailing list