[FFmpeg-user] Scale down if resolution is greater than given?

Moritz Barsnick barsnick at gmx.net
Wed Jul 29 15:46:47 CEST 2015


Hi Mahesh,

On Wed, Jul 29, 2015 at 18:08:56 +0530, Mahesh Patade wrote:
> Check this. I have written this logic in my script.

Wow. If you're going to use scripting, why not use it to make life (and
readability) easier instead of harder.

For instance, just for my(!) readability, I would change this:

>                 transcode() {
>                     # 128 Bitrate
>                     MULBIT128=$(echo  ${1}|sed 's#x#*#g' |bc)
>                     if [ ${REOLVIDEO} -gt ${MULBIT128} ]; then
>                         if ! ffmpeg -threads ${CPUNO} -ss
> ${START_TIME} -t ${LENGTH} -i "${FILENAME}" -s $1 -movflags rtphint
> -b:v 128k -vcodec libx264 -acodec libfaac -ab 20k -ar 44100 -y
> "${OUTPATH}${FILEWOEXT}.part${CURRENT_NODE}-128000.ts" >>
> "${OUTPATH}${FILEWOEXT}.part${CURRENT_NODE}-128000.log.txt" 2>&1; then
>                             ERROR=1
>                             ERRORLOG="${ERRORLOG}Failed: 128 Bitrate Conversion"
>                         fi
>                     else
>                         if ! ffmpeg -threads ${CPUNO} -ss
> ${START_TIME} -t ${LENGTH} -i "${FILENAME}" -movflags rtphint -b:v
> 128k -vcodec libx264 -acodec libfaac -ab 20k -ar 44100 -y
> "${OUTPATH}${FILEWOEXT}.part${CURRENT_NODE}-128000.ts" >>
> "${OUTPATH}${FILEWOEXT}.part${CURRENT_NODE}-128000.log.txt" 2>&1; then
>                             ERROR=1
>                             ERRORLOG="${ERRORLOG}Failed: 128 Bitrate Conversion"
>                         fi
>                     fi

To this:
>                 transcode() {
>                     # 128 Bitrate
>                     MULBIT128=$(echo  ${1}|sed 's#x#*#g' |bc)
>                     if [ ${REOLVIDEO} -gt ${MULBIT128} ]; then
>                           SCALE="-s $1"
>                     else
>                           SCALE=""
>                     fi
>                     if ! ffmpeg -threads ${CPUNO} -ss
> ${START_TIME} -t ${LENGTH} -i "${FILENAME}" $SCALE -movflags rtphint
> -b:v 128k -vcodec libx264 -acodec libfaac -ab 20k -ar 44100 -y
> "${OUTPATH}${FILEWOEXT}.part${CURRENT_NODE}-128000.ts" >>
> "${OUTPATH}${FILEWOEXT}.part${CURRENT_NODE}-128000.log.txt" 2>&1; then
>                           ERROR=1
>                           ERRORLOG="${ERRORLOG}Failed: 128 Bitrate Conversion"
>                     fi

This makes the difference between the if and the else clause much
clearer!

MULBIT128, MULBIT256, MULBIT512 could also be differentiated by simple
variables/function arguments, and one very common ffmpeg command line.
That also makes modifying other arguments to ffmpeg much easier. You
don't have to do it X times. That's the whole point of scripting! SCNR.

>                 eval $(ffprobe -v error -of flat=s=_ -select_streams
> v:0 -show_entries stream=height,width "${FILENAME}")
>                 SIZE=${streams_stream_0_width}x${streams_stream_0_height}
> 
>                 REOLVIDEO=$(echo ${SIZE} |sed 's#x#*#g' | bc)
>                 RESOLUTION=$(echo "scale=1;
> $streams_stream_0_width/$streams_stream_0_height" | bc)

So REOLVIDEO is the number of pixels, RESOLUTION is what we usually
call the aspect ratio? I wouldn't use that to choose the target sizes,
but just maintain the aspect ratio. But that's your call.

I would use this filter now (we can ignore Mahesh's multi-bitrates for
this question):

$ ffmpeg [...] -vf "scale=w=if(gt(iw*ih\,1280*720)\,iw*min(1280/iw\,1024/ih)\,iw):h=-2" [...]

This scales any video with more pixels than 1280*720 inside of a box of
1280*720. An improvement would be to scale it to the correct number of
pixels. More math. ;-)

Moritz


More information about the ffmpeg-user mailing list