FFMPEG: Overlay a video on a video after x seconds

I'm currently working on a video editor and there is a feature where you can select a gif from Giphy and overlay it on top of your video after a header transition.

With FFmpeg you can manipulate, combine, add effects with filters. There is a filter called overlay - that we can use.

This did not work as I expected:

To apply a filter after a certain amount of time we'll use the option enable (this option allows for timeline editing).

The below script overlays the gif.mp4 on top of main_video.mp4 during the 1 - 3 seconds duration.

ffmpeg -i main_video.mp4 -i gif.mp4 -filter_complex

"0:v1:voverlay=enable='between(t,1,3)'out"

-map out complete.mp4

** 0:v --> first video (main_video.mp4)

** 1:v --> the second video (gif.mp4)

** out --> first and second video combined into one after the overlay.

Problem: gif.mp4 will start playing from the beginning so after 1 second has passed the gif video will be 1 second in already.

Solution:

Use the setpts filter to delay the overlay video (gif.mp4) start with x seconds.

ffmpeg -i main_video.mp4 -i gif.mp4 -filter_complex

"1:vsetpts=PTS-STARTPTS+1/TBdelayedGif;

0:vdelayedGifoverlay=enable='between(t,1,3)'out"

-map out complete.mp4

The setpts filter evaluates its expression and assigns the value as the timestamp for the current frame it is processing. For a detailed explanation check out this awesome post.

For setpts=PTS-STARTPTS+1/TB:

** +1 is the time in seconds we want to delay our offset

** TB is the timebase.

Too see the gif-overlay in action check out Glitterly - a web based video editor I've been working on.

python 复制代码
import ffmpeg  
  
# 输入文件  
main_video = 'main_video.mp4'  
gif_video = 'gif.mp4'  # 注意:这里假设gif.mp4是一个有效的视频文件,尽管其扩展名可能误导人认为是GIF图像  
output_video = 'complete.mp4'  
  
# 创建ffmpeg流  
# 加载两个输入文件  
stream_main = ffmpeg.input(main_video)  
stream_gif = ffmpeg.input(gif_video)  
  
# 应用filter_complex  
# 设置gif视频的时间戳,使其相对于自身起始点延迟  
# 注意:ffmpeg-python中不需要显式地指定流索引(如[1:v]),因为你可以直接通过ffmpeg.input()返回的流对象来引用  


delayed_gif = ffmpeg.filter_(stream_gif.video, 'setpts', 'PTS-STARTPTS+1/TB')  

  
# 将主视频和延迟后的gif视频进行叠加  
# 只在时间1秒到3秒之间启用overlay  

overlay_filter = ffmpeg.filter_([stream_main.video, delayed_gif], 'overlay',  
                                enable='between(t,1,3)',  
                                x='(main_w-overlay_w)/2',  # 可选:居中gif  
                                y='(main_h-overlay_h)/2')  # 可选:居中gif  
  
# 创建输出流,仅包含处理后的视频  
output_stream = ffmpeg.output(overlay_filter, output_video)  
  
# 运行ffmpeg命令  
ffmpeg.run(output_stream)
相关推荐
左直拳17 小时前
利用海康CVR实现视频流历史回放
ffmpeg·cvr·视频回放·历史视频
MR.欻2 天前
ZLMediaKit 源码分析(四):RTP/RTCP 协议栈实现分析
c++·人工智能·vscode·ffmpeg·音视频
晓py2 天前
音视频基础概念入门_FFmpeg学习笔记
学习·ffmpeg·音视频
daqinzl2 天前
Mpegts.js+FFmpeg+WebSocket+Node实时视频流实现方案
websocket·ffmpeg·node·mpegts.js
qq_369224333 天前
打开剪辑/直播/播放器提示ffmpeg.dll丢失?专属场景修复方法汇总
ffmpeg·dll·dll修复·dll错误
愿天垂怜3 天前
【C++脚手架】ffmpeg 库的介绍与使用
linux·服务器·开发语言·c++·ide·git·ffmpeg
韶博雅3 天前
oracle优化用到的sql
sql·oracle·ffmpeg
kkoral4 天前
视频二进制流RAW文件转图片完整教程
运维·python·ffmpeg·音视频
weixin_421607554 天前
短剧出海的AI 视频翻译技术方案:从单集打样到批量交付的工程全链路
人工智能·ffmpeg
_oP_i4 天前
FFmpeg 安装
ffmpeg