找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 235|回复: 1

[网络连接] [分享]m3u8视频下载合并脚本

[复制链接]
发表于 6 天前 | 显示全部楼层 |阅读模式
本帖最后由 LTSC1809 于 2026-11-9 02:39 编辑

自己要用到所以写了。分享一下。输入m3u8文件的网址。用到curl和ffmpeg
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /p m3u8=M3U8:
  4. if exist output.mp4 del /f /q output.mp4
  5. if exist tmp del /f /q tmp\*&rd tmp
  6. set strlen=0
  7. set "m3u8tmp=%m3u8%"
  8. :count
  9. set "m3u8tmp=%m3u8tmp:~1%"
  10. set /a strlen+=1
  11. if "%m3u8tmp%" neq "" goto count
  12. for /l %%i in (%strlen%,-1,0) do (
  13.         if "!m3u8:~%%i,1!" equ "/" (
  14.                 set "home=!m3u8:~0,%%i!"
  15.                 goto exitfor
  16.         )
  17. )
  18. :exitfor
  19. set "m3u8=%m3u8://=\\%"
  20. for /f "tokens=1 delims=/" %%i in ("%m3u8%") do (
  21.         set "mtmp=%%i"
  22.         set "root=!mtmp:\\=//!"
  23. )
  24. set "m3u8=%m3u8:\\=//%"
  25. md tmp
  26. cd tmp
  27. for /f "delims=" %%i in ("%m3u8%") do echo Downloading %%~nxi...
  28. curl -L --ssl-revoke-best-effort -o 1.m3u8 "%m3u8%"
  29. echo Done.
  30. echo Downloading video parts...
  31. set counter=0
  32. for /f "delims=" %%i in (1.m3u8) do (
  33.         set line=%%i
  34.         if "!line:~0,1!" neq "#" (
  35.                 set /a counter+=1
  36.                 if "!line:~0,1!" neq "/" (
  37.                         start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%home%/!line!"
  38.                 ) else (
  39.                         start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%root%!line!"
  40.                 )
  41.                 echo file '!counter!%%~xi'>>filelist.txt
  42.         )
  43. )
  44. :monitor
  45. tasklist|find /I "curl.exe">nul
  46. if errorlevel 1 goto end
  47. timeout /t 1 /nobreak>nul
  48. goto monitor
  49. :end
  50. echo Done.
  51. echo Generating video...
  52. cd..
  53. ffmpeg -f concat -safe 0 -i tmp\filelist.txt -c copy output.mp4
  54. echo Done.
  55. pause
  56. endlocal
  57. exit
复制代码
 楼主| 发表于 6 天前 | 显示全部楼层
更新:使用openssl,加入了对AES加密视频的解密功能;限制了curl启动数量,防止卡顿;支持了聚合源m3u8的解析;完善了地址解析
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set MAX_CURL=16
  4. set /p m3u8=M3U8:
  5. if exist output.mp4 del /f /q output.mp4
  6. if exist tmp del /f /q tmp\*&rd tmp
  7. set strlen=0
  8. set "m3u8tmp=%m3u8%"
  9. :count
  10. set "m3u8tmp=%m3u8tmp:~1%"
  11. set /a strlen+=1
  12. if "%m3u8tmp%" neq "" goto count
  13. for /l %%i in (%strlen%,-1,0) do (
  14.         if "!m3u8:~%%i,1!" equ "/" (
  15.                 set "home=!m3u8:~0,%%i!"
  16.                 goto exitfor
  17.         )
  18. )
  19. :exitfor
  20. set "m3u8=%m3u8://=\\%"
  21. for /f "tokens=1 delims=/" %%i in ("%m3u8%") do (
  22.         set "mtmp=%%i"
  23.         set "root=!mtmp:\\=//!"
  24. )
  25. set "m3u8=%m3u8:\\=//%"
  26. echo %m3u8%|find "//">nul
  27. if not errorlevel 1 for /f "tokens=1 delims=/" %%i in ("%m3u8%") do set pt=%%i
  28. md tmp
  29. cd tmp
  30. for /f "delims=" %%i in ("%m3u8%") do echo Downloading %%~nxi...
  31. curl -L --ssl-revoke-best-effort -o main.m3u8 "%m3u8%"
  32. echo Done.
  33. echo Downloading video parts...
  34. set counter=0
  35. set "key="
  36. set "IV="
  37. set "seq="
  38. set dic=0123456789ABCDEF
  39. set chosenM3u8=main.m3u8
  40. :reDownload
  41. for /f "delims=" %%i in (%chosenM3u8%) do (
  42.         call :waitCurl
  43.         set line=%%i
  44.         if "!line:~0,1!" neq "#" (
  45.                 set /a counter+=1
  46.                 if "!line:~0,2!" equ "//" (
  47.                         start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%pt%!line!"
  48.                 ) else if "!line:~0,1!" equ "/" (
  49.                         start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%root%!line!"
  50.                 ) else (
  51.                         echo !line!|find "//">nul
  52.                         if not errorlevel 1 (
  53.                                 start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "!line!"
  54.                         ) else (
  55.                                 start "" /b curl -s -L --ssl-no-revoke -o !counter!%%~xi "%home%/!line!"
  56.                         )
  57.                 )
  58.                 if "!key!" neq "" (
  59.                         if "!IV!" equ "" (
  60.                                 set /a cseq=!seq!+!counter!-1
  61.                                 call :toHex !cseq!
  62.                                 set IV=!hexOut!
  63.                                 echo !counter!%%~xi^|!key!^|!IV!>>decrypt.txt
  64.                                 set "IV="
  65.                         ) else (echo !counter!%%~xi^|!key!^|!IV!>>decrypt.txt)
  66.                         echo file '!counter!_decrypted%%~xi'>>filelist.txt
  67.                 ) else (echo file '!counter!%%~xi'>>filelist.txt)
  68.         ) else if "!line:~0,10!" equ "#EXT-X-KEY" (
  69.                 echo !line!|find /i "METHOD=NONE">nul
  70.                 if not errorlevel 1 (
  71.                         set "key="
  72.                         set "IV="
  73.                 )
  74.                 echo !line!|find /i "METHOD=AES-128">nul
  75.                 if not errorlevel 1 (
  76.                         set "lastValue="
  77.                         for %%j in (!line!) do (
  78.                                 if "!lastValue!" equ "URI" (
  79.                                         set /a counterTmp=!counter!+1
  80.                                         set "URI=%%~j"
  81.                                         if "!URI:~0,2!" equ "//" (
  82.                                                 curl -L --ssl-revoke-best-effort -o key!counterTmp!.key "%%pt!URI!"
  83.                                         ) else if "!URI:~0,1!" equ "/" (
  84.                                                 curl -L --ssl-revoke-best-effort -o key!counterTmp!.key "%root%!URI!"
  85.                                         ) else (
  86.                                                 echo !URI!|find "//">nul
  87.                                                 if not errorlevel 1 (
  88.                                                         curl -s -L --ssl-no-revoke -o key!counterTmp!.key "!URI!"
  89.                                                 ) else (
  90.                                                         curl -s -L --ssl-no-revoke -o key!counterTmp!.key "%home%/!URI!"
  91.                                                 )
  92.                                         )
  93.                                         certutil -encodehex -f key!counterTmp!.key hexkey!counterTmp!.txt 4
  94.                                         set /p keyRead=<hexkey!counterTmp!.txt
  95.                                         set "key="
  96.                                         for %%m in (!keyRead!) do set key=!key!%%m
  97.                                 )
  98.                                 if "!lastValue!" equ "IV" (
  99.                                         set "IV=%%j"
  100.                                         set IV=!IV:0x=!
  101.                                         set IV=!IV:0X=!
  102.                                 )
  103.                                 set lastValue=%%~j
  104.                         )
  105.                         set "lastValue="
  106.                 )
  107.         ) else if "!line:~0,21!" equ "#EXT-X-MEDIA-SEQUENCE" (
  108.                 for /f "tokens=2 delims=:" %%j in ("!line!") do set seq=%%j
  109.         ) else if "!line:~0,17!" equ "#EXT-X-STREAM-INF" (
  110.                 set "lastValue="
  111.                 for %%j in (!line!) do (
  112.                         if "!lastValue!" equ "RESOLUTION" (
  113.                                 set /a counterTmp=counter+1
  114.                                 echo !counterTmp!.%%j>>m3u8list.txt
  115.                         )
  116.                         set lastValue=%%j
  117.                 )
  118.                 set "lastValue="
  119.         )
  120. )
  121. :monitor
  122. tasklist|find /I "curl.exe">nul
  123. if errorlevel 1 goto end
  124. timeout /t 1 /nobreak>nul
  125. goto monitor
  126. :end
  127. echo Done.
  128. if exist m3u8list.txt (
  129.         del /f /q filelist.txt
  130.         type m3u8list.txt
  131.         set /p chosenM3u8=Choose the resolution:
  132.         set chosenM3u8=!chosenM3u8!.m3u8
  133.         del /f /q m3u8list.txt
  134.         goto reDownload
  135. )
  136. echo Generating video...
  137. cd..
  138. echo Decrypting video...
  139. 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
  140. ffmpeg -f concat -safe 0 -i tmp\filelist.txt -c copy output.mp4
  141. echo Done.
  142. pause
  143. exit
  144. :toHex
  145.         set "num=%~1"
  146.         set "hexOut="
  147.         :loop
  148.         if !num! gtr 0 (
  149.                 set /a mod=num%%16
  150.                 set /a num/=16
  151.                 for %%k in (!mod!) do set bit=!dic:~%%k,1!
  152.                 set hexOut=!bit!!hexOut!
  153.                 goto loop
  154.         )
  155.         if "!hexOut!" equ "" set hexOut=0
  156.         set hexOut=00000000!hexOut!
  157.         set hexOut=!hexOut:~-8!
  158.         set hexOut=000000000000000000000000!hexOut!
  159. exit /b
  160. :waitCurl
  161.         :loop1
  162.         set curlCounter=0
  163.         for /f "delims=" %%l in ('tasklist^|find /i /c "curl.exe"') do set curlCounter=%%l
  164.         set /a curlCounter+=1
  165.         if !curlCounter! gtr %MAX_CURL% (
  166.                 timeout /t 1 /nobreak>nul
  167.                 goto loop1
  168.         )
  169. exit /b
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-11-15 07:20

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表