竖屏版本
import os import subprocess def add_text_to_mp4(directory): # 获取目录下所有的MP4文件 mp4_files = [file for file in os.listdir(directory) if file.endswith(".mp4")] # 设置输出目录 output_directory = os.path.join(directory, "Final") os.makedirs(output_directory, exist_ok=True) for mp4_file in mp4_files: # 获取MP4文件的完整路径 input_path = os.path.join(directory, mp4_file) # 获取MP4文件名,并按照要求处理 file_name = os.path.splitext(mp4_file)[0] # 如果文字超过10个字,则进行换行 if len(file_name) > 10: lines = [file_name[i:i+10] for i in range(0, len(file_name), 10)] formatted_name = '\n'.join(lines) else: formatted_name = file_name # 构建 FFmpeg 命令行 output_path = os.path.join(output_directory, f'{mp4_file}') cmd = [ "ffmpeg", "-i", input_path, "-vf", f"drawtext=text='{formatted_name}':fontsize=50:fontfile=YouSheBiaoTiHei-2.ttf:box=1:boxcolor=black@0.5:boxborderw=10:fontcolor=white:x=(w-text_w)/2:y=((h-text_h)/2)-500,drawtext=text='':enable='between(t,0,0)'", "-codec:v", "libx264", "-preset", "slow", "-crf", "18", "-c:a", "copy", "-y", output_path ] try: subprocess.run(cmd, check=True) except subprocess.CalledProcessError as e: print(f"Error processing {mp4_file}: {e}") if __name__ == "__main__": # 指定目录路径 directory_path = r"C:\Users\gd\Pictures\MP41" # 调用函数添加文本到MP4文件 add_text_to_mp4(directory_path)
1秒之后显示
import os import subprocess def add_text_to_mp4(directory): # 获取目录下所有的MP4文件 mp4_files = [file for file in os.listdir(directory) if file.endswith(".mp4")] # 设置输出目录 output_directory = os.path.join(directory, "Final_1920x1080") os.makedirs(output_directory, exist_ok=True) for mp4_file in mp4_files: # 获取MP4文件的完整路径 input_path = os.path.join(directory, mp4_file) # 获取MP4文件名,并按照要求处理 file_name = os.path.splitext(mp4_file)[0] # 如果文字超过10个字,则进行换行 if len(file_name) > 20: lines = [file_name[i:i+20] for i in range(0, len(file_name), 20)] formatted_name = '\n'.join(lines) else: formatted_name = file_name # 构建 FFmpeg 命令行 output_path = os.path.join(output_directory, f'{mp4_file}') cmd = [ "ffmpeg", "-i", input_path, "-vf", f"drawtext=text='{formatted_name}':fontsize=80:fontfile=YouSheBiaoTiHei-2.ttf:box=1:boxcolor=black@0.5:boxborderw=20:fontcolor=white:x=(w-text_w)/2:y=((h-text_h)/2):enable='gte(t\,1)',drawtext=text='':enable='between(t,0,0)'", "-c:v", "libx265", "-c:a", "libmp3lame", "-y", output_path ] try: subprocess.run(cmd, check=True) except subprocess.CalledProcessError as e: print(f"Error processing {mp4_file}: {e}") if __name__ == "__main__": # 指定目录路径 directory_path = r"D:\Work_Tools\chinaminjian\txtcnmp4" # 调用函数添加文本到MP4文件 add_text_to_mp4(directory_path)
打包源码
import os import subprocess def add_text_to_mp4(directory): # 获取目录下所有的MP4文件 mp4_files = [file for file in os.listdir(directory) if file.endswith(".mp4")] # 设置输出目录 output_directory = os.path.join(directory, "In-Title") os.makedirs(output_directory, exist_ok=True) for mp4_file in mp4_files: # 获取MP4文件的完整路径 input_path = os.path.join(directory, mp4_file) # 获取MP4文件名,并按照要求处理 file_name = os.path.splitext(mp4_file)[0] # 如果文字超过10个字,则进行换行 if len(file_name) > 12: lines = [file_name[i:i+12] for i in range(0, len(file_name), 12)] formatted_name = '\n'.join(lines) else: formatted_name = file_name # 构建 FFmpeg 命令行 output_path = os.path.join(output_directory, f'{mp4_file}') cmd = [ "ffmpeg", "-i", input_path, "-vf", f"drawtext=text='{formatted_name}':fontsize=70:fontfile=SourceHanSansSC-Heavy-2.otf:box=1:boxcolor=black@0.7:boxborderw=20:fontcolor=white:x=(w-text_w)/2:y=((h-text_h)/2)+200:enable='eq(t,0)'", "-c:v", "libx264", "-c:a", "copy", "-y", output_path ] try: subprocess.run(cmd, check=True) except subprocess.CalledProcessError as e: print(f"Error processing {mp4_file}: {e}") if __name__ == "__main__": # 指定目录路径 directory_path = "." # 调用函数添加文本到MP4文件 add_text_to_mp4(directory_path)