python 编写m3u8视频格式下载小工具

vbnet 复制代码
# coding:utf-8     设置编码格式
import requests   #引入requests
from bs4 import BeautifulSoup
import re   #引入正则
import lxml
import os
import threading

#解析m3u8文件
def parse_m3u8(file):
    with open(file, 'r') as f:
        lines = f.readlines()

    playlist = []
    for line in lines:
        if line.startswith('#'):
            continue  # 跳过以 # 开头的行
        line = line.strip()  # 删除行尾的空格和换行符
        playlist.append(line)  # 添加到播放列表

    return playlist

# m3u8_file = 'path_to_your_m3u8_file.m3u8'
# playlist = parse_m3u8(m3u8_file)
# print(playlist)

# 下载m3u8文件
def download_file(url, save_path):
    print(f'开始下载 {url} ...')
    #response = requests.get(url, stream=True)
    response = requests.get(url)
    try:
      response.raise_for_status()
    except  Exception as e:
      print('下载失败,重新下载')
      download_file(url, save_path)
    #save_file(save_path, response.raw)
    save_file(save_path, response.content)
    print(f'{url} 下载完成.')
#保存文件
def save_file(file_name, file_stream):
    with open(file_name, 'wb') as output_file:
        #shutil.copyfileobj(file_stream, output_file)
        output_file.write(file_stream)

url="https://hot.1080pzy.co/20230930/nC1QVsQO/1000kb/hls/index.m3u8?_t=1696133646596"

#下载m3u8视频文件列表
response=requests.get(url)   #请求返回内容
headers=response.headers     #响应头信息
content=response.text        #响应body信息
with open('list.txt', "w", encoding='utf-8') as f:
    f.write(content)
print('下载m3u8视频文件列表ok')

#解析文件列表
playlist = parse_m3u8('list.txt')
id=1

for part in playlist:
    if id<10:
      download_file(part,str(id).zfill(6)+'.ts')

      id=id+1

print('下载完成')

#cmd合并文件
#copy /b   *.ts   new.mp4

python 编写m3u8视频格式下载小工具

相关推荐
Never_Satisfied几秒前
在JavaScript / HTML中,词内断行
开发语言·javascript·html
MoRanzhi120311 分钟前
11. Pandas 数据分类与区间分组(cut 与 qcut)
人工智能·python·机器学习·数学建模·分类·数据挖掘·pandas
IT_陈寒20 分钟前
Java并发编程避坑指南:7个常见陷阱与性能提升30%的解决方案
前端·人工智能·后端
码界筑梦坊30 分钟前
276-基于Python的爱奇艺视频数据可视化分析系统
开发语言·python·信息可视化
HBR666_34 分钟前
AI编辑器(FIM补全,AI扩写)简介
前端·ai·编辑器·fim·tiptap
excel39 分钟前
一文读懂 Vue 组件间通信机制(含 Vue2 / Vue3 区别)
前端·javascript·vue.js
JarvanMo42 分钟前
Flutter 应用生命周期:使用 AppLifecycleListener 阻止应用崩溃
前端
我的xiaodoujiao2 小时前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 9--基础知识 5--常用函数 3
前端·python·测试工具·ui
李鸿耀4 小时前
Flex 布局下文字省略不生效?原因其实很简单
前端
皮蛋瘦肉粥_1215 小时前
pink老师html5+css3day06
前端·css3·html5