[FFmpeg-user] Stretch/Scale only center of overlay image

Hans Carlson forbyta at gmx.com
Fri Feb 14 21:21:57 EET 2020


On Thu, 13 Feb 2020, Ted Park wrote:

>> Is it possible to stretch/scale only the center part of an overlay image?
>>
>> I don't mean the middle section of a video, I mean only the center part 
>> of a single image (png) that will be overlayed onto a background, such 
>> that the left and right sides are relatively unchanged, but the center 
>> of the image is stretched to fit the new size.
>
> You will need to have three sections (two if you mirror the left or 
> right) and stretch only the center, and do the math for positioning each 
> one. Conceivably you could do this with one overlay image and a 
> combination of crop and split filters, but I feel that would be 
> unnecessarily convoluted, the positioning calculations are a headache 
> already, at least for me. I would create separate half-circle and 
> parallel line overlay images. Of course this wouldn’t result in an oval, 
> but I think the elongated “pill” shape is what you’re going for anyway.

I wouldn't be able to mirror the left/right because my "highlight" image 
isn't uniform... think more like circling a word or sentence by hand using 
a felt tip pen.  It is a "pill" shape, but intended to look more 
"hand drawn" than uniform.

I had considered the idea of splitting the image into 3 parts (as a backup 
plan) although I hadn't considered using a single image and crop.  As it 
turns out this isn't quite as bad as I first thought... in fact the 
calculations are simple if the hstack filter is used to recombine the 
pieces.

The raw HIGHTLIGHT.png image is 200 wide where the "ends" are each 30 
wide, meaning the center section to be scaled is 140 wide.   So, if I 
want a virtual highlight that's 300 wide, the center section needs to 
be: 300 - 30 - 30 = 240

   ffmpeg -loop 1 -i BACKGROUND.png -loop 1 -i HIGHTLIGHT.png
     -complex_filter
     [1:0]split=3[l0][c0][r0];
     [l0]crop=30:50:0:0[l];
     [c0]crop=140:50:30:0,scale=240:in_h[c];
     [r0]crop=30:50:170:0[r];
     [l][c][r]hstack=inputs=3[overlay];
     [0:0][overlay]overlay=x=200:y=100

If instead I want a virtual highlight that's only 100 wide, the center 
needs to be: 100 - 30 - 30 = 40

The only thing that changes is the scale amount for the center section:

   ffmpeg -loop 1 -i BACKGROUND.png -loop 1 -i HIGHTLIGHT.png
     -complex_filter
     [1:0]split=3[l0][c0][r0];
     [l0]crop=30:50:0:0[l];
     [c0]crop=140:50:30:0,scale=40:in_h[c];
     [r0]crop=30:50:170:0[r];
     [l][c][r]hstack=inputs=3[overlay];
     [0:0][overlay]overlay=x=200:y=100

Thanks for the suggestion.  Other than a ready made filter that only 
applies scale to part of an image I think this is probably the best I can 
hope for.  I don't know how efficient it is, but the files I'm creating 
are only a few seconds long, so efficiency isn't much of a concern.


More information about the ffmpeg-user mailing list