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. General;InputFile=%CompleteName%\r\nOutputFile=%FileName%.avi\r\nFileName=%FileName%\r\nFileSize=%FileSize%\r\nDuration=%Duration%\r\nVideo;VideoBitRate=%BitRate%\r\nAudio;AudioBitRate=%BitRate%
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).
@setlocal enableextensions enabledelayedexpansion
@echo off
rem see http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
if [%1]==[] goto :eof
mediainfo --Inform=file://template.txt "%1" > value.txt
for /f "tokens=1,2 delims==" %%A in (value.txt) do (
if %%A==InputFile set INPUT_FILE=%%B
if %%A==OutputFile set OUTPUT_FILE=%%B
if %%A==FileName set FILE_NAME=%%B
if %%A==FileSize set FILE_SIZE=%%B
if %%A==Duration set DURATION=%%B
if %%A==VideoBitRate set VIDEO_BIT_RATE=%%B
if %%A==AudioBitRate set AUDIO_BIT_RATE=%%B
)
echo "InputFile:" %INPUT_FILE%
echo "OutputFile:" %OUTPUT_FILE%
echo "FileName:" %FILE_NAME%
echo "FileSize:" %FILE_SIZE%
echo "Duration:" %DURATION%
echo "VideoBitRate:" %VIDEO_BIT_RATE%
echo "AudioBitRate:" %AUDIO_BIT_RATE%
set FILE_SIZE_MB=%2
if "%FILE_SIZE_MB%"=="" set /A FILE_SIZE_MB=FILE_SIZE/1024/1024
set AUDIO_BIT_RATE_K=%3
if "%AUDIO_BIT_RATE_K%"=="" set /A AUDIO_BIT_RATE_K=AUDIO_BIT_RATE/1000
set /A DURATION_IN_SECONDS=DURATION/1000
set /A TOTAL_BIT_RATE_K=FILE_SIZE_MB*8192/DURATION_IN_SECONDS
set /A VIDEO_BIT_RATE_K=TOTAL_BIT_RATE_K-AUDIO_BIT_RATE_K
set /A VIDEO_BIT_RATE_K=VIDEO_BIT_RATE_K*1026/1000
echo "FileSizeMB:" %FILE_SIZE_MB%MB
echo "DurationInSec:" %DURATION_IN_SECONDS%
echo "TotalBitRate:" %TOTAL_BIT_RATE_K%k
echo "VideoBitRate:" %VIDEO_BIT_RATE_K%k
echo "AudioBitRate:" %AUDIO_BIT_RATE_K%k
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 && \
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.