Making conditional statements with FFMPEG
I was wondering if there was a way to create an IF-THEN statement using FFMPEG for files in a certain directory. For example, I am using FFMPEG to crunch down file sizes of videos to 50MB or lower. I would ideally like to do the following: 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 Is this possible? And yes, I have run tests on individual file to ensure there is no real perceptible quality loss when dropping the bit rates that low. Any help that you could give would be amazing, and greatly appreciated. Thanks! - Chris
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
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@ffmpeg.org [mailto:ffmpeg-user-bounces@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: ---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 _______________________________________________ ffmpeg-user mailing list ffmpeg-user@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-user
On Thu, Apr 30, 2015 at 2:22 PM, Chris Zecco <czecco@mreach.com> wrote:
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!
It seems like you're running Windows. Moritz gave you code that runs in Bash (or Bash-like shells), which are typically found on Linux/Unix systems. There are many ways to get Bash on Windows. (Please do not top-post on this mailing list. Place your reply under the other's message.)
On Thu, Apr 30, 2015 at 3:04 PM, Werner Robitza <werner.robitza@gmail.com> wrote:
There are many ways to get Bash on Windows.
I should add that you can also transform this into a Windows Batch file, or a Java program that does the looping and executes ffmpeg. What's important is that ffmpeg on its own cannot do what you want.
On 4/30/2015 8:22 AM, Chris Zecco wrote:
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! You might find it beneficial to have 1 dir in your home dir that you keep scripts and add something like this
SrcDir=/media/BlahBlahpathtoVideoSourceFiles cd $SrcDir it will then "do" all in the dir BlahBlahpathtoVideoSourceFiles
---snip--- #!/bin/sh
SrcDir=/media/BlahBlahpathtoVideoSourceFiles cd $SrcDir
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.extensio done ---snip---
participants (4)
-
Chris Zecco -
Jim Shupert -
Moritz Barsnick -
Werner Robitza