使用Selenium下载JW Player直播流片段的问题求助
问题解决:JW Player直播流播放触发与片段录制/下载
一、修复视频播放触发问题
你的点击代码无效,核心原因有两个:一是元素未完全加载就执行点击操作,二是直接点击div/video并非JW Player的标准播放触发方式——JW Player依赖自身JS API控制播放逻辑。
解决方案:等待元素加载+调用JW Player JS API
替换原点击代码为以下内容:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # 等待播放器元素加载完成,确保JW Player实例初始化 WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "msw-js-player245-stream")) ) # 通过JS直接调用JW Player的play()方法(JW Player全局实例以jwplayer()获取) driver.execute_script("jwplayer('msw-js-player245-stream').play();") # 等待播放启动,可根据页面实际加载速度调整时长 time.sleep(2)
二、优先方案:直接下载60秒直播流片段
视频src显示为blob:格式,这是浏览器内存中的临时地址,需先获取真实流媒体URL(多为HLS协议的.m3u8地址),再用ffmpeg下载指定时长片段。
步骤1:获取真实流媒体地址
播放启动后,执行JS提取JW Player的播放源信息:
# 获取当前播放项的详细信息 stream_info = driver.execute_script("return jwplayer('msw-js-player245-stream').getPlaylistItem();") # 提取HLS流地址(通常存储在file字段) stream_url = stream_info['file'] print(f"真实流地址:{stream_url}")
步骤2:用ffmpeg下载60秒片段
确保本地已安装ffmpeg,通过Python调用命令行完成下载:
import subprocess # 定义输出文件名和录制时长(单位:秒) output_file = "live_stream_clip.mp4" duration = 60 # 构建ffmpeg命令:直接拷贝编码格式,避免转码耗时 ffmpeg_cmd = [ "ffmpeg", "-i", stream_url, "-t", str(duration), "-c:v", "copy", "-c:a", "copy", output_file ] # 执行下载命令 subprocess.run(ffmpeg_cmd, check=True)
三、备选方案:屏幕录制视频片段
若无法获取真实流地址,可采用屏幕录制方案,推荐轻量级的mss库:
from mss import mss import cv2 import numpy as np import time # 获取视频元素的位置与尺寸 video_element = driver.find_element(By.CSS_SELECTOR, "video.jw-video") location = video_element.location size = video_element.size left = location['x'] top = location['y'] width = size['width'] height = size['height'] # 定义录制区域 monitor = {"top": top, "left": left, "width": width, "height": height} # 初始化录屏工具与视频写入器 sct = mss() fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter('screen_recording.mp4', fourcc, 30.0, (width, height)) # 录制60秒 start_time = time.time() while time.time() - start_time < 60: sct_img = sct.grab(monitor) frame = np.array(sct_img) # 转换颜色空间(mss返回BGRA格式,需转为OpenCV兼容的BGR) frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) out.write(frame) # 释放资源 out.release() cv2.destroyAllWindows()
录屏注意事项
- 录制过程中需确保浏览器窗口不被遮挡
- 若视频元素有动态尺寸变化,需实时更新录制区域参数
四、完整整合脚本示例
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time import subprocess options = webdriver.ChromeOptions() options.add_experimental_option("detach", True) driver = webdriver.Chrome(options=options) # 登录流程 driver.get("https://magicseaweed.com/account/login/?dest=%2FLive-Whitesands-Webcam%2F245%2F") email = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, '//*[@id="msw-js-user-login-tab"]/form/div[2]/input')) ) email.clear() email.send_keys("***@gmail.com") pwd = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, '//*[@id="msw-js-user-login-tab"]/form/div[3]/input')) ) pwd.clear() pwd.send_keys("******") pwd.send_keys(Keys.RETURN) # 等待播放器加载完成 WebDriverWait(driver, 15).until( EC.presence_of_element_located((By.ID, "msw-js-player245-stream")) ) # 启动视频播放 driver.execute_script("jwplayer('msw-js-player245-stream').play();") time.sleep(2) # 获取真实流地址 stream_info = driver.execute_script("return jwplayer('msw-js-player245-stream').getPlaylistItem();") stream_url = stream_info['file'] print(f"获取到流地址:{stream_url}") # 下载60秒片段 output_file = "whitesands_live_clip.mp4" duration = 60 ffmpeg_cmd = [ "ffmpeg", "-i", stream_url, "-t", str(duration), "-c:v", "copy", "-c:a", "copy", output_file ] subprocess.run(ffmpeg_cmd, check=True) print(f"60秒片段已保存至:{output_file}") driver.quit()
内容的提问来源于stack exchange,提问作者Jamie
相关产品推荐
相关产品推荐

