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

Mahesh Patade patademahesh at gmail.com
Wed Jul 29 14:38:56 CEST 2015


Hey,

Check this. I have written this logic in my script.

streams_stream_0_width= ; streams_stream_0_height=

                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)

                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

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

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

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

                if [ ${RESOLUTION} = '1.3' ]; then
                    transcode 320x240 480x360 640x480 1024x768
                elif [ ${RESOLUTION} = '1.7' ]; then
                    transcode 384x216 512x288 640x360 1024x576
                else
                    transcode 384x216 512x288 640x360 1024x576
                fi

script source: https://raw.githubusercontent.com/patademahesh/Distributed-Video-Transcoding/master/transcode-nodes.sh




-- 
Thanks,
Mahesh


On Wed, Jul 29, 2015 at 5:59 PM, Moritz Barsnick <barsnick at gmx.net> wrote:
> Hi Vedran,
>
> On Wed, Jul 29, 2015 at 03:42:05 +0200, vedran wrote:
>> I'm looking for simple bash script which scales down video only if it has
>> greater resolution than given.
>
> You won't need a bash script necessarily. ffmpeg can handle
> expressions.
>
>> The problem is also with "vertical videos". So i need to check width and
>> height.
>
> Do you need the math to express what to do, or do you need the ffmpeg
> command line?
>
> For instance, if you want ffmpeg to scale down anything wider than
> 1280, use a filter somewhat like this:
>
> $ ffmpeg [...] -vf "scale=w=min(iw\,1280):h=-2" [...]
>
> ("h=-2" maintains the aspect ratio, using only even numbers.)
>
> You can make that expression more complex, e.g. if you want to restrict
> both width and height, or you want their product not be larger than a
> certain number, by construction "if-the-else" (actually "and/or") type
> expressions.
>
> What logic were you thinking of? ("If the video is so-and-so, do this,
> else if ... do that, else do something else, else don't change
> resolution.") Can you express it in words?
>
> Cheers,
> Moritz
> _______________________________________________
> ffmpeg-user mailing list
> ffmpeg-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user


More information about the ffmpeg-user mailing list