gpt4 book ai didi

python - 使用python为视频添加字幕

转载 作者:行者123 更新时间:2023-12-04 03:30:20 24 4
gpt4 key购买 nike

我有一个人们谈话的视频。我也有成绩单。我将单词分块成句子,这样我就可以在屏幕上一次显示 1 个句子,就像电影中的普通字幕一样。为此,我创建了一个 csv,其中每一帧都有一行,每一行都包含该句子时间块中的完整句子。通过这种方式,我遍历所有帧并将句子的文本放在该句子中的每一帧上。我在 OpenCV 中做到了。
样本成绩单 csv:

frame     sentence
0 hello
1 hello
2 how are you
3 how are you
4 how are you
5 how are you
6 how are you
7 how are you
8 fine
...
csv 的长度与视频中的帧数相同。要绘制字幕,我这样做:
import cv2
import pandas as pd

df = pd.read_csv('data.csv')
video = cv2.VideoCapture('vid.mp4')
num_frames = video.get(cv2.CAP_PROP_FRAME_COUNT)

assert len(df) == num_frames

for i in list(range(0, num_frames)):
ret, frame = video.read()
cv2.putText(frame, str(df.sentence), (0,50),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3, cv2.LINE_AA, True)

# additional standard cv2 code below...
这有效,但现在我没有任何音频。我知道 OpenCV 不适用于任何音频,但还有其他解决方法吗?这种方法在我的管道中运行良好,因此我希望能够将这些帧写入新视频,同时保留音频,同时尽可能少地使用其他库。
编辑
使用建议的moviepy解决方案后,我得到一个没有音频的带字幕的视频,并出现以下错误:
Moviepy - Building video vidout.mp4.
MoviePy - Writing audio in vidoutTEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video vidout.mp4

t: 100%|████████████████████████████████████████████▉| 23069/23084 [07:26<00:00, 66.35it/s, now=None]Traceback (most recent call last):
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/Clip.py", line 472, in iter_frames
frame = self.get_frame(t)
File "<decorator-gen-11>", line 2, in get_frame
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/Clip.py", line 93, in get_frame
return self.make_frame(t)
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/Clip.py", line 136, in <lambda>
newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/video/VideoClip.py", line 490, in <lambda>
return self.fl(lambda gf, t: image_func(gf(t)), apply_to)
File "make_demo.py", line 65, in pipeline
cv2.putText(frame, str(next(dfi)[1].word), (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3, cv2.LINE_AA, True)
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "make_demo.py", line 72, in <module>
out_video.write_videofile("vidout.mp4", audio=True)
File "<decorator-gen-55>", line 2, in write_videofile
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/decorators.py", line 54, in requires_duration
return f(clip, *a, **k)
File "<decorator-gen-54>", line 2, in write_videofile
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/decorators.py", line 135, in use_clip_fps_by_default
return f(clip, *new_a, **new_kw)
File "<decorator-gen-53>", line 2, in write_videofile
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/decorators.py", line 22, in convert_masks_to_RGB
return f(clip, *a, **k)
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/video/VideoClip.py", line 307, in write_videofile
logger=logger)
File "/Users/asi/anaconda3/lib/python3.7/site-packages/moviepy/video/io/ffmpeg_writer.py", line 221, in ffmpeg_write_video
fps=fps, dtype="uint8"):
RuntimeError: generator raised StopIteration

最佳答案

如果一个额外的库没问题,你可以使用 moviepy有音频支持:

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)

关于python - 使用python为视频添加字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67009452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com