import os os_sep = os.sep this_file_abspath = os.path.abspath(__file__) this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[ -1] f_mp3 = ‘{}{}{}‘.format(this_file_dirname, os_sep, ‘auido.mp3‘) from playsound import playsound import time import math this_time = time.time() # playsound(f_mp3) # t_spend = time.time() - this_time t_spend = 58.777058839797974 # 音频的秒数 t_spend = math.ceil(t_spend) import cv2 import glob ‘‘‘ python+opencv视频图像相互转换 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693 链接:https://www.zhihu.com/question/49558804/answer/343058915 OpenCV: Drawing Functions in OpenCV https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html ‘‘‘ # 每秒传输帧数(Frames Per Second) fps = 100 # 保存视频的FPS,可以适当调整 FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。FPS是测量用于保存、显示动态视频的信息数量。每秒钟帧数愈多,所显示的动作就会愈流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。 fps = 15 fps = 5 fourcc = cv2.VideoWriter_fourcc(‘M‘, ‘J‘, ‘P‘, ‘G‘) # opencv3.0 f_v = ‘{}{}‘.format(int(time.time()), ‘saveVideo.avi‘) f_img_d = ‘{}{}{}{}{}‘.format(this_file_dirname, os_sep, ‘mypng‘, os_sep, ‘*.jpg‘) imgs = glob.glob(f_img_d) img = cv2.imread(imgs[0]) img_size = (img.shape[1], img.shape[0]) videoWriter = cv2.VideoWriter(f_v, fourcc, fps, img_size) """ 用图片总数均分音频时间 """ os_delay_factor = 0.14 os_delay_factor = 0.11 myinterval = t_spend / len(imgs) * os_delay_factor f, l = ‘mybaidu.parp.txt‘, [] with open(f, ‘r‘, encoding=‘utf-8‘) as fr: for i in fr: ii = i.replace(‘\n‘, ‘‘) l.append(ii) char_loop_l = [] for i in l: mystr, le = ‘‘, len(i) for ii in range(le): iii = i[ii] print(‘-----‘, iii) mystr = ‘{}{}‘.format(mystr, iii) print(mystr) char_loop_l.append(iii) # # from fontTools.ttLib import TTFont # myfont = TTFont(‘simhei.ttf‘) from PIL import Image, ImageDraw, ImageFont myfont = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") import cv2 import numpy as np os_delay_factor = 0.23 myinterval = t_spend / (len(char_loop_l) * 1) * os_delay_factor for i in l: i_index=l.index(i) img_index=i_index%len(imgs) imgname=imgs[img_index] mystr, le = ‘‘, len(i) for ii in range(le): iii = i[ii] print(‘-----‘, iii) if len(mystr) % 15 == 0: mystr = ‘{}{}‘.format(mystr, ‘\n‘) mystr = ‘{}{}‘.format(mystr, iii) print(mystr) this_time = time.time() while time.time() - this_time < myinterval: print(imgname) frame = cv2.imread(imgname) frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_pil = Image.fromarray(frame_cv2) # 转为PIL的图片格式 draw = ImageDraw.Draw(frame_pil) font = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") # 第一个参数为字体,中文黑体 # 第二个为字体大小 ImageDraw.Draw(frame_pil).text((100, 20), mystr, (0, 0, 255), font) ‘‘‘ frame_pil:目标图像 第一个参数为打印的坐标 第二个为打印的文本 第三个为字体颜色 第四个为字体 ‘‘‘ frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR) img = frame_cv2 videoWriter.write(img) videoWriter.release()
原理是将字幕写到图片中,然后再写入视频的帧中
原文地址:https://www.cnblogs.com/yuanjiangw/p/8759181.html
时间: 2024-10-16 09:01:18