@echo off
:head
set home=%~dp0


::创建相关目录及文件
if not exist "%home%cache" (md "%home%cache")
if not exist "%home%video" (md "%home%video")


::删除文件
del /Q %home%cache\*
del /Q %home%video_ts_file\*


echo.
set /p key=请输入解密后的32位key：
if not "%key:~31,1%"=="" (
	if "%key:~32,1%"=="" (
		echo.
		echo key长度正确！
		) else (
		echo.
		echo 无效的key长度，key的长度为32位16进制，请重新输入！
		echo.
		pause&cls&goto head)
) else (
echo.
echo 无效的key长度，key的长度为32位16进制，请重新输入！
echo.
pause&cls&goto head
)

echo.
set /p "video_name=请定义视频名称："


:check
::下载加密的TS文件
for %%a in (%home%ts_url.txt) do (
	if "%%~za" equ "0" (
		echo.
		echo 错误：ts_url.txt文件未填写，请先填写！
		echo.
		pause&goto check
	) else (
		findstr ..ts %home%ts_url.txt >> %home%cache\video_ts_url.txt
		%home%software\wget.exe -i %home%cache\video_ts_url.txt -P video_ts_file\
	)
)


::将TS文件解密
cd %home%video_ts_file\
for /f "tokens=9 delims=/" %%i in (%home%cache\video_ts_url.txt) do (
	start /min openssl aes-128-ecb -d -in "%%i" -out "new_%%i" -K %key%
	echo file '%home%video_ts_file\new_%%i'>> %home%cache\filelist.txt
)


::合并TS文件
%home%software\ffmpeg.exe -f concat -safe 0 -i %home%cache\filelist.txt -c copy %home%video\test.mp4
cd %home%video\
rename test.mp4 "%video_name%.mp4"


if %errorlevel%==0 (
echo.
echo 视频解密完成。
echo.
)


::删除文件
del /Q %home%cache\*
del /Q %home%video_ts_file\*


cd %home%
pause&cls&goto head