import osimport reimport shutilimport sysimport subprocessdef func(target_file): # 读取文件中的所有内容 with open(target_file, "r", encoding="utf8") as f: lines = f.readlines() folder_name = re.search(r"(?<=QQBrowser/视频/).*?(?=/0.key)", lines[2]).group() # 提取文件夹名 lines[2] = re.sub(r'(?<=URI=").*(?=",)', 'key.ts', lines[2], 0) # 更换key的位置 for i in range(4, len(lines), 2): lines[i] = lines[i][74:] # 更换每个视频片段的位置为相对路径 with open(folder_name + os.path.sep + target_file[:-4] + ".m3u8", "w", encoding="utf8") as f: f.writelines(lines) try: os.rename(folder_name + os.path.sep + "0.key", folder_name + os.path.sep + "key.ts") # 改key的文件名 except FileNotFoundError as e: pass print(os.path.abspath(folder_name + os.path.sep + target_file[:-4] + ".m3u8")) # 输出m3u8的绝对路径 folder_abs_path = os.path.abspath(folder_name) file_abs_path = os.path.abspath(folder_name + os.path.sep + target_file[:-4] + ".m3u8") return folder_abs_path, file_abs_pathfolder, file = func(sys.argv[1])output_dir = sys.argv[2]if output_dir.endswith(":"): output_dir += os.path.sepoutput_file = os.path.join(os.path.abspath(output_dir), os.path.basename(file)[:-5] + ".mp4")os.chdir(folder)cmd = r'F:ApplicationsFormatFactoryffmpeg.exe -i "%s" -c copy %s' % (file, output_file)print("正在执行", cmd)try: child = subprocess.run(cmd, timeout=3600) print("文件输出到", output_file)except subprocess.TimeoutExpired: print("超时") 执行方法
需要先下载 ffmpeg, 把代码里面 ffmpeg.exe 的位置改了
把上面的文件放到跟视频同级目录下,然后执行
python convert.py 测试视频.mp4 C:
上面的C:是输出路径,可以改成别的
如果需要批量执行,可以用PowerShell,代码示例:
Get-ChildItem F:tmp | ForEach-Object -Process{ if($_ -is [System.IO.FileInfo] -and $_.Extension.ToString() -eq ".mp4") { Write-Host($_.name); python convert.py $_.name F:Capture }}