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视频格式下载小工具

相关推荐
星夜离尘562 小时前
MATLAB 工程实验:不等间隔流量积分,先把缺口与覆盖率说清楚
开发语言·matlab·时间戳·数据质量·数值积分
happylifetree2 小时前
Python70:核心语法-函数进阶-案例2
python
Ai-_Man2 小时前
Meta Muse还是不可以导出左侧所有对话,只能点进去单条内容导出,方法不对吗?
开发语言·人工智能·ai·小程序·c#·1024程序员节
梓豪csd2 小时前
消息订阅中心:谁该收到哪条通知
java·开发语言·人工智能·低代码·自动化·1024程序员节
王中阳Go2 小时前
两个入口答案不一致先别调模型:6 行入口口径对账表(附脚本)
人工智能·后端·python·大语言模型·1024程序员节
小poop2 小时前
CSS 基础与实战:从入门到精通网页美化
前端
BK贝壳2 小时前
bool-hybrid-array 落盘我只认 .bha:用 Create_BHA / Ask_BHA 做布尔数组序列化
数据结构·python·numpy·1024程序员节
学逆向的3 小时前
C 枚举变量
c语言·开发语言·网络安全
Sakura_j_h_3 小时前
Python 在定时任务里输出 emoji,UnicodeEncodeError 从哪来
开发语言·字符编码·windows·python·定时任务
Csvn4 小时前
运行时性能
前端