import os
import random
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip
def convert_mp3_to_mp4(mp3_directory, output_directory, background_dir, width=1920, height=1280):
# 获取所有背景图文件列表
background_files = [os.path.join(background_dir, f) for f in os.listdir(background_dir) if f.endswith(('.jpg', '.jpeg', '.png'))]
# 确保输出目录存在
os.makedirs(output_directory, exist_ok=True)
# 遍历MP3目录中的所有文件
for mp3_file in os.listdir(mp3_directory):
if mp3_file.endswith('.mp3'):
mp3_path = os.path.join(mp3_directory, mp3_file)
# 随机选择一个背景图
background_file = random.choice(background_files)
# 加载音频文件
audio_clip = AudioFileClip(mp3_path)
# 创建一个空白视频剪辑,使用选定的视频尺寸
blank_clip = VideoFileClip(background_file, audio=False).resize(width=width, height=height)
# 合并音频和背景视频
video_clip = CompositeVideoClip([blank_clip.set_audio(audio_clip)])
# 生成输出文件路径
output_path = os.path.join(output_directory, f"{os.path.splitext(mp3_file)[0]}.mp4")
# 写入视频文件
video_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')
# 示例用法
mp3_directory = r'D:\Work_Tools\chinaminjian\txtcn' # 输入MP3文件所在的目录
output_directory = r'D:\Work_Tools\chinaminjian\txtcnmp4' # 输出MP4文件的目录
background_dir = r'D:\归档\pexels风景图' # 包含背景图的目录
convert_mp3_to_mp4(mp3_directory, output_directory, background_dir)
pip install moviepy
import os
import random
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip, ImageClip
def convert_mp3_to_mp4(mp3_directory, output_directory, background_dir, width=1920, height=1280, background_change_interval=1):
# 获取所有背景图文件列表
background_files = [os.path.join(background_dir, f) for f in os.listdir(background_dir) if f.endswith(('.jpg', '.jpeg', '.PNG'))]
# 确保输出目录存在
os.makedirs(output_directory, exist_ok=True)
# 遍历MP3目录中的所有文件
for mp3_file in os.listdir(mp3_directory):
if mp3_file.endswith('.mp3'):
mp3_path = os.path.join(mp3_directory, mp3_file)
# 加载音频文件
audio_clip = AudioFileClip(mp3_path)
# 创建一个空白视频剪辑,使用选定的视频尺寸
blank_clip = ImageClip(background_files[0], duration=audio_clip.duration).resize(width=width, height=height)
# 合并音频和背景视频
video_clip = CompositeVideoClip([blank_clip.set_audio(audio_clip)])
# 为每隔一秒更换一张背景图
for i in range(10, int(audio_clip.duration), background_change_interval):
current_background = random.choice(background_files)
current_clip = ImageClip(current_background, duration=min(background_change_interval, audio_clip.duration - i)).resize(width=width, height=height)
video_clip = CompositeVideoClip([video_clip.set_duration(i), current_clip.set_start(i)])
# 生成输出文件路径
output_path = os.path.join(output_directory, f"{os.path.splitext(mp3_file)[0]}.mp4")
# 写入视频文件
video_clip.write_videofile(output_path, codec='libx264', audio_codec='aac', fps=24) # 设置fps为帧数,可根据需要调整
# 示例用法
mp3_directory = r'D:\Work_Tools\chinaminjian\txtcn' # 输入MP3文件所在的目录
output_directory = r'D:\Work_Tools\chinaminjian\txtcnmp4' # 输出MP4文件的目录
background_dir = r'D:\Work_Tools\chinaminjian\民间故事图片素材2020102' # 包含背景图的目录
convert_mp3_to_mp4(mp3_directory, output_directory, background_dir)