0

I am trying to convert a large number of files from many sources into a standard format of 426x240 using ffmpeg with the following arguments:

-y -i input.mov -ac 2 -ab 96k -ar 44100 -vcodec libx264 -pix_fmt yuv420p -preset medium -threads 0 -level 1.3 -max_muxing_queue_size 1024 -b:v 438k -r 30000/1001 -s 426x240 output.mp4

Even though this command worked in the previous version of ffmpeg I was using (from many years ago) in the new version I am using (4.2.3), video is stretched to 16:9. While attempting to fix it, I found this answer with the arguments: -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1:color=black". This appears to work, though I am concerned that I am scaling the result twice, once to 1280:720, then down to 426:240. To prevent this, I found the aspect argument for pad in the documentation, but when I try to use it I get the following error:

[Parsed_pad_0 @ 00000130808eec80] Negative values are not acceptable.
[Parsed_pad_0 @ 00000130808eec80] 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 #0:0

The arguments used to get this error (though I have tried numerous versions):

-y -i input.mov -ac 2 -ab 96k -ar 44100 -filter:v "pad=x=-1:y=-1:color=black:aspect=16\:9" -vcodec libx264 -pix_fmt yuv420p -preset medium -threads 0 -level 1.3 -max_muxing_queue_size 1024 -b:v 438k -r 30000/1001 -s 426x240 output.mp4

Can some please provide a working example of how to use the aspect parameter of the pad filter?

Additional details:

  • The video is from an iPhone X. The video was taken with the phone in portrait orientation. The video, when analyzed with ffprobe indicates that the rotation is 90.
  • I cannot update to the latest version due to the later versions having a dependency on MFPlat.DLL which is not available in my working environment. The new versions from gyan.dev do not have this issue.
Trisped
  • 412
  • 3
  • 13

2 Answers2

1

The aspect value is ideally specified as X/Y; avoid : as it is a syntax element in filtergraphs.

Your particular set of videos have an invalid sample aspect ratio. Insert setsar=1 before pad.


The easiest solution, however, is below

Add

-vf "scale=426:240:force_original_aspect_ratio=decrease,pad=426:240:-1:-1:color=black"

and remove -s 426x240

Gyan
  • 34,439
  • 6
  • 56
  • 98
  • Hi Gyan, Thanks for the answer. I am actually looking for a way to use just pad with the aspect parameter. Do you have an example on how to do that? – Trisped Oct 13 '20 at 18:53
  • pad with aspect can't be made to reliably work here since your w and h are iw and ih (by default). It can only reliably work if your output frame is guaranteed to be larger than the input after adjusting for aspect. – Gyan Oct 13 '20 at 19:18
  • If I understand you correctly, the aspect parameter of pad will not add padding until the image fits the indicated aspect ratio. This is confusing to me since the [documentation](https://ffmpeg.org/ffmpeg-filters.html#pad-1) states "Pad to aspect instead to a resolution." – Trisped Oct 13 '20 at 19:35
  • You can consider it a defect. pad can only add pixels to the source frame not remove them. With `aspect` specified, ffmpeg adjusts the pad target dimensions to satisfy the aspect ratio. But it does this by reducing target dimension values. If either one or both of the adjusted values are smaller than the source, then pad will error out. You can avoid that by specifying a larger target e.g. `pad=iw+6:ih+6:-1:-1:aspect=16/9` – Gyan Oct 13 '20 at 19:57
  • Hmm, that command fails with the same error, as does `-vf "pad=aspect=16/9"` and `-vf "pad=iw+600:ih+600:-1:-1:aspect=16/9"` – Trisped Oct 13 '20 at 23:55
  • Share full log. – Gyan Oct 14 '20 at 08:45
  • You can find the full log [here](https://pastebin.pl/view/4c831ed3). I thought about putting it in the question, but since it is specific to this answer I did not feel that was appropriate. – Trisped Oct 15 '20 at 17:58
  • Ah, you're using an older version. I corrected that check in 4.3. Update. See my profile for a link. – Gyan Oct 15 '20 at 18:47
  • I cannot upgrade to the latest version due to the MFPlat.DLL dependency, so I am stuck with 4.2.3. I did download the latest from your page and try it on my local machine, but got a new error **[Parsed_pad_0 @ 000001d2204973c0] Padded dimensions cannot be smaller than input dimensions**. I did a few tests which you can see [here](https://pastebin.pl/view/fd70ba77) – Trisped Oct 15 '20 at 20:06
  • is this a bug in ffmpeg that the aspect parameter of pad just does not work? – Trisped Oct 20 '20 at 16:26
  • Share log with latest version. Don't use the last site - it's slow. – Gyan Oct 20 '20 at 16:53
  • Downloaded the 10/17 version and ran the same commands again. Output is [here](https://controlc.com/45567113) – Trisped Oct 21 '20 at 00:12
  • Ok, so your inputs are 1280x720 with a 90 deg rotation tag. ffmpeg will autorotate the input and provide pad with 720x1280 frames. Padding to 16:9 means an output resolution of 2276x1280. And the numerical constant of 6 is not enough to target a large enough frame. It would have to be at least 1576. Which is why you should use the method in my answer. Like I said above, this can be considered a design defect. I'll fix this in pad sometime in the next month. – Gyan Oct 21 '20 at 04:32
  • Can you share one of these inputs? – Gyan Oct 21 '20 at 19:16
  • I sent you an email with one of the videos. I used the email address in your profile. – Trisped Oct 21 '20 at 19:42
  • Received. Weird, I don't get the error with a similar file of my own. Will debug in a day or two. – Gyan Oct 22 '20 at 04:59
  • Your files have an invalid aspect ratio. Insert `setsar=1` before pad. No need to add any offset in pad so iw / ih are fine with aspect. – Gyan Oct 22 '20 at 06:16
  • That worked. Thanks. Do you want to update your answer, or should I? Let me know so I can accept and upvote. – Trisped Oct 22 '20 at 17:58
  • Done. BTW, ffmpeg has no dependency on MFPlat.dll – Gyan Oct 23 '20 at 04:04
  • Your right. I had the problem last month with the previous provider. I thought I had tested again when gyan.dev became the recommended windows source, though when I just tested it worked without issues (I also tested with the previous provider's code and it still errors out asking for the MFPlat.DLL). – Trisped Oct 26 '20 at 17:22
1

Imho, subfunction pad:aspect works from width for the first step, so:

ffmpeg -hide_banner -i "input 1.mp4" -filter:v "scale='if(gte(dar,16/9),426,-2)':'if(gte(dar,16/9),-2,240)', pad=426:240:'if(gte(dar,16/9),-1,(ow-iw)/2)':'if(gte(dar,16/9),-1,0)'" "output.mkv"

if greater 16/9 then "scale=426:-2, pad=426:240:-1:-1" else "scale=-2:240, pad=426:240:(ow-iw)/2:0"

edit add:

for example: width=16, height=7

pad=aspect=16/9 will evaluate height from width, pad=9-7=2, positive value

if width=16, height=12

this will evaluate pad=9-12=-3 negative value, cause of error

  • This works for my case, but does not answer the question "How to use aspect parameter of pad in ffmpeg?" Also, what does the -2 mean? I know the -1 means to center the image in the padding. I assume gte is greater than or equal to, dar is display aspect ratio, ow is out width, and iw is in width. – Trisped Oct 15 '20 at 20:12