posts - 11,  comments - 0,  views - 717
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

moviepy==1.0.2
使用moviepy实现字符的淡入淡出(此淡入淡出,不是透明度形式上的,目前还没有找到方法实现透明度上的淡入淡出)

# 淡入不渐变🆗
import moviepy.editor as mpe
import numpy as np

# 旋转矩阵函数
rotMatrix = lambda a: np.array([[np.cos(a), np.sin(a)], [-np.sin(a), np.cos(a)]])


def fadein_effect(tclip, fadein_duration):
    """处理淡入效果"""
    return tclip.fadein(fadein_duration)


def fadeout_effect(tclip, fadeout_duration):
    """处理淡出效果"""
    return tclip.fadeout(fadeout_duration)


def txtanimation(screen_size, letter_size, character, func, offsetx, offsety, fontsize=70, color='white', fadein_duration=1,
                 fadeout_duration=1, letter_duration=2):

    screenw = screen_size[0]  # 屏幕宽度
    screenh = screen_size[1]  # 屏幕高度
    letterw = letter_size[0]  # 单个字符宽度
    letterh = letter_size[1]  # 单个字符高度
    letternum = len(character)  # 字符数量

    # 字符的起始位置计算
    letteronex = (screenw - letterw * letternum) / 2 + offsetx  # 水平偏移
    letteroney = (screenh - letterh) / 2 + offsety  # 垂直偏移

    tclips = []  # 剪辑列表
    for i in range(letternum):
        # 创建单个字符文本剪辑,并设置持续时间
        tclip = mpe.TextClip(
            character[i],
            color=color,  # 设置字体颜色
            font='KaiTi',
            kerning=5,
            fontsize=fontsize,  # 设置字体大小
            size=letter_size,
        )

        # 设置字符的初始位置
        tclip = tclip.set_pos((letteronex + i * letterw, letteroney))

        # 调用特效函数来调整字符的位置
        tclip = tclip.set_pos(func(tclip.pos(0), i, letternum))
        # 为每个字符设置持续时间:依次减少
        tclip = tclip.set_duration(15)  # 每个字符的持续时间逐渐减小

        # 先进行淡入效果
        tclip = fadein_effect(tclip, fadein_duration)

        # 设置淡出效果,并按顺序延迟开始时间
        tclip = fadeout_effect(tclip, fadeout_duration).set_start(i * letter_duration)  # 每个字符按顺序渐变

        # 添加到剪辑列表
        tclips.append(tclip)

    return tclips


# 加载背景图片并设置持续时间
iclip = mpe.ImageClip('1128.jpg').set_duration(25)

# 屏幕和字符的大小设置
screensize = iclip.size
lettersize = (80, 80)
text = ['s', '啊', '及', 's', 'a', 'Q', 's']

# 创建带有字符动画的剪辑
clip1 = mpe.CompositeVideoClip(
    txtanimation(screensize, lettersize, text, lambda screenpos, i, nletters: screenpos, 50, 100, fontsize=100,
                 color='red', fadein_duration=5, fadeout_duration=5, letter_duration=2),
    size=screensize
).subclip(2, 25)

# 合成最终的视频
clip = mpe.CompositeVideoClip([iclip, clip1])

# 输出视频文件
clip.write_videofile('letter_with_fade_order_separated.mp4', codec="libx264", fps=24)
posted on   msms123  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示