【Python音视频技术】玩AI视频创作引发写Python音视频技术系列文章1---视频添加字幕

最近对视频创作感兴趣, 详情见之前写的几篇文章。

【AI+应用】模仿爆款视频二次创作短视频操作步骤

【人工智能】AI数字人视频演示

【人工智能】AI视频二次创作演示

作为程序员出身的我,看到一些功能, 我都有猎奇的习惯, 想着自己用什么技术来实现它呢。

处理音视频,大家很容易想到大名鼎鼎的ffmpeg。FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。它提供了录制、转换以及流化音视频的完整解决方案。 2013年那时候做流媒体直播的时候我就有所了解。既然我这里选择python,这里介绍2个python库,一个是ffmpeg-python , 一个是moviepy。

这里以mac为例, windows或linux思路类似。

一、ffmpeg-python, python代码来操作ffmpeg

1、ffmpeg下载地址: https://ffmpeg.org//download.html , 选择自己合适的版本

2、下载解压后,vi /etc/profile修改环境变量,

export PATH=$PATH:/Users/xxxx/software/ffmpeg/bin

3、pip install ffmpeg-python

二、moviePy,是一个用于视频编辑Python库:切割、拼接、标题插入,视频合成,视频处理和自定义效果的创造。

1、必须先安装imagemagick , brew install imagemagick

这个安装比较扯,安装各种缺包,我秉持缺少安装什么 brew install xx的原则,所幸全部安装成功了, 主要是因为我之前已经换成国内源了。

如果你安装不成功,可以尝试换一下中科院的源。https://mirrors.ustc.edu.cn/brew.git

2、pip3 install moviepy

三、这里给一个 moviepy视频添加字幕的简单demo, 网上摘录的代码。

复制代码
from moviepy.editor import CompositeVideoClip, VideoFileClipfrom moviepy.video.VideoClip import TextClipimport osimport argparse
# Define the base directorybase_dir = os.path.dirname(os.path.abspath(__file__))
# Define CLI argumentsparser = argparse.ArgumentParser()parser.add_argument(    '-f',    type=str,    help='The file path of target video. '    + 'ex. /Users/lwj/video/example.mp4.\nDefault value is "demo.mp4"',    default=base_dir + '/demo.mp4')parser.add_argument(    '-t',    type=str,    help='The text you want to add to video top. '    + 'ex. "Hellow Video"',    default='This is demo example')parser.add_argument(    '-s',    type=int,    help='The font size of video watermark. '    + 'ex. 30',    default='30')parser.add_argument(    '-n',    type=str,    help='The new file name.\n'    + 'Default value is {current file name}_title.mp4',)
# Define Variableargs = parser.parse_args()video_path = args.fvideo_watermark = args.tfont_size = args.s
# Check whether or not the input video path is valid. If not, ask user to input again.while True:    try:        video = VideoFileClip(video_path)        print('video resultion: ', video.size)        break    except Exception:        print(            'Directory or file is not valid,'            + ' please enter a valid file directory ...')        video_path = str(input('Enter the video path again (absolute path without space): '))
origin_file = (lambda x: x.split('/')[-1])(video_path)
if args.n:    new_file = args.nelse:    new_file = f'{origin_file.split(".")[0]}_title.{origin_file.split(".")[1]}'

def add_text(content, font_size):    '''    add text on the top of video stream    '''    # txt_clip = (TextClip(content, fontsize=font_size, color='white')    #             .set_position('top')    #             .set_duration(video.duration))    txt_clip = (TextClip(content, fontsize=font_size, color='red')                .set_position('bottom')                .set_duration(1))    result = CompositeVideoClip([video, txt_clip])    result.write_videofile(new_file)

def main():    add_text(video_watermark, font_size)

if __name__ == '__main__':    main()

命令执行: python3 video-textadder.py -f demo.mp4 -t "hello world"

改一下字幕位置、颜色。

未完待续,后续找个时间继续水。 自己不止更新技术类文章, 如果想了解更多,可以关注同名公众号《子晓聊技术》

相关推荐
清水白石0085 分钟前
Python 异步编程深度解析:Cancellation 到底是异常还是控制信号?从 asyncio 取消机制到企业级事务设计最佳实践
开发语言·python
sarasuki9 分钟前
MCP 客户端接入:一行注册一个 GitHub 工具
人工智能·agent·mcp
kolyle11 分钟前
万级 QPS 下的 Token 分发系统架构:从 0 到 1 跑通 AI 时代的“水电煤“
开发语言·人工智能·系统架构·token·qps·极智词元·大模型私有化部署
Patrick在香港25 分钟前
Python 审计香港开放数据目录:两个端点差 10 倍,只有 9.3% 的资源标了「最后修改时间」
开发语言·数据库·python·数据分析·api·数据治理·开放数据
IT毕设梦工厂25 分钟前
计算机毕业设计选题推荐:基于大数据的草鱼价格分析与可视化|毕业设计选题|计算机毕设|选题推荐|毕设指导|项目定制|源码|高质量项目
大数据·hadoop·python·数据挖掘·数据分析·课程设计·大数据毕设项目
GISHUB37 分钟前
基于QGIS 3.44开发Python插件完整教程
python·qgis
古少侠42 分钟前
deepseek转word工具怎么选?DS随心转与4种方案对比实测
人工智能·word·powerpoint
大数据魔法师1 小时前
curl_cffi从入门到实战
爬虫·python
Code_Artist1 小时前
☢︎自然语言 → 机器码:这到底是 AI 编程的终极形态,还是一个伪命题?
人工智能·llm·ai编程
Sylvia33.1 小时前
火星数据体育API|一站式接入足球篮球电竞等18+项目实时数据
java·开发语言·python·websocket·游戏