I'm using the following command (in my android application) to overlay an image on top of a video:

"-i", video.mp4, "-i", image.mp4, "-filter_complex", "[0:v]pad=iw:if(lte(ih\\,iw)\\,ih\\,2trunc(iw*16/9/2)):(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2[v]", "-map", "[v]", "-map", "0:a", "-c:v", "libx264", "-preset", "ultrafast", "-r", outFPS, output.mp4

I implemented the above after asking this question.

This works perfectly fine when the video aspect ratio is 16:9, but I have users that complain that the image is not at the correct position. I noticed that this is on devices that has a different aspect ratio, for example 13:6.

Is it correct if I change the command to the following, if for example, the aspect ratio is 13:6?

"-i", video.mp4, "-i", image.mp4, "-filter_complex", "[0:v]pad=iw:if(lte(ih\\,iw)\\,ih\\,2trunc(iw*13/6/2)):(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2[v]", "-map", "[v]", "-map", "0:a", "-c:v", "libx264", "-preset", "ultrafast", "-r", outFPS, output.mp4

I don't have a device to test with, thats why I decided to ask a question.


I tested with the command above using a video with the following dimensions - 1980x1020 - aspect ratio of 16:9, then the video dimensions change to 1040x480 - aspect ratio of 13:6.

That is exactly what I wanted, here is the issue:

If I use the video mentioned above (1040x480 - aspect ratio of 13:6) and I try to change the aspect ratio to 16:9, using the first command in this question, then I get the following error:

[Parsed_pad_0 @ 0xeeb3f540] Input area 0:-94:480:946 not within the padded area 0:0:480:852 or zero-sized
[Parsed_pad_0 @ 0xeeb3f540] Failed to configure input pad on Parsed_pad_0
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #1:0


Running the command mentioned in the comments of the answer, this is the result.

After FFmpeg:

Without adding -noautorotate

enter image description here

After adding -noautorotate

enter image description here


Here is the log without using -noautorotate

I used this command:

String[] s = {"-ss", valueLeft, "-i", mStringFilePath, "-i", drawingPath, "-filter_complex", "[0:v]scale=iw*sar:ih,setsar=1,pad='max(iw\\,2*trunc(ih*16/9/2))':'max(ih\\,2*trunc(ow*9/16/2))':(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2,scale=-2:min(1080\\,ih)[v]", "-map", "[v]", "-map", "0:a", "-c:v", "libx264", "-preset", "ultrafast", "-r", outFPS, path};

Here is the log with using -noautorotate

I used this command:

String[] s = {"-ss", valueLeft,"-noautorotate", "-i", mStringFilePath, "-i", drawingPath, "-filter_complex", "[0:v]scale=iw*sar:ih,setsar=1,pad='max(iw\\,2*trunc(ih*16/9/2))':'max(ih\\,2*trunc(ow*9/16/2))':(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2,scale=-2:min(1080\\,ih)[v]", "-map", "[v]", "-map", "0:a", "-c:v", "libx264", "-preset", "ultrafast", "-r", outFPS, path};

Here is the links to the videos:

  • Link to original video
  • Link to video that doesn't use -noautorotate
  • Link to video after using -noautorotate
  • 1
    Like I said in the comment in the other Q, change 16 and 9 to 13 and 6 in the pad filter exprs.
    – Gyan
    Jul 10, 2019 at 18:27
  • @Gyan Thank you for your reply, I appreciate it. Can you please have a look at my edit.
    – HB.
    Jul 11, 2019 at 5:48

1 Answer 1




First, the scale and setsar make sure the image becomes a square-pixel input. Then the pad checks each dimension and pads to a 16:9 frame size.

  • Comments are not for extended discussion; this conversation has been moved to chat.
    – AJ Henderson
    Jul 16, 2019 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.