[FFmpeg-user] Video scaling, padding and cropping together question

TPCffmpeg at mklab.ph.rhul.ac.uk TPCffmpeg at mklab.ph.rhul.ac.uk
Sat Mar 31 07:37:00 EEST 2018


On Thu, 11 Jan 2018, Frank Tetzel wrote:

>> The thing I am trying to achieve is a compromise between these two
>> extremes, eg. a method which gives less cropping at the LH & RH
>> picture edges for the price of a little padding above and below the
>> picture.  I'm presuming this (ideally) involves scaling, padding and
>> cropping together in a single filter.
>
> Just pick a bigger width for the scale and pad filter, and then add
> crop as the last filter, cropping to the final resolution. Add crop
> with another comma to the filter chain as you did with scale and pad.
>
> Also, have a look at the filter documentation:
> http://ffmpeg.org/ffmpeg-filters.html

Many thanks for those hints which were just what I needed.  Below is a 
worked example of my solution, should it be helpful to anyone else.  The 
scaling/padding/cropping code is a little opaque but writing it this way 
enabled the whole thing to be written using ffmpeg's own parser.  If 
anyone is interested I can show how it was derived.

Recapping: Setting newfactor=0 shows the whole original 1280x720 picture 
downscaled in letterbox format on the 4x3 display.  With newfactor=1.0 the 
picture fills the whole 4x3 screen height and crops left and right.  I 
found newfactor=0.5 was a good compromise between the two limits.

The other settings (codecs, bit rates etc.) were intended to mirror the 
PAL DVD format, the target device being a DVD player.  The output file 
played nicely on it.

Cheers
Tom Crane.


#!/bin/sh
OW=720
OH=576
newfactor="$1"	# scaling factor
OUTFILE=/tmp/tmp.avi

# Scale, pad and crop to a compromise between letterbox format (newfactor=0) and a LH+RH cropped picture (newfactor=1)
ffmpeg -y -i demo.mp4 -vf                  \
         "scale=iw*min(($OW * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/iw\,($OH * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/ih):                \
                ih*min(($OW * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/iw\,($OH * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/ih),                \
          pad=($OW * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1)):                                      \
              ($OH * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1)):                                      \
              (($OW * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))-iw*min(($OW * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/iw\,($OH * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/ih))/2:         \
              (($OH * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))-ih*min(($OW * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/iw\,($OH * ((($OH/ih)*(iw/$OW) -1) * $newfactor + 1))/ih))/2:         \
              green,                                     \
          crop=$OW:$OH"                                  \
 	-c:v mpeg2video -c:a ac3 -f dvd -r 25 -pix_fmt yuv420p -g 15 -b:v 6000000 -maxrate 9000000 -minrate 0 -bufsize 1835008 -packetsize 2048 -muxrate 10080000 -b:a 448000 -ar 48000		\
 	$OUTFILE
exit 0


More information about the ffmpeg-user mailing list