Creating a batch file with the IF command

I created a small batch file that helps people convert MP4 video to FLV using FFMPEG. A way to make it simple for everyone I know to use it. I thought the line I installed was ideal for every situation (converting an MP4 file to FLV), but a few days ago it did not work for the file. (audio selection in FLV format)

I found with the help of people another coding to convert it, but I do not know how to integrate it correctly in my batch file.

There is what I'm using right now:

echo "Enter a file name without extension": set / p file name =

echo "Enter the name you will pass to the destination file": set / p destinationfile =

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

And I want to add "IF". Because if this does not work with this line, use the following command:

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

How can i do this?

Thank you very much for your help, and if I don’t understand something, just tell me this and I will do my best to make everything clear.

Thanks!

+6
source share
2 answers

I'm not sure if FFMPEG will return a standard error code in case of failure, but if so, 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 does not work, you can check for a destination file to determine what to do next:

 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.

+8
source

Similar to the first EitanT 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 has been truncated, now everything is fixed

 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 
+4
source

Source: https://habr.com/ru/post/917119/


All Articles