用python写一个程序枚举某目录下以及子目录下所有avi文件并调用ffmpeg将相应的avi文件转换为名称相同的mp4文件
你可以使用Python的os模块和subprocess模块来实现这个功能。下面是一个示例代码:
import os
import subprocess
def convert_avi_to_mp4(input_file):
output_file = os.path.splitext(input_file)[0] + '.mp4'
cmd = ['ffmpeg', '-i', input_file, output_file]
subprocess.run(cmd)
def enumerate_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".avi"):
input_file = os.path.join(root, file)
convert_avi_to_mp4(input_file)
# 用法示例
directory = "/path/to/directory" # 替换为具体的目录路径
enumerate_files(directory)
在上面的代码中,convert_avi_to_mp4函数用于将单个AVI文件转换为MP4文件,它使用了subprocess.run函数来调用ffmpeg命令行工具进行转换。enumerate_files函数用于枚举指定目录及其子目录下的所有AVI文件,并调用convert_avi_to_mp4函数进行转换。
你需要将代码中的/path/to/directory替换为你想要处理的目录的实际路径。
请确保已经安装了ffmpeg,并且它已添加到系统的环境变量中,以便在命令行中可以直接使用
原文地址: https://www.cveoy.top/t/topic/i06f 著作权归作者所有。请勿转载和采集!