import os
from PyQt5. QtWidgets import QApplication , QFileDialog
import shutil
from Crypto.Cipher import AES
#无加密合并
def merge_type_0(name,checklist):
with open ( name +'.mp4','wb+') as f1:
for i in checklist:
f2=open(i,'rb')
f1.write(f2.read())
f2.close()
f1.close()
return 1
#普通加密合并
def merge_type_1(name,kye,checklist):
cryptor = AES.new(kye.encode('utf-8'), AES.MODE_CBC,kye.encode('utf-8'))
with open ( name +'.mp4','wb+') as f1:
for i in checklist:
f2=open(i,'rb')
f1.write(cryptor.decrypt(f2.read()))
f2.close()
f1.close()
return 1
#偏移加密合并
def merge_type_2(name,kye,iv,checklist):
cryptor = AES.new(kye, AES.MODE_CBC,iv.encode('utf-8'))
with open ( name +'.mp4','wb+') as f1:
for i in checklist:
f2=open(i,'rb')
f1.write(cryptor.decrypt(f2.read()))
f2.close()
f1.close()
return 1
a=QApplication([''])
#选择文件夹,该文件夹为m3u8目录文件所存放的文件夹
directory2 = QFileDialog.getExistingDirectory(caption="选择目录文件夹",directory= "/")
print('程序开始运行……')
doclist=os.listdir(directory2)
for d in doclist:
directory3=directory2 + '/' + d
checklist=[]
sp_type=0
with open (directory3,'r')as file:
context=file.readlines()
for i in range(len(context)):
if '#EXTINF:' in context[i]:
if os.path.exists('.'+context[i+1].strip())==False:
break
checklist.append('.'+context[i+1].strip())
if 'EXT-X-KEY:' in context[i]:
if 'IV=0x'in context[i]:
sp_type=2
iv=context[i].strip()[-32:-16]
kye_path='.'+context[i].split(',')[1][5:-1]
if os.path.exists(kye_path)==False:
break
kye_file=open(kye_path,'rb')
kye=kye_file.read()
kye_file.close()
continue
else:
sp_type=1
kye_path='.'+context[i].split(',')[1][5:-1]
if os.path.exists(kye_path)==False:
break
kye_file=open(kye_path,'r')
kye=kye_file.read()
kye_file.close()
continue
if 'ENDLIST' in context[i]:
if sp_type==0:
merge_type_0(d,checklist)
shutil.rmtree(os.path.dirname('.'+context[i-1].strip()))
elif sp_type==1:
merge_type_1(d,kye,checklist)
shutil.rmtree(os.path.dirname(kye_path))
else:
merge_type_2(d,kye,iv,checklist)
shutil.rmtree(os.path.dirname(kye_path))
print(directory3+'合并完成')
break
os.remove(directory3)
print('程序处理完毕!')
千万要注意缓存碎片文件夹的路径,本代码的py文件应该放在缓存碎片文件夹的路径的根目录下。
例:本程序zh1.py的绝对路径为C:\Users\Administrator\Desktop\wj\zh1.py
存放m3u8目录文件夹为自选,无影响,本例为C:\Users\Administrator\Desktop\wj\solist
缓存碎片文件夹应当模拟UC浏览器的目录,以本程序为根目录开始创建,本例为C:\Users\Administrator\Desktop\wj\storage\emulated\0\UCDownloads\VideoData
所有的碎片文件夹均存放于此。