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

相关推荐
荣码4 分钟前
Prompt工程实战:同一个需求换3种写法,效果差10倍
java·python
想会飞的蒲公英5 分钟前
集成学习入门:Bagging、Boosting 到底在组合什么
人工智能·python·机器学习·集成学习·boosting
努力努力再努力搬砖7 分钟前
批量下载ERA5数据
python
_老码9 分钟前
AI-reader阅读助手开发过程
人工智能·python·ai
এ慕ོ冬℘゜10 分钟前
深入理解 JavaScript 事件对象:从 target 到 preventDefault 的实战指南
开发语言·前端·javascript
_Jimmy_13 分钟前
python性能分析工具
python
学逆向的16 分钟前
汇编——修改EIP的值
开发语言·汇编·网络安全
weixin_4223293117 分钟前
AgentScope Java ReActAgent 核心循环深度解析
java·开发语言·人工智能
Listen·Rain20 分钟前
Vue概述
前端·javascript·vue.js
尘世中一位迷途小书童22 分钟前
Cesium 涟漪扩散点:自定义材质,参数调对了才好看
前端·面试