今天上班的时候看到有人群里问 图文成片怎么实现。 临时给我提供一点写作的灵感,趁着下班写一篇。这里用到 python的moviepy库, 之前文章介绍过。
大体思路:假定有4张图片,每张图片将在视频中展示2秒钟,并且图片会按照顺序循环播放,直到音频结束。
from moviepy.efrom moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips, CompositeVideoClipfrom moviepy.audio.fx.all import audio_loopfrom PIL import Imageimport os
def resize_images(image_files, desired_size): resized_images = [] for image_file in image_files: # 打开一张图片文件 with Image.open(image_file) as img: # 调整图片大小到目标尺寸 img = img.resize(desired_size, Image.ANTIALIAS) # 将调整后的图片保存到临时文件 temp_file = f"temp_{os.path.basename(image_file)}" img.save(temp_file) resized_images.append(temp_file) return resized_images
def create_video_from_images(image_files, audio_file, output_file, fps=24, desired_size=(1280, 720)): # 将图片调整到目标尺寸 resized_images = resize_images(image_files, desired_size)
# 加载音频文件 audio = AudioFileClip(audio_file)
# 设置图片展示时长为2秒 image_duration = 2
# 创建单独的图片剪辑并设置展示时长 clips = [ImageClip(img).set_duration(image_duration).set_fps(fps) for img in resized_images]
# 将所有图片剪辑拼接成循环播放的一个视频剪辑 video_clip = concatenate_videoclips(clips, method="compose").loop(duration=audio.duration)
# 给视频剪辑设置音频 final_video = video_clip.set_audio(audio)
# 将结果写入输出文件 final_video.write_videofile(output_file, codec="libx264", audio_codec="aac", fps=fps)
# 清理临时文件 for temp_file in resized_images: os.remove(temp_file)
# demo:image_files = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg'] # 图片文件列表audio_file = 'background_music.mp3' # 音频文件output_file = 'output_video.mp4' # 输出视频文件
create_video_from_images(image_files, audio_file, output_file)ditor import ImageClip, AudioFileClip, concatenate_videoclips, CompositeVideoClipfrom moviepy.audio.fx.all import audio_loopfrom PIL import Imageimport os
def resize_images(image_files, desired_size): resized_images = [] for image_file in image_files: # 打开一张图片文件 with Image.open(image_file) as img: # 调整图片大小到目标尺寸 img = img.resize(desired_size, Image.ANTIALIAS) # 将调整后的图片保存到临时文件 temp_file = f"temp_{os.path.basename(image_file)}" img.save(temp_file) resized_images.append(temp_file) return resized_images
def create_video_from_images(image_files, audio_file, output_file, fps=24, desired_size=(1280, 720)): # 将图片调整到目标尺寸 resized_images = resize_images(image_files, desired_size)
# 加载音频文件 audio = AudioFileClip(audio_file)
# 设置图片展示时长为2秒 image_duration = 2
# 创建单独的图片剪辑并设置展示时长 clips = [ImageClip(img).set_duration(image_duration).set_fps(fps) for img in resized_images]
# 将所有图片剪辑拼接成循环播放的一个视频剪辑 video_clip = concatenate_videoclips(clips, method="compose").loop(duration=audio.duration)
# 给视频剪辑设置音频 final_video = video_clip.set_audio(audio)
# 将结果写入输出文件 final_video.write_videofile(output_file, codec="libx264", audio_codec="aac", fps=fps)
# 清理临时文件 for temp_file in resized_images: os.remove(temp_file)
# demo:image_files = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg'] # 图片文件列表audio_file = 'background_music.mp3' # 音频文件output_file = 'output_video.mp4' # 输出视频文件
create_video_from_images(image_files, audio_file, output_file)
这里的图片素材采用腾讯智影生成, 音频采用suno生成。最后的效果
领取专属 10元无门槛券
私享最新 技术干货