ffmpeg视频剪辑

环境 与背景:

centos 8

最近在做短视频的混剪,视频内容解析,视频合成。其中利用gpt生成文案,然后合成语音,将视频素材切分成不同片段。然后将这些片段与语音合成新的视频。

1 、安装 ffmpeg

安装方式有三种:

1、yum install FFmpeg (具体方法请网上查询)

2、直接下载编译好的二进制文件ffmpeg, 然后将文件复制到环境路径中,如:/usr/local/bin

3、源码编译安装。

我使用的是源码编译安装,即 方式3。具体安装方式参考:https://trac.ffmpeg.org/wiki/CompilationGuide/Centos#FFmpeg
但是,最后一步,即编译FFmpeg的方式有区别,完整命令如下:

PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure   --prefix="$HOME/ffmpeg_build"   --pkg-config-flags="--static"   --extra-cflags="-I$HOME/ffmpeg_build/include"   --extra-ldflags="-L$HOME/ffmpeg_build/lib"   --extra-libs=-lpthread   --extra-libs=-lm   --bindir="$HOME/bin"   --enable-gpl   --enable-libfdk_aac   --enable-libfreetype   --enable-libmp3lame   --enable-libopus   --enable-libvpx   --enable-libx264   --enable-libx265   --enable-nonfree  --enable-libass 

即增加了lib: --enable-libass , 增加该库的原因是: 在给视频增加用-vf加上字幕文件,是需要 ass库的支持。

参考资料

https://www.pengrl.com/p/20029/

https://blog.csdn.net/weixin_45617478/article/details/103336168

https://blog.csdn.net/weixin_54720578/article/details/132901995

macOS上安装很简单,直接用brew安装。

2、常用剪辑功能

分段切片视频

ffmpeg -y  -i 12b8162de5e626a8ffd7851c2bb1d5ba.mp4 -ss 0.0 -t 14.0 -an -q 0 sub1709518126-777.mts

合并视频

将生成的 mts 片段合并成一个视频。将mts视频名称填写到一个 txt文件中,如下所示:

视频文件: videos1710730113.txt

file sub1710730098-607.mts
file sub1710730104-727.mts
file sub1710730106-630.mts
file sub1710730113-937.mts

cd {dest_path} &&  ffmpeg -y -f concat  -i {video_file_name} -c copy {dest_name}

其中:

dest_path - 视频所在目录

video_file_name: 视频文件名, 即 videos1710730113.txt

dest_name: 输出视频名

合并视频与音频

ffmpeg -y  -i dest_1709518127_clip.mp4 -i tts-1709518124.mp3 -c:v  copy -c:a aac -strict experimental  dest_1709518127_clip_au.mp4

给视频添加字幕

字幕文件:

1709518125.srt

1
00:00:00,20 --> 00:00:01,30
大家好 今

2
00:00:01,30 --> 00:00:01,980
天给大家介绍

3
00:00:01,980 --> 00:00:02,890
一款睫毛贴

ffmpeg -y -i dest_1709518127_clip_au.mp4 -vf "subtitles=1709518125.srt:force_style='Fontsize=16'" dest_1709518127_clip_st.mp4

3、问题:

时间戳偏移问题

问题的产生

使用python的VideoFileClip进行视频合并时, 会发现实际输出的视频时长变短。 原因就是时间戳偏移,导致切视频的时间不是设定的时间。

Non-monotonic DTS in output stream 0:0; previous: 367247, current: 367104; changing to 367248. This may result in incorrect timestamps in the output file.

解决方法: 使用系统 ffmpeg 命令直接进行视频切分成mts片段。

python 代码

import subprocess

command = " cd {dest_path} &&  ffmpeg -y  -i {video_name} -i {audio_name} -c:v  copy -c:a aac -strict experimental  {dest_name}".format(dest_path=sub_video_path, video_name=dest_name, audio_name=audio_name, dest_name=dest_name_au)
    result = subprocess.run(command, shell=True, capture_output=True, text=True)

其中:

sub_video_path - 视频所在目录,

dest_name - 原视频文件名称

audio_name - 声音文件名称

dest_name_au - 输出文件名称

相关推荐
数据媛14 小时前
机器学习_13 决策树知识总结
人工智能·python·决策树·机器学习·numpy·pandas·sklearn
cuijiecheng201817 小时前
音视频入门基础:RTP专题(9)——FFmpeg接收RTP流的原理和内部实现
ffmpeg·音视频
数据媛18 小时前
机器学习_18 K均值聚类知识点总结
python·机器学习·均值算法·numpy·pandas·scikit-learn·聚类
偶是老李头19 小时前
Ubuntu虚拟机NDK编译ffmpeg
linux·ubuntu·ffmpeg·android ndk
lucky-billy1 天前
Qt 中使用 ffmpeg 获取采集卡数据录制视频
qt·ffmpeg·音视频
cuijiecheng20181 天前
FFmpeg源码:url_find_protocol函数分析
ffmpeg
大懒猫软件1 天前
使用 Python 爬虫和 FFmpeg 爬取 B 站高清视频
爬虫·python·ffmpeg
柯木超2 天前
使用 Swift 完成FFmpeg音频录制、播放和视频格式转换应用
ffmpeg
hjjdebug3 天前
ffmpeg configure 研究1-命令行参数的分析
ffmpeg·configure·bash 脚本
SimpleForest3 天前
FFmpeg Audio options
ffmpeg