|
|
更新:使用openssl,加入了对AES加密视频的解密功能;限制了curl启动数量,防止卡顿;支持了聚合源m3u8的解析;完善了地址解析
- @echo off
- setlocal enabledelayedexpansion
- set MAX_CURL=16
- set /p m3u8=M3U8:
- if exist output.mp4 del /f /q output.mp4
- if exist tmp del /f /q tmp\*&rd tmp
- set strlen=0
- set "m3u8tmp=%m3u8%"
- :count
- set "m3u8tmp=%m3u8tmp:~1%"
- set /a strlen+=1
- if "%m3u8tmp%" neq "" goto count
- for /l %%i in (%strlen%,-1,0) do (
- if "!m3u8:~%%i,1!" equ "/" (
- set "home=!m3u8:~0,%%i!"
- goto exitfor
- )
- )
- :exitfor
- set "m3u8=%m3u8://=\\%"
- for /f "tokens=1 delims=/" %%i in ("%m3u8%") do (
- set "mtmp=%%i"
- set "root=!mtmp:\\=//!"
- )
- set "m3u8=%m3u8:\\=//%"
- echo %m3u8%|find "//">nul
- if not errorlevel 1 for /f "tokens=1 delims=/" %%i in ("%m3u8%") do set pt=%%i
- md tmp
- cd tmp
- for /f "delims=" %%i in ("%m3u8%") do echo Downloading %%~nxi...
- curl -L --ssl-revoke-best-effort -o main.m3u8 "%m3u8%"
- echo Done.
- echo Downloading video parts...
- set counter=0
- set "key="
- set "IV="
- set "seq="
- set dic=0123456789ABCDEF
- set chosenM3u8=main.m3u8
- :reDownload
- for /f "delims=" %%i in (%chosenM3u8%) do (
- call :waitCurl
- set line=%%i
- if "!line:~0,1!" neq "#" (
- set /a counter+=1
- if "!line:~0,2!" equ "//" (
- start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%pt%!line!"
- ) else if "!line:~0,1!" equ "/" (
- start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%root%!line!"
- ) else (
- echo !line!|find "//">nul
- if not errorlevel 1 (
- start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "!line!"
- ) else (
- start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%home%/!line!"
- )
- )
- if "!key!" neq "" (
- if "!IV!" equ "" (
- set /a cseq=!seq!+!counter!-1
- call :toHex !cseq!
- set IV=!hexOut!
- echo !counter!%%~xi^|!key!^|!IV!>>decrypt.txt
- set "IV="
- ) else (echo !counter!%%~xi^|!key!^|!IV!>>decrypt.txt)
- echo file '!counter!_decrypted%%~xi'>>filelist.txt
- ) else (echo file '!counter!%%~xi'>>filelist.txt)
- ) else if "!line:~0,10!" equ "#EXT-X-KEY" (
- echo !line!|find /i "METHOD=NONE">nul
- if not errorlevel 1 (
- set "key="
- set "IV="
- )
- echo !line!|find /i "METHOD=AES-128">nul
- if not errorlevel 1 (
- set "lastValue="
- for %%j in (!line!) do (
- if "!lastValue!" equ "URI" (
- set /a counterTmp=!counter!+1
- set "URI=%%~j"
- if "!URI:~0,2!" equ "//" (
- curl -L --ssl-revoke-best-effort -o key!counterTmp!.key "%%pt!URI!"
- ) else if "!URI:~0,1!" equ "/" (
- curl -L --ssl-revoke-best-effort -o key!counterTmp!.key "%root%!URI!"
- ) else (
- echo !URI!|find "//">nul
- if not errorlevel 1 (
- curl -s -L --ssl-no-revoke -o key!counterTmp!.key "!URI!"
- ) else (
- curl -s -L --ssl-no-revoke -o key!counterTmp!.key "%home%/!URI!"
- )
- )
- certutil -encodehex -f key!counterTmp!.key hexkey!counterTmp!.txt 4
- set /p keyRead=<hexkey!counterTmp!.txt
- set "key="
- for %%m in (!keyRead!) do set key=!key!%%m
- )
- if "!lastValue!" equ "IV" (
- set "IV=%%j"
- set IV=!IV:0x=!
- set IV=!IV:0X=!
- )
- set lastValue=%%~j
- )
- set "lastValue="
- )
- ) else if "!line:~0,21!" equ "#EXT-X-MEDIA-SEQUENCE" (
- for /f "tokens=2 delims=:" %%j in ("!line!") do set seq=%%j
- ) else if "!line:~0,17!" equ "#EXT-X-STREAM-INF" (
- set "lastValue="
- for %%j in (!line!) do (
- if "!lastValue!" equ "RESOLUTION" (
- set /a counterTmp=counter+1
- echo !counterTmp!.%%j>>m3u8list.txt
- )
- set lastValue=%%j
- )
- set "lastValue="
- )
- )
- :monitor
- tasklist|find /I "curl.exe">nul
- if errorlevel 1 goto end
- timeout /t 1 /nobreak>nul
- goto monitor
- :end
- echo Done.
- if exist m3u8list.txt (
- del /f /q filelist.txt
- type m3u8list.txt
- set /p chosenM3u8=Choose the resolution:
- set chosenM3u8=!chosenM3u8!.m3u8
- del /f /q m3u8list.txt
- goto reDownload
- )
- echo Generating video...
- cd..
- echo Decrypting video...
- for /f "tokens=1,2,3 delims=^|" %%i in (tmp\decrypt.txt) do openssl aes-128-cbc -d -in tmp\%%i -out tmp\%%~ni_decrypted%%~xi -nosalt -K %%j -iv %%k
- ffmpeg -f concat -safe 0 -i tmp\filelist.txt -c copy output.mp4
- echo Done.
- pause
- exit
- :toHex
- set "num=%~1"
- set "hexOut="
- :loop
- if !num! gtr 0 (
- set /a mod=num%%16
- set /a num/=16
- for %%k in (!mod!) do set bit=!dic:~%%k,1!
- set hexOut=!bit!!hexOut!
- goto loop
- )
- if "!hexOut!" equ "" set hexOut=0
- set hexOut=00000000!hexOut!
- set hexOut=!hexOut:~-8!
- set hexOut=000000000000000000000000!hexOut!
- exit /b
- :waitCurl
- :loop1
- set curlCounter=0
- for /f "delims=" %%l in ('tasklist^|find /i /c "curl.exe"') do set curlCounter=%%l
- set /a curlCounter+=1
- if !curlCounter! gtr %MAX_CURL% (
- timeout /t 1 /nobreak>nul
- goto loop1
- )
- exit /b
复制代码 |
|