FFmpeg issue with aspect ratio 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.



EDIT:

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




Edit2

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

After FFmpeg:

Without adding -noautorotate



After adding -noautorotate





EDIT 3

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
Use

"[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[v]"

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.