目录
moviepy
python(opencv + pyaudio + moviepy)实现录制音视频文件并合并_ubuntu使用python的sounddevice+opencv录制音视频-CSDN博客
ImageMagick安装
关于Window10系统在Python上安装Imagemagick遇到的问题及解决方案_imagemagick环境变量-CSDN博客
ffmpeg命令合成:
【实操】python opencv将图片合成视频,并插入音频_python 视频添加音乐-CSDN博客
添加字幕文件:
python
from moviepy.editor import *
import pysrt
def add_subtitles(video_path, subtitles_path, output_path):
# 读取视频和字幕文件
video = VideoFileClip(video_path)
subs = pysrt.open(subtitles_path)
# 创建一个函数,用于在视频中添加字幕
def add_subtitle_frame(frame, t):
subtitle = subs.at(t) # 获取当前时间点的字幕
txt = TextClip(subtitle.text, fontsize=24, color='white', stroke_width=2) # 创建一个字幕文本片段
txt = txt.set_position(('center', 'bottom')).set_duration(video.duration) # 设置字幕位置和持续时间
return CompositeVideoClip([frame, txt]) # 将字幕和视频帧合并
# 将字幕添加到视频中的每一帧
video = video.fl(add_subtitle_frame)
# 保存添加字幕后的视频
video.write_videofile(output_path)
# 调用函数来给视频添加字幕
add_subtitles('video.mp4', 'subtitles.srt', 'output.mp4')
添加字幕文本:
python
import cv2
from moviepy.editor import *
# 打开视频文件
video = VideoFileClip("input.mp4")
# 获取视频的帧速率
fps = video.fps
# 获取视频的宽度和高度
width, height = video.size
# 初始化一个文本剪辑
text = TextClip("Hello, World!", fontsize=70, color="white", bg_color="black")
# 将文本剪辑添加到视频中间
text = text.set_position(("center", "center")).set_duration(video.duration)
# 将文本剪辑与视频合并
final = CompositeVideoClip([video, text])
# 保存最终的视频文件
final.write_videofile("output.mp4", fps=fps)
pipeline添加字幕:
python
import cv2
import pandas as pd
from moviepy.editor import VideoFileClip
def pipeline(frame):
try:
cv2.putText(frame, str(next(dfi)[1].sentence), (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3, cv2.LINE_AA, True)
except StopIteration:
pass
# additional frame manipulation
return frame
dfi = pd.read_csv('data.csv').iterrows()
video = VideoFileClip("vid.mp4")
out_video = video.fl_image(pipeline)
out_video.write_videofile("vidout.mp4", audio=True)