@echo off

:: Script to make a mpc with audio for ps2.
:: More platforms comig soon:tm:
::
:: Usage: 
:: doit_ps2_mpc input_movie_filename output_mpc_filename
::
:: Where:
::	<input_movie_filename> is a path to some video file compatible
::		with the ffmpeg tool.
::  <output_mpc_filename> is a path or filename

setlocal

set TOOLSROOT=%~dp0\Toolsbin
set FILENAME=%~dpnx1
set OUTPUT=%~dpnx2

set TEMPDIR=temp

mkdir %TEMPDIR%

pushd %TEMPDIR%
	set FPS=29.97
	
	set AUDIO_WAVNAME=temp_audio.wav
	set AUDIO_ASFNAME=temp_audio.asf
	set VIDEONAME=noaudio.mpc

	set FRAMESDIR=video_frames
	
	mkdir %FRAMESDIR%
	
	:: extract audio from source to wave,
	:: and extract the frames of the input
	%TOOLSROOT%\ffmpeg -i %FILENAME% %AUDIO_WAVNAME%
	%TOOLSROOT%\ffmpeg -i %FILENAME% "%FRAMESDIR%\%%1d.png"
	
	:: Encode audio with sx
	%TOOLSROOT%\sx -v3 -fps%FPS% -ps2stream -eaxa_blk %AUDIO_WAVNAME%=%AUDIO_ASFNAME%
	
	:: Encode initial movie (without audio), using gx
	%TOOLSROOT%\gx -v3 -sort1 -mpc -scale512,448 -v3 -fps%FPS% -gop15,3 %FRAMESDIR%\*.png =%VIDEONAME%
	
	:: Use chimp to interleave asf and mpc together into the final output file
	%TOOLSROOT%\chimp -v3 -size+=64 %VIDEONAME% %AUDIO_ASFNAME% -=%OUTPUT%
	
	:: remove temporary files we don't need anymore
	del /f /s /q %VIDEONAME% > nul
	del /f /s /q %AUDIO_ASFNAME% > nul
	del /f /s /q %AUDIO_WAVNAME% > nul
	del /f /s /q %FRAMESDIR%\* > nul
	rmdir %FRAMESDIR%
popd

:: Remove temporary directory
rmdir %TEMPDIR%

:: et voila!
endlocal