Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a batch file with the "IF" command

I have created a little batch file for helping people to convert MP4 video to FLV with FFMPEG. A way to make it simple to everyone i know to use it. I was thinking the line i've put in was perfect for every situation (converting a MP4 file to FLV), but few days ago, it didn't work for a file. (audio sample to high for FLV format)

I found with the help of people a other codeline to convert it, but i don't know how to correctly integrate it, in my batch file.

There is what i use right now :

echo "Enter the name of the file, whitout the extension" : set /p namefile=

echo "Enter the name you what to give to the destination file" : set /p destinationfile=

ffmpeg -i %namefile%.mp4 -c:v libx264 -crf 19 %destinationfile%.flv

And i want to add a "IF". Because if that doesn't work with this line, use that one :

ffmpeg -i %namefile%.mp4 -c:v libx264 -ar 22050 -crf 28 %destinationfile%.flv

How can i do that ?

Thank you very much for your help and if i'm unclear on something, just tell it to me and i will do my best to make it clear.

Thanks !

like image 464
John_D Avatar asked May 31 '12 17:05

John_D


People also ask

Can you use if statements in batch files?

One of the common uses for the 'if' statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the 'if' statement can be done for both strings and numbers.

How do I create a .bat file?

To create a Windows batch file, follow these steps: Open a text file, such as a Notepad or WordPad document. Add your commands, starting with @echo [off], followed by, each in a new line, title [title of your batch script], echo [first line], and pause. Save your file with the file extension BAT, for example, test.

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

Does batch have else if?

However, you can't use else if in batch scripting. Instead, simply add a series of if statements: if %x%==5 if %y%==5 (echo "Both x and y equal 5.") if %x%==10 if %y%==10 (echo "Both x and y equal 10.") else (echo "Invalid input.")


2 Answers

I'm not sure if FFMPEG returns a standard error code in case of failure, but if it does, you can use the following:

ffmpeg -i %namefile%.mp4 -c:v libx264 -crf 19 %destinationfile%.flv
if not errorlevel 1 goto Done
ffmpeg -i %namefile%.mp4 -c:v libx264 -ar 22050 -crf 28 %destinationfile%.flv
:Done

If this approach doesn't work, you can check the existence of the destination file to determine further action:

ffmpeg -i %namefile%.mp4 -c:v libx264 -crf 19 %destinationfile%.flv
if exist %destinationfile%.flv goto Done
ffmpeg -i %namefile%.mp4 -c:v libx264 -ar 22050 -crf 28 %destinationfile%.flv
:Done

Hope one of these works.

like image 91
Eitan T Avatar answered Oct 12 '22 11:10

Eitan T


Similar to EitanT's first solution, but without using GOTO.

ffmpeg -i %namefile%.mp4 -c:v libx264 -crf 19 %destinationfile%.flv
if errorlevel 1 ffmpeg -i %namefile%.mp4 -c:v libx264 -ar 22050 -crf 28 %destinationfile%.flv

or
Edited - the code had gotten truncated, all fixed now

ffmpeg -i %namefile%.mp4 -c:v libx264 -crf 19 %destinationfile%.flv||ffmpeg -i %namefile%.mp4 -c:v libx264 -ar 22050 -crf 28 %destinationfile%.flv

Similar to EitanT's second solution, but without using GOTO

ffmpeg -i %namefile%.mp4 -c:v libx264 -crf 19 %destinationfile%.flv
if not exist %destinationfile%.flv ffmpeg -i %namefile%.mp4 -c:v libx264 -ar 22050 -crf 28 %destinationfile%.flv
like image 35
dbenham Avatar answered Oct 12 '22 09:10

dbenham