无法抓取Zoom直播流视频,Python代码运行无报错无输出求解决方案
问题排查与修复方案
1. 核心问题排查方向
- 先验证请求响应合法性:当前代码未校验HTTP响应状态码,若接口返回401(token过期/无权限)、404(地址错误)、403(被反爬拦截)等非200状态,写入文件的实际是错误响应内容而非视频流,自然无法正常播放。建议先加状态码校验,同时打印响应头
Content-Type字段,确认返回内容确实是视频格式:
print(response.status_code) print(response.headers.get('Content-Type')) # 可以先打印前100个字符的响应内容,确认是不是视频二进制流 print(response.content[:100])
- 检查请求头配置:Zoom接口对爬虫请求的校验较严格,未携带符合要求的
User-Agent等请求头时,会返回拦截页面而非视频流,请求时补充浏览器UA即可。 - 确认流格式:Zoom直播流大多为HLS分片格式(后缀为.m3u8的索引文件),直接请求拿到的是分片索引而非完整视频,此时
Content-Type会显示为application/vnd.apple.mpegurl,需要用ffmpeg或者m3u8解析库批量下载分片后合并为完整视频,不能直接保存为mp4文件。 - 校验写入路径权限:打印
full_filename的绝对路径,确认当前程序有该路径的写入权限,避免文件实际写入成功但你找错了存储位置。
2. 修复后的参考代码
import requests import os new_download_url = e_url+'?access_token=' + zoom_access_token headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" } try: # 增加超时、请求头配置 response = requests.get(new_download_url, stream=True, headers=headers, timeout=30) # 自动抛出非200状态的请求异常 response.raise_for_status() # 确认返回内容是视频流或直播索引 content_type = response.headers.get('Content-Type','') if 'video' not in content_type and 'mpegurl' not in content_type: print(f"返回内容非视频/直播索引,内容类型:{content_type},响应内容:{response.text[:500]}") exit() except requests.exceptions.RequestException as err: print(f"请求失败:{repr(err)}") exit() try: with open(full_filename, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 8): if chunk: fd.write(chunk) # 如果是m3u8格式,这里还要补充解析下载分片的逻辑 print(f"文件写入完成,存储路径:{os.path.abspath(full_filename)}") return True except Exception as e: print(f"文件写入失败:{e}") return False
3. HLS流(m3u8)补充处理方案
如果确认返回的是m3u8索引文件,可以直接调用ffmpeg命令一键下载合并:
import subprocess subprocess.run(f"ffmpeg -i \"{new_download_url}\" -c copy \"{full_filename}\"", shell=True, check=True)
内容的提问来源于stack exchange,提问作者KApril
相关产品推荐
相关产品推荐

