[FFmpeg-user] Making conditional statements with FFMPEG

Chris Zecco czecco at mreach.com
Thu Apr 30 14:22:35 CEST 2015

Thanks Moritz,

Would I enter this right into my command prompt, or would I create a separate file to run this statement from? I keep getting an error saying "File unexpected at this time". Apologies for any ignorance. I am new to using the command window, though I do have coding experience in php and java (though I am not sure that will help here). Thanks!

- Chris

-----Original Message-----
From: ffmpeg-user-bounces at ffmpeg.org [mailto:ffmpeg-user-bounces at ffmpeg.org] On Behalf Of Moritz Barsnick
Sent: Wednesday, April 29, 2015 3:48 PM
To: FFmpeg user discussions
Subject: Re: [FFmpeg-user] Making conditional statements with FFMPEG

Hi Chris,

On Wed, Apr 29, 2015 at 18:50:55 +0000, Chris Zecco wrote:
> I was wondering if there was a way to create an IF-THEN statement 
> using FFMPEG for files in a certain directory.

No, but with Unix scripting, this is a breeze. (For non-native
speakers: This is very simple with Unix scripting. That's what it was made for.)

> For file sizes in excess of 200MB:
> Encode video bit rate at 1500k
> Encode audio bit rate at 128k
> For file sizes 100-100MB:
> Encode video bit rate at 1100k
> Encode audio bit rate at 128k
> For file sizes less than 100MB:
> Encode video bit rate at 555k
> Encode audio bit rate at 128k

I would inspect the input file. You would grab the size of the file with something like "stat" first, then compare that against your numbers. Then you put the command line parameters into a shell variable, and use that for ffmpeg's command line:


for file in *; do
  SIZE=`stat -c %s "$file"`
  if [ $SIZE -gt 209715200 ]; then # > 200 MiB
    COMPRESSION_V="-b:v 1500k"
  elif [ $SIZE -ge 104857600 ]; then # >= 100 MiB
    COMPRESSION_V="-b:v 1100k"
  else # < 100 MiB
    COMPRESSION_V="-b:v 555k"

  ffmpeg -i "$file" $COMPRESSION_V -b:a 128k "$file".new.extension done

Yeah, there are ways to preserve the file extension. I'm too lazy for that right now. (It's probably a fixed extension for you anyway.)

You're not taking the run length of the videos into consideration. You could query the original bitrate and apply e.g. half of that when converting each file. Just a suggestion. ;-)

ffmpeg-user mailing list
ffmpeg-user at ffmpeg.org

More information about the ffmpeg-user mailing list