请检查代码中的语法错误提示:NameError name get_output_filename is not definedimport osimport globfrom natsort import natsorteddir_path = rEhomeupoutput_dir = rEhomeup1# 当前文件夹下的所有mp4路径L = for root dirs files in oswa
请将函数get_output_filename()的定义提前到调用它的代码之前。因为在生成concat_command时,调用了get_output_filename()函数,但此时该函数还未定义,因此会出现NameError错误。将函数定义提前后,即可解决该问题。代码如下:
import os import glob from natsort import natsorted
dir_path = r'E:\home\up' output_dir = r'E:\home\up\1'
截取文件名并重命名输出文件
def get_output_filename(input_file): filename_without_extension, extension = os.path.splitext(os.path.basename(input_file)) if filename_without_extension.startswith('h_'): start_index = 2 else: start_index = 1 return f"{filename_without_extension[start_index:].replace('00', '-', 1).upper()}{extension}"
当前文件夹下的所有mp4路径
L = [] for root, dirs, files in os.walk(dir_path): files = natsorted(files) for file in files: if os.path.splitext(file)[1] == '.mp4': file_path = os.path.join(root, file) L.append(file_path)
生成ffmpeg命令
commands = [] for l in L: ts_file = os.path.join(os.path.dirname(l), os.path.splitext(os.path.basename(l))[0] + '.ts') commands.append(f"ffmpeg -i "{l}" -vcodec copy -acodec copy -bsf:v h264_mp4toannexb "{ts_file}"")
concat_command = ( f'ffmpeg -i "concat:{"|".join([os.path.join(os.path.dirname(l), os.path.splitext(os.path.basename(l))[0] + ".ts") for l in L])}" ' '-acodec copy -vcodec copy -bsf:a aac_adtstoasc ' f'"{os.path.join(output_dir, get_output_filename(L[0]))}"' )
执行ffmpeg命令
for command in commands: os.system(command)
os.system(concat_command)
删除ts文件
for ts_file in glob.glob(os.path.join(dir_path, '*.ts')): os.remove(ts_file)
重命名输出文件
for mp4_file in glob.glob(os.path.join(output_dir, '*.mp4')): os.rename(mp4_file, os.path.join(output_dir, get_output_filename(mp4_file))
原文地址: https://www.cveoy.top/t/topic/dcBs 著作权归作者所有。请勿转载和采集!