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

相关推荐
weixin_307779131 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ew452182 小时前
ElementUI表格表头自定义添加checkbox,点击选中样式不生效
前端·javascript·elementui
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
Moon.92 小时前
el-table的hasChildren不生效?子级没数据还显示箭头号?树形数据无法展开和收缩
前端·vue.js·html
垚垚 Securify 前沿站2 小时前
深入了解 AppScan 工具的使用:筑牢 Web 应用安全防线
运维·前端·网络·安全·web安全·系统安全
多想和从前一样4 小时前
Django 创建表时 “__str__ ”方法的使用
后端·python·django
ll7788114 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
工业甲酰苯胺5 小时前
Vue3 基础概念与环境搭建
前端·javascript·vue.js
小喵要摸鱼6 小时前
【Pytorch 库】自定义数据集相关的类
pytorch·python