[FFmpeg-user] Making conditional statements with FFMPEG

Moritz Barsnick barsnick at gmx.net
Wed Apr 29 21:48:27 CEST 2015


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:

---snip---
#!/bin/sh

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"
  fi

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

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. ;-)

Cheers,
Moritz


More information about the ffmpeg-user mailing list