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()
最新发布