@echo off
rem ------------------------------Read me--------------------------------------
rem Revision : 1.0.5
rem     Date : 2012-09-30
rem  Created : 2012-05-19
rem  Purpose : multimedia converter based on the FFmpeg libraries
rem            FFmpeg no the stop time offset
rem    Usage : Create a batch file as follows
rem ----set input output file name----
rem set ifn="C:\path\file.avi"
rem set ofn="file"
rem set extn=avi
rem ----Start the conversion----------
rem call mConversion %ifn% %ofn% %extn% 00:15:00.500 00:15:05.500 01
rem call mConversion %ifn% %ofn% %extn% 00:25:00.500 00:25:05.500 02
rem call mConversion %ifn% %ofn% %extn% 00:35:00.500 00:35:05.500 03
rem ------------------------------get time-------------------------------------
set h=%time:~0,2%
if "%h:~0,1%"==" " set "h=0%h:~1%"
set m=%time:~3,2%
set s=%time:~6,2%
set hms=%h%%m%%s%
rem ------------------------------input parameters-----------------------------
set         inFile=%1
set        outFile=%2_%hms%_%6.%3
set      start_pos=%4
set        end_pos=%5

rem set video_bit_rate=-b:v 1500k
rem set audio_bit_rate=-b:a 64k
rem set     frame_size=-s 720x404
rem set         aspect=-aspect 16:9
set    logFileName=%2_conversion.log

rem for copy
set    video_codec=-c:v copy
set    audio_codec=-c:a copy

rem for H264
rem set    video_codec=-c:v libx264
rem set    audio_codec=-c:a libvo_aacenc

rem for mpeg4 xvid
rem set    video_codec=-c:v mpeg4
rem set fourcc_tag_out=-tag:v XVID
rem set    audio_codec=-c:a libmp3lame

rem set         inFile="D:\TDDOWNLOAD\01.mp4"
rem set        outFile="%hms%.avi"
rem set      start_pos=00:40:10.000
rem set        end_pos=00:40:11.000
rem ------------------------------calctime-------------------------------------
if "%end_pos%"    NEQ "" (call Lib_CalculateTotalTime %start_pos% %end_pos% display)
if "%total_time%" NEQ "" (set total_time=-t %total_time%)
if "%start_pos%"  NEQ "" (set start_pos=-ss %start_pos%)
rem ------------------------------make cmd-------------------------------------
set cmd=ffmpeg
set cmd=%cmd% %start_pos%
set cmd=%cmd% -y -i %inFile% %total_time%
set cmd=%cmd% %audio_codec% %audio_bit_rate%
set cmd=%cmd% %video_codec% %fourcc_tag_out% %video_bit_rate%
rem set cmd=%cmd% -g 12 -bf 2 -qcomp 0.2 -dc 10 -mbd 2 %frame_size% %aspect%
set cmd=%cmd% %frame_size% %aspect%
set cmd=%cmd% %outFile%
rem ------------------------------run cmd--------------------------------------
set begin_time=%date:~0,10% %time:~0,11%
@echo on
%cmd%
@echo off
set end_time=%date:~0,10% %time%
rem ------------------------------write to log---------------------------------
echo ---%date:~0,10% %time:~0,11%---------------------------------------------->>%logFileName%
echo %cmd%>>%logFileName%
echo.>>%logFileName%
echo         inFile = %inFile%>>%logFileName%
echo        outFile = %outFile%>>%logFileName%
if "%video_bit_rate%" NEQ "" (echo Video bit rate = %video_bit_rate:~5,10%>>%logFileName%)
if "%audio_bit_rate%" NEQ "" (echo Audio bit rate = %audio_bit_rate:~5,10%>>%logFileName%)
echo.>>%logFileName%
echo      start_pos = %start_pos:~4,12%>>%logFileName%
echo        end_pos = %end_pos%>>%logFileName%
echo     total_time = %total_time:~3,12%>>%logFileName%
echo.>>%logFileName%
echo     begin time = %begin_time%>>%logFileName%
echo       end time = %end_time%>>%logFileName%
rem ------------------------------put message----------------------------------
echo -------The end of the conversion------------------------------------------
rem echo         inFile = %inFile%
rem echo        outFile = %outFile%
if "%video_bit_rate%" NEQ "" (echo  video bit rate = [v:%video_bit_rate:~5,10%] [a:%audio_bit_rate:~5,10%])
echo      video time = [%start_pos:~4,12% - %end_pos%] = %total_time:~3,12%
call Lib_CalculateTotalTime %begin_time:~11,11% %end_time:~11,11%
echo conversion time = [%begin_time:~11,11%  - %end_time:~11,11% ] = %total_time:~0,12%
echo --------------------------------------------------------------------------
rem ------------------------------clear tmp var--------------------------------
set h=
set m=
set s=
set hms=
set inFile=
set outFile=
set start_pos=
set end_pos=
set logFileName=
set video_codec=
set audio_codec=
set video_bit_rate=
set audio_bit_rate=
set frame_size=
set aspect=
set cmd=
set begin_time=
set end_time=
set total_time=
rem ---------------------------------------------------------------------------
rem pause>nul
