VLC多块网卡问题

本文解决了一台配置了局域网和广域网两个网卡的服务器上,VLC接收不到局域网内广播数据的问题。通过使用`--miface-addr`命令行参数指定IPv4多播输出接口地址,成功解决了该问题。

曾经遇到的问题:

在一台配置两个网卡的服务器上,一个网卡指定了局域网IP,令一个网卡指定了广域网IP,VLC接收不到局域网内的广播数据。由下面的命令行可以解决:

--miface-addr=<string>     IPv4 multicast output interface address

IPv4 adress for the default multicast interface. This overrides the routing table.

 
import subprocess import time from urllib.parse import quote import os import sys # ---------- 配置区(按需修改) ---------- iface_name = "以太网" # 要禁用/启用的网卡名(Windows 显示名) ipc_ip = "192.168.0.60" # IPC 的 IP(用于拼 URL) multicast_path = "/multicastStream1" # 组播路径 rtsp_port = "554" # RTSP 端口 vlc_exe = r"D:\UserData\Documents\VLC\vlc.exe" # vlc.exe 路径 play_seconds = 5 # 播放时长(秒) # ---------------------------------------- def run_cmd(cmd, check=False, capture=False): """辅助运行命令并返回 CompletedProcess(自动处理错误信息)。""" try: if capture: return subprocess.run(cmd, text=True, capture_output=True, check=check) else: return subprocess.run(cmd, check=check) except subprocess.CalledProcessError as e: return e def set_interface_state(name, enable=True): """Windows 下用 netsh 启用/禁用网络适配器。enable=True 启用,False 禁用""" action = "enable" if enable else "disable" cmd = ["netsh", "interface", "set", "interface", name, action] print(f"执行: {' '.join(cmd)}") res = run_cmd(cmd, capture=True) # res.returncode 0 表示成功 return res def play_multicast_and_check(ip, path, port, vlc_path, duration): """用 VLC 播放指定 RTSP,播放 duration 秒,返回 True/False(是否有播放迹象)""" url = f"rtsp://{ip}:{port}{path}" print("播放 RTSP URL:", url) cmd = [ vlc_path, "--no-one-instance", "--rtsp-tcp", "--network-caching=1500", "-vvv", url, "--run-time={}".format(duration), "vlc://quit" ] # 指定 encoding='utf-8' 并忽略解码错误以避免 Windows 控制台编码问题 try: res = subprocess.run(cmd, text=True, capture_output=True, encoding="utf-8", errors="ignore") except Exception as e: print("启动 VLC 时异常:", e) return False log = (res.stdout or "") + (res.stderr or "") success_keys = [ "stream buffering done", "started playback", "playing", "play started", "live555 debug: play", "decoder debug:", "stream opened successfully", "opening stream" ] played = any(k in log.lower() for k in success_keys) # 调试时可取消下面一行注释查看日志片段 # print("VLC log tail:\\n", log[-2000:]) return played def main(): # 权限提示 if os.name != "nt": print("该脚本仅用于 Windows(使用 netsh 管理网卡)。") return # 检查 vlc 可执行文件 if not os.path.exists(vlc_exe): print("找不到 vlc.exe,请确认 vlc_exe 路径:", vlc_exe) return print("注意:执行网卡禁用/启用需要管理员权限,并会影响网络连通性。") input("确认继续请按 Enter(取消请 Ctrl+C)...") # 1) 禁用网卡 print("尝试禁用网卡:", iface_name) res_disable = set_interface_state(iface_name, enable=False) if hasattr(res_disable, "returncode") and res_disable.returncode != 0: print("禁用网卡返回非0(可能失败),stdout/stderr:\n", getattr(res_disable, "stdout", ""), getattr(res_disable, "stderr", "")) # 仍继续尝试播放(视你的需求可以直接返回) else: print("已发出禁用网卡命令,等待 1 秒以稳定状态...") time.sleep(1) # 2) 播放组播 RTSP 并检测 try: played = play_multicast_and_check(ipc_ip, multicast_path, rtsp_port, vlc_exe, play_seconds) if played: print("RTSP 播放成功(已检测到播放迹象)") else: print("RTSP 未播放(未检测到播放迹象)") finally: # 3) 确保重新启用网卡(清理) print("恢复网卡:", iface_name) res_enable = set_interface_state(iface_name, enable=True) if hasattr(res_enable, "returncode") and res_enable.returncode != 0: print("启用网卡失败,stdout/stderr:\n", getattr(res_enable, "stdout", ""), getattr(res_enable, "stderr", "")) else: print("已发出启用网卡命令。若仍无网络,请手动检查网络适配器。") if __name__ == "__main__": main()
最新发布
10-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值