How to automatically convert mp4 video file to divx format
I developed this solution because I was tired for tedious tasks related to conversion of nowadays mp4 files to avi format readable by my Panasonic plasma TV. These tasks requires getting important parameteres from mp4 files like video and audio bit rates and concluding long command line for ffmpeg utility. One day last year I decided to free myself from it due to automation. I made a lot of searching on internet, I spent almost two days by compiling all gathered information into one package and there is my solution. It requires just one additional utility – MediaInfo, which is freely available from SourceForge site [be aware that instalation contains additional adware – toolbars, I advise you to carefully deselect it during instalation process]. MediaInfo utility has neccessary CLI features which will help you to develop automated solution. The documentation is available here. Next step is creation of template file which defines attributes of video file required for video conversion. The template file has structure of old fashioned INI files, call it template.txt. Note, it is a just single line.
Third step requires such coding capabilities, you need to create batch file. The batch file I created accepts two parameteres, the first one, original video file (in mp4) is mandatory, and the second one, output file size is voluntary. Without second parameter, the batch file assume that you want to make video conversion from mp4 to avi (divx) file with almost the same file size comparing to original one. So, second parameter gives you option to shrink or extend file size, if you want. The batch file in the first part call MediaInfo utility to create INI file called value.txt according to structure defined in template.txt file (and mentioned two paragraphs above). In middle part, batch file reads all data from value.txt, makes conversion to appropriate units and save these values into variables. The last section is about calling ffmpeg utility with two passes which makes desired conversion. See below content of my solution (I saved it under name ff.bat).INI |copy code |?
Winbatch |copy code |?
01 @setlocal enableextensions enabledelayedexpansion 02 @echo off 03 rem see http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide 04 05 if [%1]== goto :eof 06 07 mediainfo --Inform=file://template.txt "%1" > value.txt 08 09 for /f "tokens=1,2 delims==" %%A in (value.txt) do ( 10 if %%A==InputFile set INPUT_FILE=%%B 11 if %%A==OutputFile set OUTPUT_FILE=%%B 12 if %%A==FileName set FILE_NAME=%%B 13 if %%A==FileSize set FILE_SIZE=%%B 14 if %%A==Duration set DURATION=%%B 15 if %%A==VideoBitRate set VIDEO_BIT_RATE=%%B 16 if %%A==AudioBitRate set AUDIO_BIT_RATE=%%B 17 ) 18 19 echo "InputFile:" %INPUT_FILE% 20 echo "OutputFile:" %OUTPUT_FILE% 21 echo "FileName:" %FILE_NAME% 22 echo "FileSize:" %FILE_SIZE% 23 echo "Duration:" %DURATION% 24 echo "VideoBitRate:" %VIDEO_BIT_RATE% 25 echo "AudioBitRate:" %AUDIO_BIT_RATE% 26 27 set FILE_SIZE_MB=%2 28 if "%FILE_SIZE_MB%"=="" set /A FILE_SIZE_MB=FILE_SIZE/1024/1024 29 30 set AUDIO_BIT_RATE_K=%3 31 if "%AUDIO_BIT_RATE_K%"=="" set /A AUDIO_BIT_RATE_K=AUDIO_BIT_RATE/1000 32 33 set /A DURATION_IN_SECONDS=DURATION/1000 34 set /A TOTAL_BIT_RATE_K=FILE_SIZE_MB*8192/DURATION_IN_SECONDS 35 set /A VIDEO_BIT_RATE_K=TOTAL_BIT_RATE_K-AUDIO_BIT_RATE_K 36 set /A VIDEO_BIT_RATE_K=VIDEO_BIT_RATE_K*1026/1000 37 38 echo "FileSizeMB:" %FILE_SIZE_MB%MB 39 echo "DurationInSec:" %DURATION_IN_SECONDS% 40 echo "TotalBitRate:" %TOTAL_BIT_RATE_K%k 41 echo "VideoBitRate:" %VIDEO_BIT_RATE_K%k 42 echo "AudioBitRate:" %AUDIO_BIT_RATE_K%k 43 44 ffmpeg -y -i %INPUT_FILE% -f avi -vtag DIVX -c:v mpeg4 -preset medium -b:v %VIDEO_BIT_RATE_K%k -pass 1 -an NUL && \ 45 ffmpeg -y -i %INPUT_FILE% -f avi -vtag DIVX -c:v mpeg4 -preset medium -b:v %VIDEO_BIT_RATE_K%k -pass 2 -c:a mp3 -b:a %AUDIO_BIT_RATE_K%k -ac 2 -ar 48k %OUTPUT_FILE
The last thing you do it is calling ff.bat from command line or do it in probable more convenient and user friendly way: just drop mp4 file onto ff.bat in Windows Explorer. Note that solution will work only if you had to have path to ffmpeg and mediainfo utilities in system %PATH% variable. Otherwise you need to redefine path to these utlities in the content above to right destionation. Good luck and make a lot of fun.