推荐一个下载流式媒体的开源库

在浏览器中用F12打开调试,找到网络一页,刷新页面,找到.M3U8的请求,并找到相关的引用项,比如

罗小黑战记2:https://www.66ss.org/e/DownSys/play/?classid=20&id=27261&pathid1=0&bf=0

里面的选项就是:

M3U8_URL = "https://vip.dytt-cine.com/20251024/59985_d292974a/3000k/hls/mixed.m3u8"
OUTPUT = "output.mp4"

HEADERS = {
    "User-Agent": "Mozilla/5.0",
    # 如果你的合法资源需要 Referer / Cookie,可加
    "Referer": "https://vip.dytt-cine.com/share/d292974a12895cb3f54966cfc83576a8",
    # "Cookie": "session=xxxx",
}

如果使用ffmpeg下载,代码如下:

import subprocess

M3U8_URL = "https://vip.dytt-cine.com/20251024/59985_d292974a/3000k/hls/mixed.m3u8"
OUTPUT = "output.mp4"

HEADERS = {
    "User-Agent": "Mozilla/5.0",
    # 如果你的合法资源需要 Referer / Cookie,可加
    "Referer": "https://vip.dytt-cine.com/share/d292974a12895cb3f54966cfc83576a8",
    # "Cookie": "session=xxxx",
}

def build_header_args(headers: dict):
    # ffmpeg 需要把 header 拼成字符串
    header_str = ""
    for k, v in headers.items():
        header_str += f"{k}: {v}\r\n"
    return header_str

cmd = [
    "ffmpeg",
    "-y",
    "-headers", build_header_args(HEADERS),
    "-i", M3U8_URL,
    "-c", "copy",
    "-bsf:a", "aac_adtstoasc",
    OUTPUT,
]

print("Running ffmpeg...")
subprocess.run(cmd, check=True)
print("Done:", OUTPUT)

但是这个东西实在是太慢了,

目前有开源的项目直接做了这个:

https://github.com/nilaoda/N_m3u8DL-RE

主页有详细的参数说明,那么一个命令行执行就可以了:

./N_m3u8DL-RE "https://vip.dytt-cine.com/20251024/59985_d292974a/3000k/hls/mixed.m3u8" --thread-count 6 --auto-select --binary-merge --save-name output --header "User-Agent:Mozilla/5.0" --header "Referer:https://vip.dytt-cine.com/share/d292974a12895cb3f54966cfc83576a8"

速度比ffmpeg快,而且输出更加清晰。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值