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