Skip to main content
deleted 33 characters in body
Source Link
David Yaw
  • 797
  • 2
  • 7
  • 13

I would use ffprobe to read the width & height of the existing video, and do the math in bash to figure out which is the limiting factor.

(You mentioned that you wanted to set up one "script", so I'm hoping that means bash is acceptable.)

#!/bin/bash

W=$( ffprobe input.mp4 -show_streams |& grep width )
W=${W#width=}

H=$( ffprobe input.mp4 -show_streams |& grep height )
H=${H#height=}

# Target a 1920x1080 output video. 
TARGETW=1920
TARGETH=1080

USEWIDTH=$( echo "$W * $TARGETH > $H * $TARGETW" | bc )

# I'm not familiar with the resizing parameters to ffmpeg, 
# so I'm writing the below code based on the question you linked to. 

if [ $USEWIDTH$(( ==$W "1"* $TARGETH )) -gt $(( $H * $TARGETW" )) ]; then
    # The width is larger, use that
    SCALEPARAM="scale=-1SCALEPARAM="scale=$TARGETW:$TARGETH"-1"
else
    # The height is larger, use that
    SCALEPARAM="scale=$TARGETW:SCALEPARAM="scale=-1"1:$TARGETH"
fi

ffmpeg -i input.mp4 -vf $SCALEPARAM output.mp4

I would use ffprobe to read the width & height of the existing video, and do the math in bash to figure out which is the limiting factor.

(You mentioned that you wanted to set up one "script", so I'm hoping that means bash is acceptable.)

#!/bin/bash

W=$( ffprobe input.mp4 -show_streams |& grep width )
W=${W#width=}

H=$( ffprobe input.mp4 -show_streams |& grep height )
H=${H#height=}

# Target a 1920x1080 output video. 
TARGETW=1920
TARGETH=1080

USEWIDTH=$( echo "$W * $TARGETH > $H * $TARGETW" | bc )

# I'm not familiar with the resizing parameters to ffmpeg, 
# so I'm writing the below code based on the question you linked to. 

if [ $USEWIDTH == "1" ]; then
    # The width is larger, use that
    SCALEPARAM="scale=-1:$TARGETH"
else
    # The height is larger, use that
    SCALEPARAM="scale=$TARGETW:-1"
fi

ffmpeg -i input.mp4 -vf $SCALEPARAM output.mp4

I would use ffprobe to read the width & height of the existing video, and do the math in bash to figure out which is the limiting factor.

(You mentioned that you wanted to set up one "script", so I'm hoping that means bash is acceptable.)

#!/bin/bash

W=$( ffprobe input.mp4 -show_streams |& grep width )
W=${W#width=}

H=$( ffprobe input.mp4 -show_streams |& grep height )
H=${H#height=}

# Target a 1920x1080 output video. 
TARGETW=1920
TARGETH=1080

# I'm not familiar with the resizing parameters to ffmpeg, 
# so I'm writing the below code based on the question you linked to. 

if [ $(( $W * $TARGETH )) -gt $(( $H * $TARGETW" )) ]; then
    # The width is larger, use that
    SCALEPARAM="scale=$TARGETW:-1"
else
    # The height is larger, use that
    SCALEPARAM="scale=-1:$TARGETH"
fi

ffmpeg -i input.mp4 -vf $SCALEPARAM output.mp4
Source Link
David Yaw
  • 797
  • 2
  • 7
  • 13

I would use ffprobe to read the width & height of the existing video, and do the math in bash to figure out which is the limiting factor.

(You mentioned that you wanted to set up one "script", so I'm hoping that means bash is acceptable.)

#!/bin/bash

W=$( ffprobe input.mp4 -show_streams |& grep width )
W=${W#width=}

H=$( ffprobe input.mp4 -show_streams |& grep height )
H=${H#height=}

# Target a 1920x1080 output video. 
TARGETW=1920
TARGETH=1080

USEWIDTH=$( echo "$W * $TARGETH > $H * $TARGETW" | bc )

# I'm not familiar with the resizing parameters to ffmpeg, 
# so I'm writing the below code based on the question you linked to. 

if [ $USEWIDTH == "1" ]; then
    # The width is larger, use that
    SCALEPARAM="scale=-1:$TARGETH"
else
    # The height is larger, use that
    SCALEPARAM="scale=$TARGETW:-1"
fi

ffmpeg -i input.mp4 -vf $SCALEPARAM output.mp4