通过Python将视频添加图片

发布于:2024-07-02 ⋅ 阅读:(14) ⋅ 点赞:(0)
from PIL import Image
from moviepy.editor import *

from configs.settings import PROJECT_PATH


def movie_add_image(video_config, type, video_path, out_path):
    # 加载视频文件
    video = VideoFileClip(video_path)

    all_time = 0
    for config in video_config:
        image = config.get('top_image')
        time = config.get('time')
        if image:
            # 打开图片
            img = Image.open(f'{PROJECT_PATH}/src/{type}/{image}')
            new_img = img.resize((200, 300))
            new_img.save(f'{PROJECT_PATH}/src/{type}/tmp_{image}')

            # 添加图片
            image_clip = ImageClip(f'{PROJECT_PATH}/src/{type}/tmp_{image}')
            image_clip = image_clip.set_position("center").set_start(all_time).set_duration(time)

            # 将文字和音乐添加到视频中
            video = CompositeVideoClip([video, image_clip])
        all_time += time

    # 导出视频
    video.write_videofile(out_path, codec='mpeg4', audio_codec='aac')
[{'subtitle': '盼望着,盼望着,东风来了,春天的脚步近了。', 'image': '1.png', 'time': 3}, {'subtitle': '一切都像刚睡醒的样子,欣欣然张开了眼。', 'image': '1.png', 'time': 3}, {'subtitle': '山朗润起来了,水涨起来了,太阳的脸红起来了。', 'image': '1.png', 'time': 3}, {'subtitle': '不好意思打断下,先来介绍下最新的显卡排名。', 'image': '1.png', 'top_image': '9.png', 'time': 4}, {'subtitle': 'xxxxxxx第一名。', 'image': '1.png', 'top_image': '9.png', 'time': 2}, {'subtitle': 'xxxxxxx第2名。', 'image': '2.png', 'top_image': '9.png', 'time': 3}, {'subtitle': 'xxxxxxx第3名。', 'image': '2.png', 'top_image': '9.png', 'time': 3}, {'subtitle': 'xxxxxxx第4名。', 'image': '2.png', 'top_image': '9.png', 'time': 2}, {'subtitle': 'xxxxxxx第5名。', 'image': '2.png', 'top_image': '9.png', 'time': 3}, {'subtitle': '园子里,田野里,瞧去,一大片一大片满是的。', 'image': '2.png', 'time': 3}, {'subtitle': '坐着,躺着,打两个滚,踢几脚球,', 'image': '2.png', 'time': 3}, {'subtitle': '赛几趟跑,捉几回迷藏。风轻悄悄的,草软绵绵的。', 'image': '2.png', 'time': 4}, {'subtitle': '桃树、杏树、梨树,你不让我,我不让你,都开满了花赶趟儿。', 'image': '3.png', 'time': 5}, {'subtitle': '红的像火,粉的像霞,白的像雪。', 'image': '3.png', 'time': 2}, {'subtitle': '花里带着甜味儿,闭了眼,树上仿佛已经满是桃儿、杏儿、梨儿。', 'image': '3.png', 'time': 5}, {'subtitle': '花下成千成百的蜜蜂嗡嗡地闹着,大小的蝴蝶飞来飞去。', 'image': '3.png', 'time': 4}, {'subtitle': '野花遍地是:杂样儿,有名字的,没名字的,', 'image': '3.png', 'time': 3}, {'subtitle': '散在花丛里,像眼睛,像星星,还眨呀眨的。', 'image': '3.png', 'time': 3}, {'subtitle': '“吹面不寒杨柳风”,不错的,像母亲的手抚摸着你。', 'image': '4.png', 'time': 4}, {'subtitle': '风里带来些新翻的泥土的气息,混着青草味儿,', 'image': '4.png', 'time': 3}, {'subtitle': '还有各种花的香,都在微微润湿的空气里酝酿。', 'image': '4.png', 'time': 3}, {'subtitle': '鸟儿将巢安在繁花嫩叶当中,高兴起来了,', 'image': '4.png', 'time': 3}, {'subtitle': '呼朋引伴地卖弄清脆的喉咙,唱出宛转的曲子,', 'image': '4.png', 'time': 3}, {'subtitle': '跟轻风流水应和着。牛背上牧童的短笛,', 'image': '4.png', 'time': 3}, {'subtitle': '这时候也成天在嘹亮地响着。', 'image': '4.png', 'time': 2}, {'subtitle': '雨是最寻常的,一下就是三两天。可别恼。', 'image': '5.png', 'time': 4}, {'subtitle': '看,像牛毛,像花针,像细丝,密密地斜织着,', 'image': '5.png', 'time': 3}, {'subtitle': '人家屋顶上全笼着一层薄烟。树叶儿却绿得发亮,', 'image': '5.png', 'time': 4}, {'subtitle': '小草也青得逼你的眼。傍晚时候,上灯了,', 'image': '5.png', 'time': 3}, {'subtitle': '一点点黄晕的光,烘托出一片这安静而和平的夜。', 'image': '5.png', 'time': 4}, {'subtitle': '在乡下,小路上,石桥边,有撑起伞慢慢走着的人;', 'image': '6.png', 'time': 4}, {'subtitle': '还有地里工作的农民,披着蓑戴着笠。他们的草屋,', 'image': '6.png', 'time': 3}, {'subtitle': '稀稀疏疏的,在雨里静默着。', 'image': '6.png', 'time': 2}, {'subtitle': '天上风筝渐渐多了,地上孩子也多了。', 'image': '7.png', 'time': 3}, {'subtitle': '城里乡下,家家户户,老老小小,', 'image': '7.png', 'time': 2}, {'subtitle': '也赶趟儿似的,一个个都出来了。', 'image': '7.png', 'time': 3}, {'subtitle': '舒活舒活筋骨,抖擞抖擞精神,', 'image': '7.png', 'time': 2}, {'subtitle': '各做各的一份儿事去,“一年之计在于春”;', 'image': '7.png', 'time': 3}, {'subtitle': '刚起头儿,有的是工夫,有的是希望。', 'image': '7.png', 'time': 2}, {'subtitle': '春天像刚落地的娃娃,从头到脚都是新的,它生长着。', 'image': '8.png', 'time': 4}, {'subtitle': '春天像小姑娘,花枝招展的,笑着,走着。', 'image': '8.png', 'time': 3}, {'subtitle': '春天像健壮的青年,有铁一般的胳膊和腰脚,他领着我们上前去。', 'image': '8.png', 'time': 5}]

1.通过VideoFileClip加载需要处理的视频

2.通过Image类,来修改图片的尺寸到指定大小

3.通过ImageClip来加载需要添加的图片
可以设置图片的显示位置set_position
开始显示时间set_start
停留时间set_duration

4.通过CompositeVideoClip类,来合并视频和图片