@echo off
echo 正在处理文件:%~1
rem ~可以确保获取到的参数不包含多余的引号
rem 判断是否传递了参数
if "%~1"=="" (
echo 未传递任何参数,请提供文件或文件夹路径。
exit /b 1
)
rem 获取参数的属性
set "attribute=%~a1"
set "fpath=%~1"
rem 判断是否为文件夹
if "%attribute:~0,1%"=="d" (
echo "%~1 是一个文件夹目录。"
rem 这里可以添加处理文件夹的逻辑
rem set "fpath=%~1"
) else if "%attribute:~0,1%"=="-" (
echo "%~1 是一个文件路径。"
rem 这里可以添加处理文件的逻辑
set "fpath=%~dp1"
) else (
echo "无法识别 %~1 是文件还是文件夹。"
exit /b 1
)
echo 切换目录到 "%fpath%"
pushd "%fpath%"
if errorlevel 1 (
echo "Error: Directory %~1 not found."
pause
exit /b
)
for %%i in (.) do set "curr=%%~ni"
echo "使用最后一级目录 %curr% 作为文件名"
rem 判断文件是否存在,如果存在询问用户是否删除重新生成
setlocal enableextensions enabledelayedexpansion
rem 检查 index.m3u8 文件是否存在
rem set "m3u8=index.m3u8"
set "m3u8=%curr%.m3u8"
if not exist "!m3u8!" (
set "m3u8=index.m3u8"
)
echo "!m3u8!"
rem if exist "%curr%.mp4" (del /f "%curr%.mp4")
rem 定义目标文件路径,你可以根据实际情况修改
set "target_file=%curr%.mp4"
rem 检查目标文件是否存在
if exist "%target_file%" (
:ask_user
set /p "choice=目标文件 %target_file% 已存在,是否重新生成?(Y/N)(回车=是): "
:: 检测用户输入
if /i "!choice!"=="" (
set "choice=Y" :: 如果用户直接回车,则设置默认值为 Y
)
if /i "!choice!"=="Y" (
rem 用户选择重新生成,删除原有文件
del /f "%target_file%"
echo "已删除原有文件 %target_file%。"
) else if /i "!choice!"=="N" (
echo 你选择不重新生成,程序将退出。
goto ask_user3
pause
exit /b
) else (
echo 无效的输入,请输入 Y 或 N。
goto ask_user
)
)
rem 如果 newindex.m3u8 文件存在则直接合并文件
if exist "newindex.m3u8" (
:ask_user2
set /p "choice=目标文件 newindex.m3u8 已存在,是否重新生成?(Y/N)(回车=是): "
echo !choice!
:: 检测用户输入
if /i "!choice!"=="" (
set "choice=Y" :: 如果用户直接回车,则设置默认值为 Y
)
if /i "!choice!"=="Y" (
rem 用户选择重新生成,删除原有文件
del /f newindex.m3u8
del /f links.list
echo 已删除原有文件 newindex.m3u8。
) else if /i "!choice!"=="N" (
echo 你选择不重新生成,直接跳转到合并过程。
goto ffmpeg_merge
) else (
echo 无效的输入,请输入 Y 或 N。
goto ask_user2
)
)
set /a b=0
set /a fn=0
rem 遍历 index.m3u8 文件的每一行
for /f "usebackq delims=" %%a in ("!m3u8!") do (
rem 去除可能的引号
set "url=%%a"
set "url=!url:"=!"
rem 判断 url 是否为有效的文件链接(简单判断是否以 http 或 https 开头)
if "!url:~0,4!"=="http" (
rem 提取文件名
for %%f in ("!url!") do set "filename=%%~nxf"
if exist "!filename!" (
rem 判断文件大小 for /f %%i in ('dir /b %%a') do if 0 lss %%~zi (echo file %%a >> newindex.m3u8)
echo !filename! 文件已存在!
) else (
echo !url! >> links.list
)
echo !filename! >> newindex.m3u8
) else (
if "!b!"=="0" (
if "!url:~0,30!"=="#EXT-X-KEY:METHOD=AES-128,URI=" (
echo "!url!"
set "uri=!url:~30!"
set "uri=!uri:"=!"
echo "uri=!uri!"
if "!uri:~0,4!"=="http" (
if not exist "!uri!" (
echo !uri! >> links.list
)
set /a b+=1
echo #EXT-X-KEY:METHOD=AES-128,URI=key.key >> newindex.m3u8
rem 输出提取到的 URI
echo 提取到的 URI 是: !uri!
)
)
if "!b!"=="0" (
rem echo !url! 不是有效的文件链接,跳过
echo !url! >> newindex.m3u8
)
) else (
rem echo !url! 不是有效的文件链接,跳过
echo .
echo !url! >> newindex.m3u8
)
)
set /a fn+=1
)
rem 设置包含下载链接的文本文件路径
set "links_file=links.list"
rem 设置下载文件保存的目录
set "download_dir=%cd%"
if exist "%links_file%" (
rem 调用 aria2c 批量下载文件
aria2c -i "%links_file%" -d "%download_dir%" -x 16 -s 8 -j 10 -c
echo 调用 aria2c 批量下载完成!
)
:ffmpeg_merge
rem 如果 key.key 文件不存在,ffmpeg 不会报错,会继续执行
echo ffmpeg merge begin
ffmpeg -protocol_whitelist "file,crypto" -allowed_extensions ALL -threads auto -i "newindex.m3u8" -c copy -bsf:a aac_adtstoasc "%curr%.mp4"
rem ffmpeg -protocol_whitelist "file,crypto" -allowed_extensions ALL -i "newindex.m3u8" -c copy -bsf:a aac_adtstoasc "output.mp4"
rem ffmpeg -protocol_whitelist "file,crypto" -allowed_extensions ALL -threads auto -i "newindex.m3u8" -c copy "%curr%.mp4"
rem ffmpeg -protocol_whitelist "file,crypto" -allowed_extensions ALL -i "newindex.m3u8" -c:v libx264 -c:a aac "%curr%.mp4"
rem ffmpeg -f concat -safe 0 -protocol_whitelist "file,concat" -i newindex.m3u8 -c copy %curr%.mp4
if errorlevel 1 (
echo Error: ffmpeg failed to merge files.
) else (
echo "%curr%.mp4 合并成功!"
:ask_user3
set /p "choice=是否删除下载的切片文件?(Y/N)(回车=Y): "
echo "!choice!"
:: 检测用户输入
if /i "!choice!"=="" (
set "choice=Y" :: 如果用户直接回车,则设置默认值为 Y
)
if /i "!choice!"=="Y" (
rem 用户选择重新生成,删除原有文件
echo "!m3u8!"
del /f *.ts
rem 遍历 index.m3u8 文件的每一行
for /f "usebackq delims=" %%a in ("!m3u8!") do (
rem 去除可能的引号
set "url=%%a"
set "url=!url:"=!"
rem 判断 url 是否为有效的文件链接(简单判断是否以 http 或 https 开头)
if "!url:~0,4!"=="http" (
rem 提取文件名
for %%f in ("!url!") do set "filename=%%~nxf"
echo !filename!
if exist "!filename!" (
del /f "!filename!"
)
)
)
if exist "newindex.m3u8" (
del /f newindex.m3u8
)
if exist "links.list" (
del /f links.list
)
if exist "index.html" (
del /f index.html
)
if exist "index.m3u8" (
del /f index.m3u8
)
if exist "index.1.html" (
del /f index.1.html
)
if exist "url.txt" (
del /f url.txt
)
if exist "!m3u8!" (
del /f "!m3u8!"
)
) else if /i "!choice!"=="N" (
echo 你选择不删除,程序即将退出!
exit /b 0
) else (
echo 无效的输入,请输入 Y 或 N。
goto ask_user3
)
)
endlocal
pause