This commit is contained in:
eric
2025-09-27 15:54:37 +08:00
parent f9bf0b8d6b
commit d507ccff4c

49
main.py
View File

@@ -258,17 +258,17 @@ def launch_ffmpeg(input_url: str, out_url: str, extra_headers: Optional[str] = N
""" """
启动一个 ffmpeg 进程:输入 input_url输出 RTMP 到 out_url。 启动一个 ffmpeg 进程:输入 input_url输出 RTMP 到 out_url。
""" """
user_agent = ("Mozilla/5.0 (Linux; Android 11; SAMSUNG SM-G973U) AppleWebKit/537.36 " user_agent = ("Mozilla/5.0 (Linux; Android 11; SM-G973U) "
"(KHTML, like Gecko) SamsungBrowser/14.2 Chrome/87.0.4280.141 Mobile Safari/537.36") "AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/87.0.4280.141 Mobile Safari/537.36")
rw_timeout = "15000000" rw_timeout = "15000000"
analyzeduration = "20000000" analyzeduration = "20000000"
probesize = "10000000" probesize = "10000000"
bufsize = "8000k"
base = [ base = [
"ffmpeg", "-nostdin", "-hide_banner", "ffmpeg", "-nostdin", "-hide_banner",
# 超时 & 重连(必须在 -i 前面) # 超时和重连
"-rw_timeout", rw_timeout, "-rw_timeout", rw_timeout,
"-timeout", "10000000", "-timeout", "10000000",
"-reconnect", "1", "-reconnect", "1",
@@ -280,33 +280,40 @@ def launch_ffmpeg(input_url: str, out_url: str, extra_headers: Optional[str] = N
"-user_agent", user_agent, "-user_agent", user_agent,
"-protocol_whitelist", "rtmp,crypto,file,http,https,tcp,tls,udp,rtp,httpproxy", "-protocol_whitelist", "rtmp,crypto,file,http,https,tcp,tls,udp,rtp,httpproxy",
# 缓冲 & 分析 # 缓冲分析
"-thread_queue_size", "1024", "-thread_queue_size", "1024",
"-analyzeduration", analyzeduration, "-analyzeduration", analyzeduration,
"-probesize", probesize, "-probesize", probesize,
"-fflags", "+discardcorrupt", "-fflags", "+discardcorrupt",
# 输入流(放在重连参数之后) # 输入
"-re", "-i", input_url, "-i", input_url,
# 视频/音频编解码 # 强制视频转码成 YouTube 最兼容的格式
"-c:v", "copy", # 直接拷贝视频,省 CPU低延迟 "-c:v", "libx264",
"-c:a", "aac", # 音频转码成 AAC保证 YouTube 兼容) "-preset", "veryfast",
"-ar", "44100", # 音频采样率 "-pix_fmt", "yuv420p",
"-b:a", "128k", # 音频码率 "-profile:v", "high",
"-ac", "2", # 双声道 "-level", "4.1",
"-bufsize", bufsize, "-r", "30", # 固定帧率
"-max_muxing_queue_size", "2048", "-g", "60", # GOP=2s
"-avoid_negative_ts", "1", "-b:v", "5000k",
"-fflags", "+genpts", # 确保时间戳连续,防止花屏 "-maxrate", "5000k",
"-bufsize", "10000k",
# 音频转码成 AAC
"-c:a", "aac",
"-b:a", "128k",
"-ar", "44100",
"-ac", "2",
# 避免缓冲过长导致 "no data"
"-muxdelay", "0",
"-shortest",
# 输出容器
"-f", "flv", out_url "-f", "flv", out_url
] ]
if extra_headers:
base[ base.index("-user_agent") : base.index("-protocol_whitelist") ] += ["-headers", extra_headers]
if proxy_addr: if proxy_addr:
base = ["ffmpeg", "-http_proxy", proxy_addr] + base[1:] base = ["ffmpeg", "-http_proxy", proxy_addr] + base[1:]