16

I am trying to convert 20+ .avi files in a batch using ffmpeg.

I've got the following

@echo off.

for file in *.avi
do
   ffmpeg -i "$file" -s 640x480 -vcodec msmpeg4v2 "'basename "$file" .avi'.mpg';
done

in my .bat file but it does not work. How can I make it work under Windows OS. Oh, and yes all the files are in the same folder.

The error message I get:

File was unexpected at this time

Darius
  • 2,246

3 Answers3

30

Your batch file is not in the correct format for a Windows bat script. Instead your script looks like it was intended for Linux. You will need to change the script to use a for loop that is supported by the Windows Shell.

Below is an example of how to accomplish this task by using a windows for loop. Just put the below line in your batch file and move the script to the same directory as the avi files, then run it.

for %%A IN (*.avi) DO ffmpeg -i "%%A" "%%A.mpg"
Brett
  • 651
2

A simple addition to Brett's answer:

for %A IN (*.avi) DO ffmpeg -i "%A" "%~nA.mpg"
  • OBs.: cmd one-liner, for use in batch files use %%

Add %~n to the variable in the output file name to get rid of the original file extension.

This was also used in It Wasn't Me's answer, but I thought this convenient detail was maybe lost on people who just want to use a cmd one-liner.

Io-oI
  • 9,237
1

Why not crop/remove the file extension in your output file?

@echo off

for file in *.avi do ffmpeg -i "$file" -s 640x480 -vcodec msmpeg4v2 "'basename "$file" .avi'.mpg'; done


@echo off

for %%i in (*.avi)do ffmpeg -i "%%~fi" -s 640x480 -vcodec msmpeg4v2 "%%~dpni.mpg"

rem :: "%%~fi" == Full Path == Drive + Path + Name + Extension %%i:
rem :: "%%~fi" == C:\Path\FileName.AVI

rem :: "%%~dpni.mpg" == Drive + Path + Name + .mpg %%i: rem :: "%%~dpni.mpg" == C:\Path<b>FileName.mpeg

  • To have the Name.eXtension (%%~nxi) of the input and output file on the screen, and to reduce the ffmeg output information, limiting only the conversion in progress (-hide_banner -v error -stats):
@echo off

for %%i in (*.avi)do echo\Enconding: "%%~nxi" "%%~ni.mpg" && ( ffmpeg -i "%%~fi" -s 640x480 -vcodec msmpeg4v2 "%%~dpni.mpg" -hide_banner -v error -stats )

  • Output in my test:
Enconding: "bird_test.avi" "bird_test.mpg"
frame=  355 fps=0.0 q=31.0 Lsize=     680kB time=00:00:11.80 bitrate= 472.1kbits/s speed=  22x
Enconding: "file_input.avi" "file_input.mpg"
frame=  903 fps=248 q=11.3 Lsize=    2440kB time=00:00:30.56 bitrate= 653.9kbits/s dup=2 drop=0 speed= 8.4x
Io-oI
  • 9,237