Scale down if resolution is greater than given?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Scale down if resolution is greater than given?

vedran
I'm looking for simple bash script which scales down video only if it has
greater resolution than given.


The problem is also with "vertical videos". So i need to check width and
height.


Please, if you have got some script that does that, send it to me.

greetings

p.s.
So far i'm very confused that all those basic steps ffmpeg does through
numerous bash tricks.
It is not possible to get video height/width or length easily but i often
would have to use awk and so many more applications to achieve that.
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Scale down if resolution is greater than given?

Moritz Barsnick
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
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Scale down if resolution is greater than given?

Mahesh Patade
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 <[hidden email]> 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
> [hidden email]
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Scale down if resolution is greater than given?

Moritz Barsnick
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
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Scale down if resolution is greater than given?

vedran
Anyway, thank you all guys. I did it like this, although i don't understand
well what this does

ffmpeg -i myvideo001.3gp -vf scale=1024:-1 -vb 600k -ac 2 -ab 96k -ar 44100
-f mp4 myvideo001out.mp4

On Wed, Jul 29, 2015 at 3:46 PM, Moritz Barsnick <[hidden email]> wrote:

> 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
> _______________________________________________
> ffmpeg-user mailing list
> [hidden email]
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
>
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Scale down if resolution is greater than given?

Moritz Barsnick
On Wed, Jul 29, 2015 at 17:27:47 +0200, vedran wrote:
> Anyway, thank you all guys. I did it like this, although i don't understand
> well what this does
>
> ffmpeg -i myvideo001.3gp -vf scale=1024:-1 -vb 600k -ac 2 -ab 96k -ar 44100
> -f mp4 myvideo001out.mp4

Oh my, what's there not to understand? ;-)

It scales your input video to a width of 1024 (regardless of whether
the input is narrower or wider), while maintaining the aspect ratio.

Moritz
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Scale down if resolution is greater than given?

vedran
sorry not that one, this one

ffmpeg -i video.mov -filter_complex
"scale=iw*min(1\,min(640/iw\,360/ih)):-1"  -vb 600k -ac 2 -ab 96k -ar 44100
-f mp4 out.mp4

On Wed, Jul 29, 2015 at 5:33 PM, Moritz Barsnick <[hidden email]> wrote:

> On Wed, Jul 29, 2015 at 17:27:47 +0200, vedran wrote:
> > Anyway, thank you all guys. I did it like this, although i don't
> understand
> > well what this does
> >
> > ffmpeg -i myvideo001.3gp -vf scale=1024:-1 -vb 600k -ac 2 -ab 96k -ar
> 44100
> > -f mp4 myvideo001out.mp4
>
> Oh my, what's there not to understand? ;-)
>
> It scales your input video to a width of 1024 (regardless of whether
> the input is narrower or wider), while maintaining the aspect ratio.
>
> Moritz
> _______________________________________________
> ffmpeg-user mailing list
> [hidden email]
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
>
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user