python中pysrt+moviepy加载srt字幕文件合并到视频中生成新视频

代码语言:python

所属分类:其他

代码描述:python中pysrt+moviepy加载srt字幕文件合并到视频中生成新视频,这样视频就不需要单独加载字幕了。字幕融进了视频中。

代码标签: python pysrt moviepy 加载 srt 字幕 文件 合并 视频 生成 新视频

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
#pip install moviepy
#pip install pysrt
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
import pysrt

def time_to_seconds(time_obj):
    return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000

def add_subtitles_to_video(video_path, srt_path, output_path):
    # 加载视频
    video = VideoFileClip(video_path)
    
    # 读取SRT文件
    subtitles = pysrt.open(srt_path)
    
    # 创建字幕clips列表
    subtitle_clips = []
    
    for subtitle in subtitles:
        start_time = time_to_seconds(subtitle.start)
        end_time = time_to_seconds(subtitle.end)
        duration = end_time - start_time
        
        text_clip = (TextClip(subtitle.text, fontsize=34, color='white', font='SimHei', 
                              stroke_color='b.........完整代码请登录后点击上方下载按钮下载查看

网友评论0