当前位置: 首页 > news >正文

chattts生成的音频与字幕修改完善,每段字幕对应不同颜色的视频,准备下一步插入视频。

上一节中,实现了先生成一个固定背景的与音频长度一致的视频,然后插入字幕。再合并成一个视频的方法。

但是:这样有点单了,所以:

1.根据字幕的长度先生成视频片断

2.在片段上加上字幕。

3.合并所有片断,成为一个新的视频。

4.在新的视频上添加上音频。再次合成一个新的视频,即最后的视频。

可用代码1

from moviepy import VideoFileClip, AudioFileClip, TextClip, CompositeVideoClip, ImageClip
import cv2
import numpy as np
import random
import os
import warnings# 忽略特定的 UserWarning
# warnings.filterwarnings("ignore", category=UserWarning, message="In file .*\.mp4, .* bytes wanted but 0 bytes read at frame index .* \(out of a total .* frames\), at time .* sec. Using the last valid frame instead.")def parse_time(time_str):""" 解析 SRT 时间格式 """hours, minutes, seconds = time_str.split(':')seconds, milliseconds = seconds.split(',')return float(hours) * 3600 + float(minutes) * 60 + float(seconds) + float(milliseconds) / 1000def create_video(audio_path, subtitle_path, video_path, subtitle_position='center', use_temp_files=False):# 创建一个灰色背景的视频width, height = 1920, 1080  # 横屏视频分辨率fps = 24  # 视频帧率duration = AudioFileClip(audio_path).duration  # 视频时长与音频相同# 加载音频audio_clip = AudioFileClip(audio_path)# 读取字幕文件with open(subtitle_path, 'r', encoding='utf-8') as file:subtitles = file.readlines()# 处理字幕video_clips = []temp_files = []  # 用于存储临时文件路径for i in range(0, len(subtitles), 3):  # 4代表字幕文件中每一块所占行数index = subtitles[i].strip()time_range = subtitles[i + 1].strip().split(' --> ')start_time = time_range[0]end_time = time_range[1]text = subtitles[i + 2].strip()# 计算片段的持续时间start_seconds = parse_time(start_time)end_seconds = parse_time(end_time)clip_duration = end_seconds - start_seconds# 生成随机背景颜色random_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))frame = np.zeros((height, width, 3), dtype=np.uint8)frame[:, :] = random_color# 创建视频片段fourcc = cv2.VideoWriter_fourcc(*'

http://www.mrgr.cn/news/79988.html

相关文章:

  • ArcGIS MultiPatch数据转换Obj数据
  • 优秀前端文章笔记----持续更新(20241211-至今)
  • YashanDB 23.2 YAC 共享集群部署和使用自带YMP迁移工具进行数据迁移,效果很city
  • 麒麟系统+达梦数据库+MybatisPlus+Redis+SpringBoot
  • 使用Windbg排查C++软件安装包安装时被安全软件拦截导致安装堵塞(线程卡住)的问题
  • 360极速浏览器不支持看PDF
  • MinerU:PDF文档提取工具
  • 在CentOS中安装和卸载mysql
  • centos部署SkyWalking以及在springcloud项目中搭配loki使用
  • 【优选算法 模拟】模拟算法入门详解 : 模拟算法小专题
  • Gitlab ci/cd
  • Maven插件打包发布远程Docker镜像
  • strace,tcmalloc,asan使用
  • jmeter CLI Mode 传参实现动态设置用户数
  • 腾讯微信Android面试题及参考答案(多张原理图)
  • soul Java开发面试题及参考答案
  • 110.【C语言】数据结构之判断是否为完全二叉树
  • erlang 基于jose 实现 aes 加解密
  • 【C++】判断能否被 3, 5, 7 整除问题解析与优化
  • WIN11中安装Mamba常见问题解决方案