当前位置: 首页 > ai >正文

python + streamlink 下载 vimeo 短视频

1. 起因, 目的:

  • 看到一个视频,很喜欢,想下载。
  • https://player.vimeo.com/video/937787642

2. 先看效果

能下载。请添加图片描述
请添加图片描述

3. 过程:

  • 因为我自己没头绪。先看一下别人的例子, 问一下 ai 或是 google
  • 问了几个来回,原来是流式加载的视频。 那么好办, 就使用 streamlink , 之前用过,效果很好的。
下载
import requests
import streamlink
import osdef get_vimeo_stream_url(video_url):# 设置请求头,模拟浏览器headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36","Referer": "https://vimeo.com/"}try:# Step 1: 获取配置 JSONconfig_url = video_url.replace("player.vimeo.com/video/", "player.vimeo.com/video/") + "/config"response = requests.get(config_url, headers=headers)response.raise_for_status()# Step 2: 解析 JSON 数据config_data = response.json()# 优先提取 HLS 链接hls_url = config_data.get("request", {}).get("files", {}).get("hls", {}).get("cdns", {}).get("akfire_interconnect_quic", {}).get("url")if not hls_url:hls_url = config_data.get("request", {}).get("files", {}).get("hls", {}).get("cdns", {}).get("fastly",{}).get("url")if hls_url:print(f"找到 HLS 链接: {hls_url}")return hls_url# 如果没有 HLS,尝试提取 progressive(MP4)链接video_files = config_data.get("request", {}).get("files", {}).get("progressive", [])if video_files:video_files = sorted(video_files, key=lambda x: x.get("height", 0), reverse=True)mp4_url = video_files[0]["url"]print(f"没有 HLS 链接,使用 MP4 链接: {mp4_url}")return mp4_urlprint("未找到可用的流链接!可能是受限视频或需要登录。")return Noneexcept requests.exceptions.RequestException as e:print(f"请求错误: {e}")return Noneexcept (KeyError, IndexError) as e:print(f"解析错误: {e},可能是视频受限或格式不正确")return Nonedef download_with_streamlink(stream_url, output_path="video.mp4"):try:# Step 3: 使用 streamlink 下载streams = streamlink.streams(stream_url)if not streams:print("无法获取视频流,可能是链接无效或需要认证")return False# 选择最佳质量stream = streams.get("best")if not stream:print("未找到最佳质量流")return Falseprint(f"开始下载,质量: {stream}")with stream.open() as stream_data:with open(output_path, "wb") as f:while True:data = stream_data.read(8192)if not data:breakf.write(data)print(f"视频已下载到: {output_path}")return Trueexcept Exception as e:print(f"下载错误: {e}")return False# 示例使用
video_url = "https://player.vimeo.com/video/937787642"
output_file = "downloaded_video.mp4"# 获取流链接
stream_url = get_vimeo_stream_url(video_url)
if stream_url:# 使用 streamlink 下载download_with_streamlink(stream_url, output_file)

4. 结论 + todo

  • 想法: 下载其他视频, 或是批量下载。但是我觉得没意思。

  • 我就是想试试看能不能下载成功。

  • 聊天记录: https://x.com/i/grok?conversation=1923401591656530204

  • vimeo 简介: https://chatgpt.com/c/68275b18-7d68-8002-9197-a84a62128cab


希望对大家有帮助。

http://www.xdnf.cn/news/6740.html

相关文章:

  • 区块链基本理解
  • OCCT知识笔记之OCAF框架详解
  • CSPM-3级考试成绩已出!附查询流程
  • 1949-2022年各省农作物播种面积数据(22个指标)
  • Python开源项目月排行 2025年4月
  • SpringBoot中使用Flux实现流式返回的技术总结
  • Java8到24新特性整理
  • 芯片生态链深度解析(一):基础材料篇——从砂砾到硅基王国的核心技术突围
  • AI人工智能在教育领域的应用
  • 水库雨水情测报与安全监测系统解决方案
  • 科达嘉数字功放电感应用于英飞凌参考设计REF_MA5302BTLSPS_400W
  • opencv入门指南
  • STM32外设AD-DMA+定时读取模板
  • 如何离线环境下安装Dify插件
  • SettingsIntelligence
  • 梦熊解析:202505基础算法
  • debugfs:Linux 内核调试的利器
  • 如何有效的开展接口自动化测试?
  • 今日行情明日机会——20250516
  • PMP-第十二章 项目采购管理
  • windows平台监控目录、子目录下的文件变化
  • 革新直流计量!安科瑞DJSF1352-D电表:360A免分流直连,精度与空间双突破
  • Linux远程连接服务
  • 1基·2台·3空间·6主体——蓝象智联解码可信数据空间的“数智密码”
  • 5 Celery多节点部署
  • c++,linux,多线程编程详细介绍
  • FC7300 ADC采样理论介绍
  • 宽河道流量监测——阵列雷达波测流系统如何监测河道流量
  • GTS-400 系列运动控制器板卡介绍(三十六)--- 电机到位检测功能
  • Ubuntu 22.04 上安装 Drupal 10并配置 Nginx, mysql 和 php