import os, shutil, base64, traceback new_extension = '.mykey' # 修改为新的文件后缀 for dirs, _, files in os.walk('.'): for file in files: if file.endswith('.m3u8'): try: with open(f"{dirs}/encryption-f5.key", '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)].replace('.m3u8', new_extension))
                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, '.') # 移动文件到当前文件夹
                    os.rename(os.path.join('.', file), os.path.join('.', file.replace('.m3u8', new_extension))) # 重命名文件后缀为新的后缀名
Python 批量修改 .m3u8 文件加密密钥并重命名为指定后缀

原文地址: https://www.cveoy.top/t/topic/ngNB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录