使用此脚本前,您可能需要配置如下清单
完成上述配置后,即可享受无限制的视频下载及合成
[Python] 纯文本查看 复制代码import os
import tkinter as tk
import tkinter.messagebox
def alert_success():
if (tk.messagebox.askquestion(title='提示', message='合并完成,是否打开下载文件夹?') == 'yes'):
output_dir = r'C:\Users\Administrator\Downloads'
os.system("explorer.exe %s" % output_dir)
def trim_file(file_name):
if isinstance(file_name, str):
if file_name.startswith('audio') or file_name.startswith('video'):
if ' ' in file_name:
rename = file_name.replace(' ', '')
print('清除文件名中的空格', file_name)
os.rename(download_dir + '\\' + file_name, download_dir + '\\' + rename)
return rename
return file_name
def scan_download_dir(download_dir):
dict1 = {}
g = os.walk(download_dir)
for path, dir_list, file_list in g:
for file_name in file_list:
rename = trim_file(file_name)
if isinstance(rename, str):
if rename.startswith('audio'):
key1 = rename[5:]
if key1 in dict1:
dict2 = dict1[key1]
else:
dict2 = {}
dict2['audio'] = os.path.join(path, rename)
dict1[key1] = dict2
if rename.startswith('video'):
key1 = rename[5:]
if key1 in dict1:
dict2 = dict1[key1]
else:
dict2 = {}
dict2['video'] = os.path.join(path, rename)
dict1[key1] = dict2
return dict1
def synthetic_video(dict1):
for key in dict1:
dict2 = dict1[key]
if isinstance(dict2, dict):
audio = dict2['audio']
video = dict2['video']
output = download_dir + '\\output' + key
cmd = f'{ffmpeg_dir} -i {video} -i {audio} -c:v copy -c:a aac -strict experimental {output}'
print('执行脚本', cmd)
os.system(cmd)
if __name__ == '__main__':
print('扫描下载文件夹')
download_dir = r'C:\Users\Administrator\Downloads'
ffmpeg_dir = r'D:\program\ffmpeg-5.1.2-full_build\bin\ffmpeg'
dict1 = scan_download_dir(download_dir)
if dict1:
print('循环合成文件并输出', dict1)
synthetic_video(dict1)
print('弹窗提醒')
alert_success()