Video scaling, padding and cropping together question
I am attempting to down-convert HD video (1280x720 with 16:9 aspect ratio) to 720x576 for use on an old PAL 4x3 display. I found two days of doing this, using, (1) -filter:v scale=iw*min(720/iw\,576/ih):ih*min(720/iw\,576/ih), pad=720:576:(720-iw*min(720/iw\,576/ih))/2:(576-ih*min(720/iw\,576/ih))/2 -target pal-dvd (2) -filter:v 'crop=ih/3*4:ih' -target pal-dvd Method 1 scales and pads to letterbox format, so I get the whole of the original picture width with bars above and below. Method 2 scales and crops, so I get to use the whole 4x3 display screen height (better resolution) but lose the LH & RH picture edges via the cropping. 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. Could anyone show how to do this or point me in the right direction? Many thanks Tom Crane -- Tom Crane, Dept. Physics, Royal Holloway, University of London, Egham Hill, Egham, Surrey, TW20 0EX, England. Email: T.Crane@rhul.ac.uk Fax: +44 (0) 1784 472794
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
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
participants (2)
-
Frank Tetzel -
TPCffmpeg@mklab.ph.rhul.ac.uk