Answers.Body: <p>Use</p>
  
  <p><code>"[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]"</code></p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-16T23:45:25.810Z
Answers.Text: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/96273/discussion-on-answer-by-gyan-ffmpeg-issue-with-aspect-ratio).
Answers.UserDisplayName: AJ Henderson
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-11T06:22:37.733Z
Answers.LastActivityDate: 2019-07-11T06:22:37.733Z
Answers.UserDisplayName: Gyan
Body: <p>I'm using the following command (in my android application) to overlay an image on top of a video:</p>
  
  <pre><code>"-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
  </code></pre>
  
  <p>I implemented the above after asking <a href="https://stackoverflow.com/q/55332539/5550161">this</a> question.</p>
  
  <hr>
  
  <p>This works perfectly fine when the video aspect ratio is <code>16:9</code>, 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 <code>13:6</code>.</p>
  
  <hr>
  
  <p>Is it correct if I change the command to the following, if for example, the aspect ratio is <code>13:6</code>?</p>
  
  <pre><code>"-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
  </code></pre>
  
  <p>I don't have a device to test with, thats why I decided to ask a question.</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I tested with the command above using a video with the following dimensions - <code>1980x1020</code> - aspect ratio of <code>16:9</code>, then the video dimensions change to <code>1040x480</code> - aspect ratio of <code>13:6</code>.</p>
  
  <p>That is exactly what I wanted, here is the issue:</p>
  
  <p>If I use the video mentioned above (<code>1040x480</code> - aspect ratio of <code>13:6</code>) and I try to change the aspect ratio to <code>16:9</code>, using the first command in this question, then I get the following error:</p>
  
  <pre><code>[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
  </code></pre>
  
  <hr>
  
  <p><strong>Edit2</strong></p>
  
  <p>Running the command mentioned in the comments of the answer, this is the result.</p>
  
  <p>After FFmpeg:</p>
  
  <p><strong>Without adding <code>-noautorotate</code></strong></p>
  
  <p><a href="https://i.sstatic.net/tN3pC.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tN3pC.jpg" alt="enter image description here"></a></p>
  
  <p><strong>After adding <code>-noautorotate</code></strong></p>
  
  <p><a href="https://i.sstatic.net/89W3V.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/89W3V.jpg" alt="enter image description here"></a></p>
  
  <hr>
  
  <p><strong>EDIT 3</strong></p>
  
  <p><a href="https://pastebin.com/7xKDxP4h" rel="nofollow noreferrer">Here</a> is the log without using <code>-noautorotate</code></p>
  
  <p>I used this command:</p>
  
  <pre><code>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};
  </code></pre>
  
  <hr>
  
  <p><a href="https://pastebin.com/FNu0tdCZ" rel="nofollow noreferrer">Here</a> is the log with using <code>-noautorotate</code></p>
  
  <p>I used this command:</p>
  
  <pre><code>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};
  </code></pre>
  
  <hr>
  
  <p>Here is the links to the videos:</p>
  
  <ul>
  <li><a href="https://wetransfer.com/downloads/92ba9c4652aa07d209c12f59e4c7b8f220190711112651/fa969cf7ddb7ea86b9709c37bc4d298320190711112651/f2e0ac" rel="nofollow noreferrer">Link</a> to original video</li>
  <li><a href="https://wetransfer.com/downloads/b7347585d7dbaf01ae0db8ae1176792f20190711113615/4c83b5d523725eb6892af386e65efa3b20190711113615/aead93" rel="nofollow noreferrer">Link</a> to video that doesn't use <code>-noautorotate</code></li>
  <li><a href="https://wetransfer.com/downloads/11c94ac17be8c028bd614997a28e08b820190711113141/8efe951610dc9f228ecc5e3a4fa12b2b20190711113141/23e20b" rel="nofollow noreferrer">Link</a> to video after using <code>-noautorotate</code></li>
  </ul>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-10T18:27:02.613Z
Comments.Text: Like I said in the comment in the other Q,  change 16 and 9 to 13 and 6 in the pad filter exprs.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-11T05:48:17.780Z
Comments.Text: @Gyan Thank you for your reply, I appreciate it. Can you please have a look at my edit.
Comments.UserDisplayName: HB.
Tags: <ffmpeg><aspect>
Title: FFmpeg issue with aspect ratio
UserDisplayName: HB.
ThreadText: # FFmpeg issue with aspect ratio
  
  (Asked by: username_0 on 2019-07-10)
  
  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](https://stackoverflow.com/q/55332539/5550161) 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`**
  
  [![enter image description here](https://i.sstatic.net/tN3pC.jpg)](https://i.sstatic.net/tN3pC.jpg)
  
  **After adding `-noautorotate`**
  
  [![enter image description here](https://i.sstatic.net/89W3V.jpg)](https://i.sstatic.net/89W3V.jpg)
  
  ---
  
  **EDIT 3**
  
  [Here](https://pastebin.com/7xKDxP4h) 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](https://pastebin.com/FNu0tdCZ) 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](https://wetransfer.com/downloads/92ba9c4652aa07d209c12f59e4c7b8f220190711112651/fa969cf7ddb7ea86b9709c37bc4d298320190711112651/f2e0ac) to original video
  * [Link](https://wetransfer.com/downloads/b7347585d7dbaf01ae0db8ae1176792f20190711113615/4c83b5d523725eb6892af386e65efa3b20190711113615/aead93) to video that doesn't use `-noautorotate`
  * [Link](https://wetransfer.com/downloads/11c94ac17be8c028bd614997a28e08b820190711113141/8efe951610dc9f228ecc5e3a4fa12b2b20190711113141/23e20b) to video after using `-noautorotate`
  
  **Tags:** ffmpeg, aspect
  
  > **Comments:**
  > **username_1**: Like I said in the comment in the other Q,  change 16 and 9 to 13 and 6 in the pad filter exprs.  
  > **username_0**: @username_1 Thank you for your reply, I appreciate it. Can you please have a look at my edit.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-07-11. Score: 1
  
  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.
  
  > **Comments:**
  > **username_2**: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/96273/discussion-on-answer-by-username_1-ffmpeg-issue-with-aspect-ratio).  
  
  ---
  
  