物流网站如何设计,惠安规划局建设局网站,山东省住房和城乡建设厅官网查询,企业网站设计价格上一节中#xff0c;实现了先生成一个固定背景的与音频长度一致的视频#xff0c;然后插入字幕。再合并成一个视频的方法。
但是#xff1a;这样有点单了#xff0c;所以#xff1a;
1.根据字幕的长度先生成视频片断
2.在片段上加上字幕。
3.合并所有片断#xff0c;…上一节中实现了先生成一个固定背景的与音频长度一致的视频然后插入字幕。再合并成一个视频的方法。
但是这样有点单了所以
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, categoryUserWarning, messageIn 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_positioncenter, use_temp_filesFalse):# 创建一个灰色背景的视频width, height 1920, 1080 # 横屏视频分辨率fps 24 # 视频帧率duration AudioFileClip(audio_path).duration # 视频时长与音频相同# 加载音频audio_clip AudioFileClip(audio_path)# 读取字幕文件with open(subtitle_path, r, encodingutf-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), dtypenp.uint8)frame[:, :] random_color# 创建视频片段fourcc cv2.VideoWriter_fourcc(*mp4v)if use_temp_files:output_path ftemp_clip_{i}.mp4out cv2.VideoWriter(output_path, fourcc, fps, (width, height), isColorTrue)for _ in range(int(fps * clip_duration)):out.write(frame)out.release()temp_files.append(output_path)# 检查临时视频文件是否存在且大小大于0if not os.path.exists(output_path) or os.path.getsize(output_path) 0:raise Exception(fTemporary video file {output_path} is missing or empty.)# 加载视频片段video_clip VideoFileClip(output_path).with_duration(clip_duration)else:# 直接在内存中创建 VideoClipvideo_clip ImageClip(frame).with_duration(clip_duration)# 折行处理font_size 50font cv2.FONT_HERSHEY_SIMPLEXmax_width width * 0.8 # 最大宽度为视频宽度的80%words text.split()lines []line for word in words:test_line line word(test_width, _), _ cv2.getTextSize(test_line, font, 1, font_size)if test_width max_width:line test_lineelse:lines.append(line)line wordlines.append(line)# 创建TextClipfinal_text \n.join(lines)subtitle_clip TextClip(textfinal_text, font_sizefont_size, colorwhite,font/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc)subtitle_clip subtitle_clip.with_start(0).with_end(clip_duration).with_position((center, subtitle_position))# 创建灰色背景框text_width, text_height subtitle_clip.sizepadding 10 # 字幕框的内边距box_width text_width 2 * paddingbox_height text_height 2 * paddingbox_frame np.zeros((box_height, box_width, 3), dtypenp.uint8) 128 # 灰色背景box_clip ImageClip(box_frame).with_start(0).with_end(clip_duration)# 设置背景框的位置if subtitle_position center:box_position (center, center)elif subtitle_position bottom:box_position (center, bottom)else:box_position (center, top)box_clip box_clip.with_position(box_position, relativeTrue).with_duration(clip_duration)# 合成片段final_clip CompositeVideoClip([video_clip, box_clip, subtitle_clip]).with_start(start_seconds)video_clips.append(final_clip)# 合成最终视频final_video CompositeVideoClip(video_clips)final_video final_video.with_audio(audio_clip)final_video.write_videofile(video_path, codeclibx264, fpsfps)# 清理临时文件if use_temp_files:for temp_file in temp_files:os.remove(temp_file)# 示例调用
audio_path wwww.wav
subtitle_path wwww.srt
video_path wwww.mp4
subtitle_position bottom # 可选值: center, bottom
use_temp_files False # 设置为 True 以使用临时文件# 创建最终视频
create_video(audio_path, subtitle_path, video_path, subtitle_position, use_temp_files)可用参考代码2
from moviepy import VideoFileClip, AudioFileClip, TextClip, CompositeVideoClip, ImageClip
import cv2
import numpy as np
import random
import os
import warnings# 忽略特定的 UserWarning
# warnings.filterwarnings(ignore, categoryUserWarning, messageIn 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_positioncenter, use_temp_filesFalse):# 创建一个灰色背景的视频width, height 1920, 1080 # 横屏视频分辨率fps 24 # 视频帧率duration AudioFileClip(audio_path).duration # 视频时长与音频相同# 加载音频audio_clip AudioFileClip(audio_path)# 读取字幕文件with open(subtitle_path, r, encodingutf-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), dtypenp.uint8)frame[:, :] random_color# 创建视频片段fourcc cv2.VideoWriter_fourcc(*mp4v)if use_temp_files:output_path ftemp_clip_{i}.mp4out cv2.VideoWriter(output_path, fourcc, fps, (width, height), isColorTrue)for _ in range(int(fps * clip_duration)):out.write(frame)out.release()temp_files.append(output_path)# 检查临时视频文件是否存在且大小大于0if not os.path.exists(output_path) or os.path.getsize(output_path) 0:raise Exception(fTemporary video file {output_path} is missing or empty.)# 加载视频片段video_clip VideoFileClip(output_path).with_duration(clip_duration)else:# 直接在内存中创建 VideoClipvideo_clip ImageClip(frame).with_duration(clip_duration)# 折行处理font_size 50font cv2.FONT_HERSHEY_SIMPLEXmax_width width * 0.8 # 最大宽度为视频宽度的80%words text.split()lines []line for word in words:test_line line word(test_width, _), _ cv2.getTextSize(test_line, font, 1, font_size)if test_width max_width:line test_lineelse:lines.append(line)line wordlines.append(line)# 创建TextClipfinal_text \n.join(lines)subtitle_clip TextClip(textfinal_text, font_sizefont_size, colorwhite,font/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc)subtitle_clip subtitle_clip.with_start(0).with_end(clip_duration).with_position((center, subtitle_position))# 创建灰色背景框text_width, text_height subtitle_clip.sizepadding 10 # 字幕框的内边距box_width text_width 2 * paddingbox_height text_height 2 * paddingbox_frame np.zeros((box_height, box_width, 3), dtypenp.uint8) 128 # 灰色背景box_clip ImageClip(box_frame).with_start(0).with_end(clip_duration)# 设置背景框的位置if subtitle_position center:box_position (center, center)elif subtitle_position bottom:box_position (center, bottom)else:box_position (center, top)box_clip box_clip.with_position(box_position, relativeTrue).with_duration(clip_duration)# 合成片段final_clip CompositeVideoClip([video_clip, box_clip, subtitle_clip]).with_start(start_seconds)video_clips.append(final_clip)# 合成最终视频final_video CompositeVideoClip(video_clips)final_video final_video.with_audio(audio_clip)final_video.write_videofile(video_path, codeclibx264, fpsfps)# 清理临时文件if use_temp_files:for temp_file in temp_files:os.remove(temp_file)# 示例调用
audio_path wwww.wav
subtitle_path wwww.srt
video_path wwww.mp4
subtitle_position bottom # 可选值: center, bottom
use_temp_files False # 设置为 True 以使用临时文件# 创建最终视频
create_video(audio_path, subtitle_path, video_path, subtitle_position, use_temp_files)以上代码参考下一步。我计划。使用视频及字幕建立数据库然后使用字幕进行匹配替换目前的随机背景色的视频片断。那位朋友有好的参考意见的。交流一下。 文章转载自: http://www.morning.dljujia.com.gov.cn.dljujia.com http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.jbysr.cn.gov.cn.jbysr.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.sskkf.cn.gov.cn.sskkf.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.pljxz.cn.gov.cn.pljxz.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.rwzmz.cn.gov.cn.rwzmz.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.pwzzk.cn.gov.cn.pwzzk.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.sooong.com.gov.cn.sooong.com http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.mprky.cn.gov.cn.mprky.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.cjsnj.cn.gov.cn.cjsnj.cn