import os def convert(from_file, to_file): command = ( "ffmpeg -i " + from_file + " -s 1280x720 -b:v 3M -vcodec h264 -r 29.97 -acodec aac -ab 48k -ac 2 " + to_file ) os.system(command) def get_file_list(fpath): # 定义视频文件的常见扩展名 video_extensions = {".mp4", ".m4v", ".avi", ".mov", ".mkv", ".flv", ".wmv", ".webm"} # 用来存储视频文件名的列表 video_files = [] dir_path = os.path.dirname(os.path.abspath(__file__)) base_path = dir_path + "/" print(dir_path) # 遍历指定目录 for root, dirs, files in os.walk(fpath): for file in files: # 获取文件扩展名 _, ext = os.path.splitext(file) # 如果扩展名是视频格式,则添加到列表中 if ext.lower() in video_extensions: from_file = "./in/"+file name, ext = os.path.splitext(file) to_file = "z_" + name + ".mp4" convert(from_file, to_file) # video_files.append(dir_path + "/" + file) # return video_files if __name__ == "__main__": # 使用示例 directory = "./" # 请替换为你想要遍历的文件夹路径 get_file_list(directory)