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

相关推荐
张小凡vip1 分钟前
python--爬虫--经验积累的遇到的坑
开发语言·爬虫·python
毕业设计70315 分钟前
(免费领源码) 基于微信小程序的预制菜商城的设计与实现25172-java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
vue.js·python·mysql·微信小程序·pycharm·微信开发者工具·推荐算法
xifangge202521 分钟前
AGENTS.md 怎么写?涵盖 Java、Python、Vue、Go 的 8 套开箱即用模板
java·vue.js·python
几何心凉23 分钟前
没有万能模型:我用 AiiOnly Token Plan,把多款大模型的长处拼进同一个项目
前端
Wang's Blog24 分钟前
Java框架快速入门: Spring Security+OAuth2之云服务集成与多因子认证设计
java·开发语言·spring
kyriewen25 分钟前
Claude Code 的额度今天缩水了17%——官方公告上写的是"永久提高25%"
前端·ai编程·claude
门思科技43 分钟前
LoRaWAN 网络容量估算:一个 SX1302 网关到底能带多少设备
开发语言·网络·php
大草原的小灰灰1 小时前
Python基础语法
开发语言·python
雪芽蓝域zzs1 小时前
vue解构平铺VS对象包裹
前端·javascript·vue.js
小葱炖豆腐2 小时前
python绘制excel折线图
python·excel·numpy·pandas·matplotlib