减小文件大小 减少 帧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | # 设置分句的标志符号;可以根据实际需要进行修改 # cutlist = "。!?".decode('utf-8') cutlist = [ '\n' , '\t' , '。' , ';' , '?' , '.' , ';' , '?' , '...' , '、、、' , ':' ] cutlist = [ '\n' , '\t' , '。' , ';' , '?' , ':' ] # cutlist = [ '。', ';', '?', '.', ';', '?', '...', '、、、',':',':',','] # cutlist = [ '。', ';', '?', '.', ';', '?', '...', '、、、',':',',','、'] # 检查某字符是否分句标志符号的函数;如果是,返回True,否则返回False def FindToken(cutlist, char): if char in cutlist: return True else : return False # 进行分句的核心函数 def Cut(cutlist, lines): # 参数1:引用分句标志符;参数2:被分句的文本,为一行中文字符 l = [] # 句子列表,用于存储单个分句成功后的整句内容,为函数的返回值 line = [] # 临时列表,用于存储捕获到分句标志符之前的每个字符,一旦发现分句符号后,就会将其内容全部赋给l,然后就会被清空 for i in lines: # 对函数参数2中的每一字符逐个进行检查 (本函数中,如果将if和else对换一下位置,会更好懂) if FindToken(cutlist, i): # 如果当前字符是分句符号 line.append(i) # 将此字符放入临时列表中 l.append(''.join(line)) # 并把当前临时列表的内容加入到句子列表中 line = [] # 将符号列表清空,以便下次分句使用 else : # 如果当前字符不是分句符号,则将该字符直接放入临时列表中 line.append(i) return l r_s = [] # 以下为调用上述函数实现从文本文件中读取内容并进行分句。 # with open('mybaidu.parp.b.txt','r',encoding='utf-8') as fr : # for lines in fr: # l = Cut(list(cutlist), list(lines)) # for line in l: # if len(line.replace(' ', '')) == 0: # continue # if line.strip() != "": # line=line.strip() # r_s.append(line) # # # li = line.strip().split() # # for sentence in li: # # r_s.append(sentence) str_ = '' # cutlist = [ '。', ';', '?', '.', ';', '?', '...', '、、、',':',':',',','\n'] with open ( 'mybaidu.parp.b.txt' , 'r' , encoding = 'utf-8' ) as fr: for lines in fr: if len (lines.replace( ' ' , '')) = = 0 : continue # str_='{}{}'.format(str_,lines.replace('\n','')) # if len(lines.replace(' ','').replace('\n',''))==0: # continue str_ = '{}{}' . format (str_, lines) # l = Cut(list(cutlist), list(lines)) # for line in l: # if line.strip() != "": # line=line.strip() from aip import AipSpeech bd_k_l = [ '11059852' , '5Kk01GtG2fjCwpzEkwdn0mjw' , 'bp6Wyx377Elq7RsCQZzTBgGUFzLm8G2A' ] APP_ID, API_KEY, SECRET_KEY = bd_k_l mp3_dir = 'C:\\Users\\sas\\PycharmProjects\\produce_video\\result_g3com\\' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # result = client.synthesis(str_, 'zh', 1, { # 'vol': 5, # }) uid = 'liukeyuanG3_whole_para' # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 f_w = '{}{}{}{}{}' . format (mp3_dir, 'g3db' , uid, 'g3uid' , '.mp3' ) # # if not isinstance(result, dict): # # f_w = '{}{}{}{}'.format(mp3_dir, 'g3uid', uid, '.mp3') # f_w = '{}{}{}{}{}'.format(mp3_dir, 'g3db', uid, 'g3uid', '.mp3') # # ,'g3db',uid,'g3uid' # # with open('auido.b.mp3', 'wb') as f: # with open(f_w, 'wb') as f: # f.write(result) sentence_l, sentence_l_chk = Cut( list (cutlist), list (str_)), [] for i in sentence_l: chk_br = i.replace( '\n' , ' ' ) # del sentence_l[sentence_l.index(i)] if len (chk_br.replace( ' ' , '')) > 0 : sentence_l_chk.append(chk_br.replace( ' ' , '')) bdmp3filter_l = [ ':' , '——' , ',' , '《' , '》' , '“' , '”' , '、' , '(' , ')' , '.' , ' ' ] # 注意空格 bdmp3filter_l = [ ':' , '——' , ',' , '《' , '》' , '“' , '”' , '、' , '(' , ')' , '.' , ' ' , '·' , ' ' ] # 注意空格 # 保留其他标点符号 mp3_str = ' ' .join(sentence_l_chk) mp3_str_bdmp3filter = mp3_str for i in bdmp3filter_l: mp3_str_bdmp3filter = mp3_str_bdmp3filter.replace(i, '') import os, time, glob import cv2 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 ] logo_f_name, logo_f = 'g3logo.jpg' , '' f_img_d = '{}{}{}{}{}' . format (this_file_dirname, os_sep, 'mypng' , os_sep, '*.jpg' ) imgs, img_size_d = glob.glob(f_img_d), {} for i in imgs: if logo_f_name in i: logo_f = i del imgs[imgs.index(i)] for i in imgs: if logo_f_name in i: logo_f = i img = cv2.imread(i) w_h_s = '{},{}' . format (img.shape[ 1 ], img.shape[ 0 ]) if w_h_s not in img_size_d: img_size_d[w_h_s] = 1 else : img_size_d[w_h_s] + = 1 mode_img_size_wh = [ int (i) for i in sorted (img_size_d.items(), key = lambda mytuple: mytuple[ 1 ], reverse = True )[ 0 ][ 0 ].split( ',' )] mode_img_size_wh = [ 1208 , 720 ] mode_img_size_wh = [ 1280 , 720 ] 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 ] import time, math this_time = time.time() import imageio imageio.plugins.ffmpeg.download() from moviepy.editor import VideoFileClip f_mp3 = 'g3dbG3g3uidnoBRBlankLine.06.mp3' import mutagen.id3 from mutagen.easyid3 import EasyID3 from mutagen.mp3 import MP3 EasyID3.valid_keys[ "comment" ] = "COMM::'XXX'" id3info = MP3(f_mp3, ID3 = EasyID3) t_spend = id3info.info.length 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帧。 f_img_d = '{}{}{}{}{}' . format (this_file_dirname, os_sep, 'mypng' , os_sep, '*.jpg' ) imgs = glob.glob(f_img_d) """ 用图片总数均分音频时间 """ def resize_rescale_pilimg(img_f, w_h_tuple = (mode_img_size_wh[ 0 ], mode_img_size_wh[ 1 ]), mid_factor = 1 ): # print(img_f) img_n, img_type = img_f.split( '.' )[ - 2 ], img_f.split( '.' )[ - 1 ] # print(img_n) img_n_resize_rescale_pilimg_dir = '{}{}{}' . format (os_sep.join(img_n.split(os_sep)[: - 1 ]), 'resize_rescale_pilimg' , os_sep, img_n.split(os_sep)[ - 1 ], os_sep) img_n_resize_rescale_pilimg = '{}{}{}' . format (img_n_resize_rescale_pilimg_dir, img_n.split(os_sep)[ - 1 ], '.PNG' ) # print(img_n_resize_rescale_pilimg) img_type = 'PNG' # img_f_new = '{}{}{}{}'.format(img_n, int(time.time()), 'resize_rescale.', img_type) img_f_new = img_n_resize_rescale_pilimg mid_icon = Image. open (img_f) mid_icon_w, mid_icon_h = w_h_tuple[ 0 ] * mid_factor, w_h_tuple[ 1 ] * mid_factor mid_icon = mid_icon.resize((mid_icon_w, mid_icon_h), Image.ANTIALIAS) mid_icon.save(img_n_resize_rescale_pilimg, img_type) return img_f_new from PIL import Image, ImageDraw, ImageFont # myfont = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") myfont = ImageFont.truetype( "simhei.ttf" , encoding = "utf-8" ) import cv2 import numpy as np equal_str_l = [] le = len (mp3_str) br_step = 34 br_step = math.floor((mode_img_size_wh[ 0 ]) * 0.038 ) br_step = math.floor((mode_img_size_wh[ 0 ]) * 0.036 ) br_times = math.ceil(le / br_step) for i_br_loop in range (br_times): s_p = mp3_str[i_br_loop * br_step:i_br_loop * br_step + br_step] equal_str_l.append(s_p) l = equal_str_l char_loop_l_len = len (mp3_str) / len (equal_str_l) char_loop_l_len = len (mp3_str_bdmp3filter) import numpy as np font_size = math.floor((mode_img_size_wh[ 0 ]) * 0.020 ) f_x, f_y = math.floor((mode_img_size_wh[ 0 ]) * 0.06 ), math.floor(mode_img_size_wh[ 1 ] * 0.09 ) one_frame_line_num = math.floor((mode_img_size_wh[ 1 ] - 2 * f_y) / font_size * 0.01 ) one_frame_line_num = 12 one_frame_line_num = 23 multi_lines_l = [] for i in range ( len (equal_str_l)): if i < one_frame_line_num: ll = l[ 0 :i] else : ll = l[i - one_frame_line_num:i] multi_lines_l.append(ll) del multi_lines_l[ 0 ] multi_lines_l.append([i for i in equal_str_l[ - one_frame_line_num:]]) l = multi_lines_l def gen_video(os_delay_factor = 0.245 , mystep = 0.01 , bear_error_second = 1 , audio_spend = t_spend, step_para = 1 ): f_v = '{}{}{}{}{}{}{}' . format ( 'D:\\myv\\', ' g3db ', uid, ' g3uid ', uid, int(time.time()), ' NOimg.avi') fps, fourcc = 15 , cv2.VideoWriter_fourcc( 'M' , 'J' , 'P' , 'G' ) # fps, fourcc = 15, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') videoWriter = cv2.VideoWriter(f_v, fourcc, fps, (mode_img_size_wh[ 0 ], mode_img_size_wh[ 1 ])) img1 = np.zeros((mode_img_size_wh[ 1 ], mode_img_size_wh[ 0 ], 3 ), np.uint8) # fill the image with white img1.fill( 255 ) img2 = cv2.imread(logo_f) # logo rows, cols, channels = img2.shape roi = img1[ 0 :rows, 0 :cols] # 取img1的这个区域来处理 img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 建立logo的二值图,也建立相反的二值图 ret, mask = cv2.threshold(img2gray, 175 , 255 , cv2.THRESH_BINARY) # 二值化 mask_inv = cv2.bitwise_not(mask) # 做非操作,黑的变白,白的变黑,黑色0,白色255 img1_bg = cv2.bitwise_and(roi, roi, mask = mask) # 与操作 ,参数输入,输出,与mask做and操作,黑色的被填充 img2_fg = cv2.bitwise_and(img2, img2, mask = mask_inv) # 与操作 dst = cv2.add(img1_bg, img2_fg) # 相加 img1[ 0 :rows, 0 :cols] = dst # 把添加了logo的该区域赋值回原来的地方 for i in l: i_index = l.index(i) img_index = i_index % len (imgs) imgname = imgs[img_index] mystr = '\n' .join(i) # frame = cv2.imread(imgname) # if (frame.shape[1], frame.shape[0]) != (mode_img_size_wh[0], mode_img_size_wh[1]): # imgname = resize_rescale_pilimg(imgname) # frame = cv2.imread(imgname) # else: # pass # img1 = cv2.imread(imgname) # 加载图像 # img1 = np.zeros((mode_img_size_wh[1], mode_img_size_wh[0], 3), np.uint8) # # fill the image with white # img1.fill(255) # # img2 = cv2.imread(logo_f) # logo # rows, cols, channels = img2.shape # roi = img1[0:rows, 0:cols] # 取img1的这个区域来处理 # img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 建立logo的二值图,也建立相反的二值图 # ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY) # 二值化 # mask_inv = cv2.bitwise_not(mask) # 做非操作,黑的变白,白的变黑,黑色0,白色255 # img1_bg = cv2.bitwise_and(roi, roi, mask=mask) # 与操作 ,参数输入,输出,与mask做and操作,黑色的被填充 # img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv) # 与操作 # dst = cv2.add(img1_bg, img2_fg) # 相加 # img1[0:rows, 0:cols] = dst # 把添加了logo的该区域赋值回原来的地方 frame = img1 frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_pil = Image.fromarray(frame_cv2) # 转为PIL的图片格式 # font = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") # font_size = math.floor((mode_img_size_wh[0]) * 0.020) font = ImageFont.truetype( "simhei.ttf" , font_size, encoding = "utf-8" ) # ImageDraw.Draw(frame_pil).text((100, 20), mystr, (0, 0, 255), font) # f_x, f_y = math.floor((mode_img_size_wh[0]) * 0.06), math.floor(mode_img_size_wh[1] * 0.85) # f_x, f_y = math.floor((mode_img_size_wh[0]) * 0.06), math.floor(mode_img_size_wh[1] * 0.06) # ImageDraw.Draw(frame_pil).text((30, mode_img_size_wh[1]-30), mystr, (0, 0, 255), font) ImageDraw.Draw(frame_pil).text((f_x, f_y), mystr, ( 0 , 0 , 255 ), font) frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR) img = frame_cv2 # line_bdmp3filter = i line_bdmp3filter = equal_str_l[i_index] for bdmp3filter in bdmp3filter_l: line_bdmp3filter = line_bdmp3filter.replace(bdmp3filter, '') myinterval = t_spend / ( len (mp3_str_bdmp3filter) * 1 ) * os_delay_factor * len (line_bdmp3filter) # print(myinterval, '---------------', mystr) print (myinterval, '---------------' , line_bdmp3filter) this_time = time.time() while time.time() - this_time < myinterval: videoWriter.write(img) time.sleep( 0.05 ) videoWriter.release() time.sleep( 4 ) print (f_v) video_playtime = VideoFileClip(f_v).duration if video_playtime - audio_spend > bear_error_second: # os_delay_factor -= mystep os_delay_factor * = t_spend / video_playtime gen_video(os_delay_factor = os_delay_factor, mystep = 0.005 , audio_spend = t_spend) elif audio_spend - video_playtime > bear_error_second: # os_delay_factor += mystep os_delay_factor * = t_spend / video_playtime gen_video(os_delay_factor = os_delay_factor, mystep = 0.005 , audio_spend = t_spend) else : os._exit( 123 ) ''' 326 ''' gen_video(os_delay_factor = 0.0015 , mystep = 0.03 , bear_error_second = 0.5 , audio_spend = t_spend) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
2017-04-13 Moving average
2017-04-13 REQUEST
2017-04-13 Unix file types
2017-04-13 mysqld.sock
2017-04-13 Tunneling protocol
2017-04-13 多账户 真正意义上的多任务、多用户操作系统
2017-04-13 IP地址分类 CIDR 无类别域间路由