@echo off
set ffmpeg="%CD%\util\ffmpeg.exe"
set BinkConv="%CD%\util\BinkConv.exe"
set MediaInfo="%CD%\util\MediaInfo\MediaInfo.exe"
set cws2fws="%CD%\util\cws2fws.exe"
set Midi2Mtx="%CD%\util\Midi2Mtx.exe"
set Mtx2Midi="%CD%\util\Mtx2Midi.exe"
set file=%1
if /I "%~x1"==".avi" goto avi
if /I "%~x1"==".mkv" goto wav
if /I "%~x1"==".flv" goto mp4
if /I "%~x1"==".swf" goto mp4
if /I "%~x1"==".iflv" goto mp4
if /I "%~x1"==".mp4" goto mp4
if /I "%~x1"==".mov" goto wav
if /I "%~x1"==".m4v" goto mp4
if /I "%~x1"==".psp" goto mp4
if /I "%~x1"==".ogm" goto webm
if /I "%~x1"==".webm" goto webm
if /I "%~x1"==".ogv" goto webm
if /I "%~x1"==".wmv" goto wmv
rem if /I "%~x1"==".asf" goto wmv
rem if /I "%~x1"==".rm" goto avi
if /I "%~x1"==".mpg" goto mpg
if /I "%~x1"==".mpeg" goto mpg
if /I "%~x1"==".mpe" goto mpg
if /I "%~x1"==".dat" goto mpg
if /I "%~x1"==".vob" goto vob
if /I "%~x1"==".3g2" goto 3gp
if /I "%~x1"==".3gp" goto 3gp
if /I "%~x1"==".smk" goto smk
if /I "%~x1"==".bik" goto smk
if /I "%~x1"==".ape" goto wav
if /I "%~x1"==".m4a" goto wav
if /I "%~x1"==".alac" goto wav
if /I "%~x1"==".aac" goto wav
if /I "%~x1"==".ac3" goto wav
if /I "%~x1"==".aiff" goto wav
if /I "%~x1"==".mpc" goto wav
if /I "%~x1"==".flac" goto wav
if /I "%~x1"==".mp2" goto wav
if /I "%~x1"==".mp3" goto wav
if /I "%~x1"==".oga" goto wav
if /I "%~x1"==".ogg" goto wav
if /I "%~x1"==".spx" goto wav
if /I "%~x1"==".wma" goto wav
if /I "%~x1"==".amr" goto wav
if /I "%~x1"==".xa" goto xa
if /I "%~x1"==".voc" goto wav
if /I "%~x1"==".caf" goto wav
if /I "%~x1"==".acm" goto wav
if /I "%~x1"==".aud" goto wav
if /I "%~x1"==".mid" goto mid
if /I "%~x1"==".mtx" goto mtx
if /I "%~x1"==".wav" goto wav1
%ffmpeg% -i %1 "%~dpn1.wav"
exit

:mp4
call :mediainfo
call :form
@echo extention: %ext%
%ffmpeg% -i %1 -map 0:0 -acodec copy -vn "%~dpn1_0.%ext%"
%ffmpeg% -i %1 -map 0:1 -acodec copy -vn "%~dpn1_1.%ext%"
rem %ffmpeg% -i %1 -map 0:0 -c copy "%~dpn1_0.%ext%"
rem %ffmpeg% -i %1 -map 0:1 -c copy "%~dpn1_1.%ext%"
 for /f "tokens=2 delims=," %%A in ('type value.txt') do set format=%%A
call :form
%ffmpeg% -i %1 -map 0:2 -acodec copy -vn "%~dpn1_2.%ext%"
for /f "tokens=3 delims=," %%A in ('type value.txt') do set format=%%A
call :form
%ffmpeg% -i %1 -map 0:3 -acodec copy -vn "%~dpn1_3.%ext%"
goto clean

:avi
call :mediainfo
echo Stream 0
set id=0
for /f "tokens=1 delims=," %%A in ('type value.txt') do set format=%%A
call :form
%ffmpeg% -i %1 -map 0:%id% -acodec copy -vn "%~dpn1_%id%.%ext%"
echo Stream 1
set id=1
for /f "tokens=1 delims=," %%A in ('type value.txt') do set format=%%A
call :form
%ffmpeg% -i %1 -map 0:%id% -acodec copy -vn "%~dpn1_%id%.%ext%"
echo Stream 2
set id=2
for /f "tokens=2 delims=," %%A in ('type value.txt') do set format=%%A
call :form
%ffmpeg% -i %1 -map 0:%id% -acodec copy -vn "%~dpn1_%id%.%ext%"
echo Stream 3
set id=3
for /f "tokens=3 delims=," %%A in ('type value.txt') do set format=%%A
call :form
%ffmpeg% -i %1 -map 0:%id% -acodec copy -vn "%~dpn1_%id%.%ext%"
goto clean

:wmv
%ffmpeg% -i %1 -acodec copy -vn "%~dpn1.wma"
goto clean

:mpg
%ffmpeg% -i %1 -acodec copy -vn "%~dpn1.mp2"
goto clean

:vob
%ffmpeg% -i %1 -map 0:0 -acodec copy -vn "%~dpn1_2.mp2"
%ffmpeg% -i %1 -map 0:1 -acodec copy -vn "%~dpn1_2.mp2"
%ffmpeg% -i %1 -map 0:2 -acodec copy -vn "%~dpn1_2.mp2"
%ffmpeg% -i %1 -map 0:3 -acodec copy -vn "%~dpn1_3.mp2"
%ffmpeg% -i %1 -map 0:4 -acodec copy -vn "%~dpn1_4.mp2"
goto clean

:3gp
%ffmpeg% -i %1 -acodec copy -vn "%~dpn1.amr"
%ffmpeg% -i %1 -acodec copy -vn "%~dpn1.acc"
goto clean

:webm
%ffmpeg% -i %1 -acodec copy -vn "%~dpn1.ogg"
goto clean

:smk
rem %ffmpeg% -i %1 "%~dpn1.wav"
%BinkConv% %1 "%~dpn1.wav"
goto clean

:wav
%ffmpeg% -i %1 "%~dpn1.wav"
goto exit

:wav1
%ffmpeg% -i %1 "%~dpn1_pcm.wav"
goto exit

:xa
%ffmpeg% -i %1 -map 0:0 "%~dpn1_1.wav"
%ffmpeg% -i %1 -map 0:1 "%~dpn1_2.wav"
%ffmpeg% -i %1 -map 0:2 "%~dpn1_3.wav"
%ffmpeg% -i %1 -map 0:3 "%~dpn1_4.wav"
%ffmpeg% -i %1 -map 0:4 "%~dpn1_5.wav"
%ffmpeg% -i %1 -map 0:5 "%~dpn1_6.wav"
%ffmpeg% -i %1 -map 0:6 "%~dpn1_7.wav"
%ffmpeg% -i %1 -map 0:7 "%~dpn1_8.wav"
goto exit

:swf
%cws2fws% %1 "%~dpn1_unp.swf"
goto exit

:mid
%Midi2Mtx% %1
goto exit

:form
if "%format%" == "" goto clean
set ext="wav"
if "%format%" == "AAC" set ext=aac
if "%format%" == "MPEG Audio" set ext=mp3
if "%format%" == "PCM" set ext=wav
if "%format%" == "AC-3" set ext=ac3
GOTO :eof

:mediainfo
%mediainfo% --Inform="Audio;%%Format%%,%%FirstPacketOrder%%" %file% > value.txt
for /f "tokens=1 delims=," %%A in ('type value.txt') do set format=%%A
for /f "tokens=2 delims=," %%A in ('type value.txt') do set order=%%A
GOTO :eof

:clean
for %%G in ("%~dpn1*.wav";"%~dpn1*.mp3";"%~dpn1*.ogg";"%~dpn1*.wma";"%~dpn1*.aac";"%~dpn1*.mp2") do if %%~zG EQU 0 del "%%G"
for %%G in ("%~dpn1*.wav") do if %%~zG EQU 20 del "%%G"
del value.txt
exit