最近想下m3u8格式视频流但是网址太卡好慢看不了所以搞了个python脚本下载 ,给有需要的也用用 ,可以有点小问题大家可以改改,搬或者移到其他视频流下载上,不要嫌弃
# 自己定义start ,url,key变量 在E下建立目录dowload目录和name0.txt ,name1.txt, 在 dowload目录下建立q1目录
import importlib
import os
import sys
import _thread
import requests
import time
import datetime
import string
#from Crypto.Cipher import AES
# pycryptohome
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
importlib.reload(sys)
#sys.setdefaultencoding('utf-8')
def download(url,num,open1,end1,pathend):
global cuocu #判断出错最后不合并
global okok
global start
#num=t
download_path = os.getcwd() + "\download"
download_path1=download_path
download_path0 = os.getcwd()
if not os.path.exists(download_path):
os.mkdir(download_path)
download_path = os.path.join(download_path, datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
os.mkdir(download_path)
all_content = requests.get(url).text
file_line = all_content.split("\n")
zb=0
for index, line in enumerate(file_line): # 第二层
if "EXTINF" in line: # 找ts地址并下载
if file_line[index + 1].rsplit("/", 1)[-1]==open1:
zb=1
elif file_line[index + 1].rsplit("/", 1)[-1]==end1 or open1=="EXT-X-ENDLIST" :
zb=0
break
elif file_line[index + 1].rsplit("/", 1)[-1]!=end1 and end1=="EXT-X-ENDLIST" and zb==1 : #返回过载任务
try:
start=file_line[index + 2001].rsplit("/", 1)[-1]
except:
start=""
print("返回下个线程中"+start)
end1=start
# ""一个线程
if zb==1 and "EXTINF" in line:
pd_url = url.rsplit("/", 1)[0] + "/" + file_line[index + 1] # 拼出ts片
# res = requests.get(pd_url)
res=dowfile(pd_url,0)
num=num+1
c_fule_name =str(num)+ ".ts"
print("准备解密"+c_fule_name)
# 定义文件名
#无密钥处理
#with open(os.path.join(download_path, c_fule_name),'ab') as f:
# f.write(res.content)
# f.flush() '''
#密钥处理
# 密钥 处理225419579aaeeeeb
key= 'ae943c33f8d89cab'.encode('utf-8')
cryptor = AES.new(key, AES.MODE_CBC, key)
with open(os.path.join(download_path, c_fule_name),'ab') as f:
try:
f.write(cryptor.decrypt(res.content))
f.flush()
except:
mkmk=0
while mkmk<1:
# 密钥 处理225419579aaeeeeb
print("解密错误稍等")
res=dowfile(pd_url,0)
key= 'ae943c33f8d89cab'.encode('utf-8')
cryptor = AES.new(key, AES.MODE_CBC, key)
try:
f.write(cryptor.decrypt(res.content))
f.flush()
mkmk=1
except: #记录文件缺失信息
mkmk=mkmk-1
lines1 = []
print("记录文件错误信息")
if mkmk<0: #定义尝试次数
cuocu=1
with open(os.path.join(download_path0,'name0')+".TXT", 'r') as f_in1:
for line in f_in1:
lines1.append(line)
line ="\n"+"等待完成"+"\n"+file_line[index + 1]+"\n"+end1+"\n"+download_path+"\n"+str(num)
lines1.append(line)
with open(os.path.join(download_path0,'name0')+".TXT", 'w') as f_out1:
f_out1.writelines(lines1)
lines1 = []
with open(os.path.join(download_path1,'name0')+".TXT", 'r') as f_in1:
for line in f_in1:
lines1.append(line)
line ="\n"+"等待完成"+"\n"+file_line[index + 1]+"\n"+end1+"\n"+download_path+"\n"+str(num)
lines1.append(line)
with open(os.path.join(download_path1,'name0')+".TXT", 'w') as f_out1:
f_out1.writelines(lines1)
name= file_line[index + 1].rsplit("/", 1)[-1]
print(name)
lines = []
with open(os.path.join(download_path0,'name1')+".TXT", 'r') as f_in1:
for line in f_in1:
lines.append(line)
line="\n"+"线"+str(num)+"程"+name
lines.append(line)
with open(os.path.join(download_path0,'name1')+".TXT", 'w') as f_out:
f_out.writelines(lines)
if "EXT-X-ENDLIST" in line: # 完成
print("soon")
merge_file(download_path,num,pathend)
print("ok 线程"+str(num))
okok=okok+1
def dowfile(pathfile,t):
t=t+40
res=[]
path=pathfile
if t<200: #定义超时重试次数
try:
res = requests.get(path,timeout=40)
except requests.exceptions.RequestException as e:
res =dowfile(path,t)
print("Bitch")
else:
res ="" #返回空文档
return res
def merge_file(path,too,pathend):
#os.chdir(path)
too1=str(too)+".ts"
cmd = r"copy /b " +path+r"\*.ts "+os.path.join(pathend,too1)
os.system(cmd)
try:
os.remove(path+r"\*.ts")
os.remove(path+r"\*.txt")
os.removedirs(path)
except:
print("删除文件错误")
if __name__ == '__main__':
with open(r"E:\name1"+".TXT", 'w') as f_out:
f_out.writelines(r"#")
print(os.path.getsize(r"E:\name1.txt"))
global cuocu
cuocu=0
pathend=r"E:\download\q1"
global okok
global t1
okok=0
global start
start="9xWqF4209000.ts"
op=""
std=str(555)
print(std)
url=r"https://d1zasx6kkglh3ut.net/9720190101/TK2/1000kb/hls/index.m3u8"
# 创建线程
t1=0
t2=None
num1=2000000
if start=="" or os.path.getsize(r"E:\name0.txt")>2:
start==""
downloadpath=os.getcwd()
downloadpath1=os.getcwd()+"\download"
downloadpath2=os.path.join(downloadpath1,'name0')+".txt"
os.remove(downloadpath2)
os.mknod(downloadpath2)
with open(downloadpath2+".TXT", 'w') as f_out:
f_out.writelines(r"#")
okok=1
num2=4000000
lines=[]
with open(os.path.join(downloadpath,'name0')+".TXT", 'r') as f_in2:
for line in f_in2:
lines.append(line)
#lines2 =line2.split("\n")
for index, line in enumerate(lines):
cuocu=0
if "等待完成" in line: #
open2=lines[index + 1].rsplit("/", 1)[-1]
end2=lines[index + 2].rsplit("/", 1)[-1]
filepath=lines[index + 3].rsplit("/", 1)[-1]
pathend=filepath
numend=lines[index + 4].rsplit("/", 1)[-1]
download(url,num2,open2,end2,pathend)
cmd = "copy /b "+filepath+"*.ts "+os.path.join(r"E:\download\q1",numend)+".ts"
os.system(cmd)
if cuowu==0:
try:
os.remove(filepath+r"\*.ts")
os.remove(filepath+r"\*.txt")
os.removedirs(filepath)
except:
print("删除文件错误")
#12
os.remove(os.path.join(downloadpath,'name0')+".txt")
cmd = r"copy "+r"E:\download\name0.txt "+r"E:\name0.txt"
os.system(cmd)
print("结束第一次重启")
while start!="":
if t1==0:
with open(r"E:/download/name0"+".TXT", 'w') as f_out:
f_out.writelines(r"#")
op=start
try:
num1=num1+10000
t2= _thread.start_new_thread( download,(url,num1,start,"EXT-X-ENDLIST",pathend) )
# _thread.start_new_thread(download,(url,num2,"6QAJbU53638657.ts","EXT-X-ENDLIST") )
except:
print("Error: 无法启动线程")
t1=t1+1
while start==op:
time.sleep(4)
print("线程中等待返回状态"+start+op)
time.sleep(10)
while okok<t1:
print("一共线程"+str(t1)+"完成"+str(okok))
time.sleep(60)
print("准备拼接")
if cuocu==0:
cmd = r"copy"+r" /b"+r" E:\download\q1\*.ts"+r" "+r" E:\download\q1\w.ts"
os.system(cmd)
elif cuocu!=0 and os.path.getsize(r"E:\name0.txt")<2 :
os.remove(r"E:\name0.txt")
cmd = r"copy"+r" E:\download\name0.txt"+r" "+r" E:\name0.txt"
os.system(cmd)
elif os.path.getsize(r"E:\name0.txt")>2:
cmd = r"python"+r" E:\ts-python.py"
os.system(cmd)
本文地址:https://blog.csdn.net/X1173855636/article/details/107580874
上一篇: 项目中遇到的问题的总结