You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python+FFmpeg为图片对比视频添加动态滑块线条

复刻imgsli平滑滑块对比视频并添加动态滑块样式

一、优化滑块移动的平滑度(模拟人工操作)

原脚本使用线性公式W*T/{duration}实现滑块匀速移动,缺少自然的加速/减速过程。我们可以用FFmpeg的tween函数实现缓入缓出的平滑效果,更贴近人工拖动滑块的体验。

常用缓动类型:cubic(三次方缓动)、sine(正弦缓动)、quad(二次方缓动),可根据需求调整。

二、添加动态滑块样式

有两种实现方式:使用外部滑块图片,或直接用FFmpeg滤镜生成滑块。

方案1:使用外部滑块图片

假设你已有滑块图片(如slider.png),修改后的代码如下:

import subprocess

def create_comparison_video(image_a, image_b, slider_img, output_video, duration=5, frame_rate=30, video_width=1920, video_height=1080):
    ffmpeg_cmd = [
        'ffmpeg',
        '-y',
        '-loop', '1',
        '-i', image_a,
        '-loop', '1',
        '-i', image_b,
        '-loop', '1',
        '-i', slider_img,  # 引入滑块图片
        '-filter_complex',
        # 缩放两张对比图
        f"[0]scale={video_width}:{video_height}[img1];"
        f"[1]scale={video_width}:{video_height}[img2];"
        # 缩放滑块图片(调整宽度为40,高度匹配视频)
        f"[2]scale=40:{video_height}[slider];"
        # 平滑混合两张图,使用cubic缓动函数
        f"[img1][img2]blend=all_expr='if(gte(X,W*tween(T/{duration},\"cubic\")),A,B)':shortest=1[blended];"
        # 将滑块叠加到混合视频上,滑块中心与分割线对齐
        f"[blended][slider]overlay=x='W*tween(T/{duration},\"cubic\") - 20':y=0:shortest=1,"
        f"format=yuv420p",
        '-t', str(duration),
        '-r', str(frame_rate),
        '-c:v', 'libx264',
        '-preset', 'slow',
        '-crf', '12',
        output_video
    ]

    subprocess.run(ffmpeg_cmd, check=True)

# 调用示例
# create_comparison_video("img_a.jpg", "img_b.jpg", "slider.png", "output.mp4")

方案2:用FFmpeg内置滤镜生成滑块(无需外部图片)

直接通过drawbox和drawcircle生成滑块样式,避免依赖外部图片:

import subprocess

def create_comparison_video(image_a, image_b, output_video, duration=5, frame_rate=30, video_width=1920, video_height=1080):
    ffmpeg_cmd = [
        'ffmpeg',
        '-y',
        '-loop', '1',
        '-i', image_a,
        '-loop', '1',
        '-i', image_b,
        '-filter_complex',
        # 缩放两张对比图
        f"[0]scale={video_width}:{video_height}[img1];"
        f"[1]scale={video_width}:{video_height}[img2];"
        # 生成透明背景的滑块:白色竖线+圆形手柄
        f"color=s={video_width}x{video_height}:c=black@0,"
        f"drawbox=x='W*tween(T/{duration},\"cubic\") - 2':y=0:w=4:h={video_height}:c=white,"
        f"drawcircle=x='W*tween(T/{duration},\"cubic\")':y={video_height}/2:r=20:color=white:thickness=2[slider];"
        # 平滑混合两张图
        f"[img1][img2]blend=all_expr='if(gte(X,W*tween(T/{duration},\"cubic\")),A,B)':shortest=1[blended];"
        # 叠加滑块
        f"[blended][slider]overlay=shortest=1,"
        f"format=yuv420p",
        '-t', str(duration),
        '-r', str(frame_rate),
        '-c:v', 'libx264',
        '-preset', 'slow',
        '-crf', '12',
        output_video
    ]

    subprocess.run(ffmpeg_cmd, check=True)

# 调用示例
# create_comparison_video("img_a.jpg", "img_b.jpg", "output.mp4")

关键修改说明

  • 平滑移动:用tween(T/{duration}, "cubic")替代线性公式,实现滑块从静止加速、中间匀速、末尾减速的自然运动。
  • 滑块对齐:通过x='W*tween(...) - 滑块宽度/2'确保滑块中心与分割线完全重合。
  • 样式调整:可修改滑块的宽度、颜色、手柄大小等参数,匹配你需要的样式。

内容的提问来源于stack exchange,提问作者Furkan Gözükara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 18:41:03