Tuesday, September 13, 2011

Batch converting with ffmpeg in Windows

 

As mentioned in my earlier post, ffmpeg is the best tool to convert media files. However sometimes you need to convert a collection files in one go. Of course you can use various gui tools for ffmpeg, but I like to  keep things simple (or complicated, depending on the context).

avs, converter, s icon

Basically ffmpeg can only process 1 file at a time from the command line. Linux/Mac users have powerful bash script at their disposal, but do not underestimate Windows Smile

You probably already have a batch file to convert a single file (which contains all the required parameters for ffmpeg). What we need to do is add an additional batch file which will call “converting” batch file.

So, say you want to convert all .mov files in a folder to .mp3. Let’s start by our converting batch file.

convert-to-mp3.bat

IF EXIST "%1.mp3" GOTO exit

@echo Conversion for %1 started on %DATE% %TIME%
ffmpeg -b 128k -i %1 %1.mp3

:exit
@echo %1.mp3 already exists

What does it do: Checks if output exists, if not then converts the input file to .mp3 using ffmpeg.

Next step is to create the batch file which will call the above batch file.

Startconvert.bat

for %%i IN (*.mov) DO (convert-to-mp3.bat "%%i")
pause

As you can see this is a simple for loop calling your converting batch file.

To just convert all the .mov files to mp3, start startconvert.bat and you will be fine.

3 comments:

Anonymous said...

Nice tip, exactly what I needed for my conversion from MP4 to MP3!

pcshare.com said...

I've tried your command by just substituting the file extension and wouldn't work for MP4 to FLV, but I got the below to work:

I did a batch conversion using your parameters for MP4 to FLV. Used:

MP4_TO_FLV.BAT
---------------------------
IF EXIST "%1.flv" GOTO exit
ffmpeg -i %1 -ar 44100 -ab 96 -f flv %1.flv
:exit

BATCH_MP4_TO_FLV.BAT
---------------------------------------
for %%i IN (*.mp4) DO (mp4_to_flv.bat "%%i")



Only thing is the file name contains the old MP4 prior to FLV extension, ie myfile.mp4.flv. Manually removing ".mp4" from 90 files is not fun.





PC Share

Anonymous said...

Thank you very much! I used your conversion template batch program to convert .wtv Windows Media Center DVR recorded files to simple .mpg with no re-encoding. Then I can watch the .mpgs on any of my home networked PCs, even if they don't play .wtv files natively (such as my 2 Linux Mint laptops).

However, I had to remove the "" (double quotes) from the wild-card step in the batch file that calls the conversion batch. My conversion batch is:

IF EXIST %1.mpg GOTO exit

@echo Conversion for %1 started on %DATE% %TIME%
rem ffmpeg -b 128k -i %1 %1.mp3
"f:\recorded tv\bin\ffmpeg" -i %1 -vcodec copy -acodec copy -f dvd %1.mpg >cnvrtlog.txt 2>&1

:exit
@echo %1.mpg already exists

And my calling batch file is:

for %%i IN (*.wtv) DO (wtv2mpg.bat "%%i")
pause

My only problem is the same as the last commenter's -- it leaves the original extension (in this case, .wtv) as part of the final .mpg filename. This could be a problem when the files are copied to or played on different systems.

I am not a programmer, just a curious semi-technical user. So I'm not sure how to stop the inclusion of the '.wtv' in the converted output filename. It's probably something simple and obvious that I'm not seeing. I will try some things and report back. I post this comment in the hopes that it helps others like me! And, once again, thanks for the original post.