Batch convert *.avi files using ffmpeg

46,631

Solution 1

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"

Solution 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.

Solution 3

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\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
Share:
46,631

Related videos on Youtube

Darius
Author by

Darius

Updated on September 18, 2022

Comments

  • Darius
    Darius almost 2 years

    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
      Darius almost 12 years
      If you can point me how to get the error message since the cmd disappears very fast, I will be able to tell you what I'm having problem with.
    • slhck
      slhck almost 12 years
      You should be able to just open a command window before (Start » Run… » cmd) and then run the batch file using its full path from there. What do you actually want to do with the videos? What kind of videos are these? Do you just need to move them to an MPG container? Or do you need to resize them? Or do you specifically need the msmpeg4v2 codec?
    • Darius
      Darius almost 12 years
      The files are taken off a DVR and are over 1GB 5 minutes clips. Yes the DVR software is pathetic at compression. The codec works since it takes 1GB files to roughly 7MB files which are more manageable. And when trying to run that batch I get "File was unexpected at this time" message.
    • slhck
      slhck almost 12 years
      I see. If you don't care about the resulting video codec and container, I'd suggest to use x264 as an encoder instead, which results in much better quality than MPEG-4. The command would be something like ffmpeg -i input.avi -c:v libx264 -crf 23 -s 640x480 output.mp4 where the CRF value sets the quality (less is better, more is worse, sane values from 19 to 24). See also: Convert old videos to have smaller sizes and What parameters should I be looking at to reduce the size of a .MOV file?
  • Darius
    Darius almost 12 years
    With this I get [NULL @ 000000000037e360] Unable to find a suitable output format for 'outputfile.avi.mpg;' outputfile.avi.mpg:: Invalid argument
  • Brett
    Brett almost 12 years
    Need to remove that semi-colon from the end of the line