网上看到一个油猴脚本,可以自动下载任何视频,下载后自动生成音频和视频,需要用到ffmpeg对音视频进行合成,于是花了几个小时写了一个python脚本,实现自动扫描并合成视频功能
使用此脚本前,您可能需要配置如下清单
安装油猴脚本
安装无限制下载视频脚本:https://greasyfork.org/zh-CN/scripts/445751-unlimited-downloader
下载ffmpeg音视频合成工具
配置下载目录和ffmpeg目录,分别是脚本中的两个变量:download_dir,ffmpeg_dir
完成上述配置后,即可享受无限制的视频下载及合成
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()
标签:rename,dict1,name,dict2,python,音视频,下载,file,dir From: https://www.cnblogs.com/shijia/p/17112714.html