During the actual development at work recently, I encountered a need to implement python to add subtitles to a video. I didn’t know where to start. I finally asked for advice on the AI tool for code generation and realized this need. I will record it here. For your reference, after I solved the problem, I thought it would be better to write down all the pitfalls clearly so that everyone can avoid pitfalls.
In order to achieve this task, we need to use some libraries in Python to implement video and subtitle processing. Specifically, we can use the OpenCV and MoviePy libraries to process videos, and the pycaption library to process subtitles.
Here is a sample code that accomplishes this task:
```python
import cv2
from moviepy.editor import VideoFileClip
import pycaption
# 读取视频文件
clip = VideoFileClip("video.mp4")
# 读取字幕文件
with open("subtitle.srt", "r", encoding="utf-8") as f:
subtitle = pycaption.WebVTTReader().read(f.read()).get_captions("en-US")
# 将字幕转换为SRT格式
srt = pycaption.SRTWriter().write(subtitle)
# 解析SRT格式的字幕内容
cues = pycaption.SRTReader().read(srt)
# 在视频中添加字幕
for cue in cues:
# 获取字幕时间
start = cue.start
end = cue.end
# 将视频中的每一帧读取为图像
for i, frame in enumerate(clip.iter_frames(start=start, end=end)):
# 将字幕渲染到图像上
img = cv2.putText(frame, cue.plaintext(), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# 写入包含字幕的视频帧
cv2.imwrite("frame_{}.png".format(i), img)
# 将带有字幕的视频帧合成为视频文件
clip_with_subtitle = cv2.VideoWriter("video_with_subtitle.mp4", cv2.VideoWriter_fourcc(*"mp4v"), clip.fps,
(clip.w, clip.h), True)
for i in range(len(cues)):
frame = cv2.imread("frame_{}.png".format(i))
clip_with_subtitle.write(frame)
# 清理临时文件
clip_with_subtitle.release()
cv2.destroyAllWindows()
This code is brought to you by the AI assistant, the v-letter applet: