Python 批量修改 .m3u8 文件加密密钥 - 支持自定义密钥文件
import os, shutil, base64, traceback key_file = input('请输入文件名:') # 用户输入任何以.key后缀结尾的文件名 for dirs, _, files in os.walk('.'): for file in files: if file.endswith('.m3u8'): try: with open(f'{dirs}/{key_file}', 'rb') as (f): key = f.read() except: print(traceback.format_exc()) with open('error.txt', 'a+') as (f): f.write(dirs + '\n')
if key:
if len(file.split('.')[0]):
if file.split('.')[0]:
filename = dirs + '.m3u8'
else:
filename = dirs + file.split('.')[0] + '.m3u8'
print(filename)
jroot = os.path.join(dirs, file)
with open(jroot, 'r+', encoding='ansi') as (f1):
a = f1.read().splitlines()
a[4] = '#EXT-X-KEY:METHOD=AES-128,URI='base64:{0}''.format(base64.b64encode(key).decode())
f1.seek(0)
for line in a:
f1.write(line + '\n')
f1.close()
try:
os.rename(jroot, filename.split('\')[(-1)])
except:
print(traceback.format_exc())
with open('error.txt', 'a+') as (f):
f.write(dirs + '\n')
else:
jroot = os.path.join(dirs, file)
with open(jroot, 'r+', encoding='ansi') as (f1):
a = f1.read().splitlines()
a[4] = '#EXT-X-KEY:METHOD=AES-128,URI='base64:{0}''.format(base64.b64encode(key).decode())
f1.seek(0)
for line in a:
f1.write(line + '\n')
f1.close()
shutil.move(jroot, '.')
原文地址: https://www.cveoy.top/t/topic/ngNJ 著作权归作者所有。请勿转载和采集!