一句话一个画面 一个字符 一帧
一句话一个画面 一个字符 一帧
''' SELECT * FROM Info_Roles WHERE Flag=1 LIMIT 2; select top y * from 表 where 主键 not in(select top (x-1)*y 主键 from 表) 如果表中无主键,可以用临时表,加标识字段解决.这里的x,y可以用变量. select id=identity(int,1,1),* into #tb from 表 select * from #tb where id between (x-1)*y and x*y-1 select top 1000 Info_ID from Info_Roles select top 2000 Info_ID,',xiaole20180410SPLIT,',content from Info_Content where Info_ID not in( select top 1000 Info_ID from Info_Roles ) ; select top 399 Info_ID,',xiaole20180410SPLIT,',UPPER(content) from Info_Content ; select top 399 CHARINDEX('IMG',UPPER(content)) from Info_Content ; select top 15 Info_ID,',xiaole20180410SPLIT,',content from Info_Content where CHARINDEX('IMG',UPPER(content))>0; select top 15 Info_ID,',xiaole20180410SPLIT,',content from Info_Content where Info_ID in( select top 1000 Info_ID from Info_Roles ) and CHARINDEX('IMG',UPPER(content))>0 ; SELECT TOP 15 Info_ID, ',xiaole20180410SPLIT,', content FROM Info_Content WHERE Info_ID IN ( SELECT TOP 1000 Info_ID FROM Info_Roles WHERE Flag = 1 ) AND CHARINDEX('IMG', UPPER(content)) > 0; SELECT TOP 200 Info_ID, ',xiaole20180410SPLIT,', content FROM Info_Content WHERE Info_ID IN ( SELECT TOP 90000 Info_ID FROM Info_Roles ) AND CHARINDEX('<IMG', UPPER(content)) > 0; ''' from bs4 import BeautifulSoup from selenium import webdriver xlsplit_str = ',xiaole20180410SPLIT,' f_db_txt, uid_d = 'db.uid.para.txt', {} with open(f_db_txt, 'r', encoding='utf-8') as fr: for i in fr: i = i.replace('\t', '').replace('\n', '') if xlsplit_str in i: l = i.split(xlsplit_str) uid = l[0].replace(' ', '') uid_d[uid] = {} uid_d[uid]['html'] = [] uid_d[uid]['html'].append(l[1]) else: uid_d[uid]['html'].append(i) r_d = {} ''' 中文分句 ''' 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 ''' ''' def paragraph_to_sentence(paragraph, sentence_l): paragraph = paragraph.replace(' ', '') sentence_split_l = ['\n', '\t', '。', ';', '?', '.', ';', '?', '...', '、、、', ',', ','] for i in sentence_split_l: ll = paragraph.split(i) sentence_l.append(ll[0]) if len(ll) > 1: paragraph_to_sentence(ll[1], sentence_l) else: break return sentence_l def paragraph_to_sentence_no_recursion(paragraph, sentence_l): paragraph = paragraph.replace(' ', '') sentence_split_l = ['\n', '\t', '。', ';', '?', '.', ';', '?', '...', '、、、', ',', ','] for i in sentence_split_l: ll = paragraph.split(i) sentence_l.append(ll[0]) if len(ll) > 1: paragraph_to_sentence(ll[1], sentence_l) else: break return sentence_l paragraph = '' sentence_l = [] paragraph = paragraph.replace(' ', '') sentence_split_l = ['\n', '\t', '。', ';', '?', '.', ';', '?', '...', '、、、', ',', ','] for i in sentence_split_l: ll = paragraph.split(i) sentence_l.append(ll[0]) if len(ll) > 1: paragraph_to_sentence(ll[1], sentence_l) else: break def sentence_l_to_sentence_l_l(sentence_l): sentence_l_l = [] sentence_split_l = ['\n', '\t', '。', ';', '?', '.', ';', '?', '...', '、、、', ',', ','] for i in sentence_l: for ii in sentence_split_l: ll = i.split(ii) if len(ll) > 1: sentence_l_l += ll else: sentence_l_l.append(i) continue return sentence_l_l import requests, time, threading img_dir = 'C:\\Users\\sas\\PycharmProjects\\py_win_to_unix\\crontab_chk_url\\personas\\trunk\\plugins\\spider\\dl_img_tmp\\' img_dir = 'C:\\Users\\sas\\PycharmProjects\\produce_video\\mypng\\' # http://www.lky365.com/editor/uploadfile/20090508144220411.jpg # C:\Users\sas\PycharmProjects\produce_video\mypng def spider_webimg_dl_return_local_img_path(img_dir, img_url, uid, local_default='default.DONOT_REMOVE.png'): r = '%s%s' % (img_dir, local_default) try: bytes = requests.get(img_url)._content # r = '{}{}{}{}{}'.format(img_dir, time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())), 'g3dbuid', uid, '.png') r = '{}{}{}{}{}{}'.format(img_dir, 'g3db', uid, 'g3uid', img_url.split('.')[0].split('/')[-1], '.png') # if bytes != 0: if bytes != 0 and requests.get(img_url).status_code == 200: with open(r, 'wb')as f: f.write(bytes) else: print(img_url) except Exception as e: print(img_url, ',,,', uid) print(e) return r from aip import AipSpeech bd_k_l = ['11059852', '5Kk01GtG2fjCwpzEkwdn0mjw', 'bp6Wyx377Elq7RsCQZzTBgGUFzLm8G2A'] APP_ID, API_KEY, SECRET_KEY = bd_k_l f_p, str_ = 'mybaidu.parp.b.txt', '' with open(f_p, 'r', encoding='utf-8') as fr: for i in fr: ii = i.replace('\n', '') str_ = '{}{}'.format(str_, ii) def gen_bd_mp3(uid, str_): mp3_dir = 'C:\\Users\\sas\\PycharmProjects\\produce_video\\mymp3\\' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis(str_, 'zh', 1, { 'vol': 5, }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 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) import random for uid in uid_d: str_ = ''.join(uid_d[uid]['html']) # f_v = '{}{}{}{}{}{}{}'.format('D:\\myv\\', 'g3db', uid, 'g3uid', uid, int(time.time()), 'saveVideo.b.avi') fhtml = 'qqzong.vedio.allinone.tmp.html' fhtml = '{}{}{}{}'.format('D:\\myv\\myhtml\\', int(time.time()),random.randint(1234,6789),fhtml) with open(fhtml, 'w', encoding='utf-8') as fw: fw.write(str_) with open(fhtml, 'r', encoding='utf-8') as fo: soup = BeautifulSoup(fo, 'html.parser') img_l = soup.find_all('img') if len(img_l) > 0: l = soup.find_all('img') uid_d[uid]['img'], uid_d[uid]['txt'] = [i.attrs['src'] for i in l], soup.text r_d[uid] = {} r_d[uid] = uid_d[uid] # incr_l = ['http://www.51g3.net/templates/images/logo.jpg', # 'http://www.51g3.net/attached/image/20171206104541_20247.jpg', # 'http://www.51g3.net/attached/image/20171129183441_78749.png', # 'http://www.51g3.net/templates/images/agentimg.jpg'] incr_l = [] r_d[uid]['img'] += incr_l # r_d[uid]['sentence_l']=paragraph_to_sentence(uid_d[uid]['txt'],[]) sentence_l = paragraph_to_sentence(uid_d[uid]['txt'], []) try: str_ = uid_d[uid]['txt'] # gen_bd_mp3(uid, str_) except Exception as e: print(e) for img_url in r_d[uid]['img']: # spider_webimg_dl_return_local_img_path(img_dir, img_url, uid, local_default='default.DONOT_REMOVE.png') pass # r_d[uid]['sentence_l'] = sentence_l_to_sentence_l_l(sentence_l) r_d[uid]['sentence_l'] = Cut(list(cutlist), list(uid_d[uid]['txt'])) ''' 加限制 ''' if len(r_d[uid]['sentence_l']) < 5: del r_d[uid] else: # print(uid) pass uid_l = [i for i in r_d] import os 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] f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.png') f_mp3_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mymp3', os_sep, '*.mp3') imgs, img_size_d = glob.glob(f_img_d), {} mp3s, mp3_size_d = glob.glob(f_mp3_d), {} for uid in r_d: chk_str = '{}{}{}'.format('g3db', uid, 'g3uid') r_d[uid]['img_n'], r_d[uid]['img_path'] = 0, [] for img in imgs: if chk_str in img or 'nfwtsite00' in img: r_d[uid]['img_n'] += 1 r_d[uid]['img_path'].append(img) else: pass for mp3 in mp3s: if chk_str in mp3: r_d[uid]['mp3_path'] = mp3 else: pass print('-----------------') uid_r_l = [] for uid in r_d: if int(r_d[uid]['img_n']) >= 1: # print(uid) uid_r_l.append(uid) uid = uid_r_l[4] chk_str = '{}{}{}'.format('g3db', uid, 'g3uid') import os 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] f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.png') imgs, img_size_d = glob.glob(f_img_d), {} imgs = r_d[uid]['img_path'] for i in imgs: try: img = cv2.imread(i) except Exception as e: print(1) os._exit(3) if img is None: print(uid) os._exit(332) else: pass 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(',')] 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') f_mp3 = r_d[uid]['mp3_path'] from playsound import playsound import time import math this_time = time.time() playsound(f_mp3) t_spend = time.time() - this_time print(t_spend) # t_spend = t_spend # 音频的秒数 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帧。 # # f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg') # imgs = glob.glob(f_img_d) # # """ # 用图片总数均分音频时间 # """ # os_delay_factor = 0.14 # os_delay_factor = 0.11 # if len(imgs)==0: # os._exit(387) # print(uid) # myinterval = t_spend / len(imgs) * os_delay_factor # f, l = 'mybaidu.parp.b.txt', [] # with open(f, 'r', encoding='utf-8') as fr: # for i in fr: # ii = i.replace('\n', '') # l.append(ii) l = r_d[uid]['sentence_l'] 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) char_loop_l = r_d[uid]['sentence_l'] # # from fontTools.ttLib import TTFont # myfont = TTFont('simhei.ttf') 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 def compute_video_playtime(f): # Create a VideoCapture object and read from input file # If the input is the camera, pass 0 instead of the video file name cap = cv2.VideoCapture(f) # Check if camera opened successfully if (cap.isOpened() == False): print("Error opening video stream or file") # Read until video is completed while (cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() if ret == True: # Display the resulting frame cv2.imshow('Frame', frame) # Press Q on keyboard to exit if cv2.waitKey(25) & 0xFF == ord('q'): break # Break the loop else: break # When everything done, release the video capture object cap.release() # Closes all the frames cv2.destroyAllWindows() return time.time() - this_time from PIL import Image, ImageDraw, ImageFont myfont = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") import cv2 import numpy as np char_loop_l = r_d[uid]['sentence_l'] imgs = r_d[uid]['img_path'] len_char_loop_l = len(char_loop_l) l=char_loop_l def gen_video(os_delay_factor=0.245, mystep=0.01, bear_error_second=1, audio_spend=t_spend): print(562, uid) # print(r_d) f_v = '{}{}'.format(int(time.time()), 'saveVideo.b.avi') f_v = '{}{}{}{}{}{}{}'.format('D:\\myv\\', 'g3db', uid, 'g3uid', uid, int(time.time()), 'saveVideo.b.avi') fps, fourcc = 15, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # opencv3.0 videoWriter = cv2.VideoWriter(f_v, fourcc, fps, (mode_img_size_wh[0], mode_img_size_wh[1])) # f_v = '{}{}'.format(int(time.time()), 'saveVideo.b.avi') # myinterval = t_spend / (len(char_loop_l) * 1) * os_delay_factor 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) del_f = False 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) del_f = True else: pass frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_pil = Image.fromarray(frame_cv2) # 转为PIL的图片格式 font = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") # 第一个参数为字体,中文黑体 # 第二个为字体大小 ImageDraw.Draw(frame_pil).text((100, 20), mystr, (0, 0, 255), font) frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR) img = frame_cv2 videoWriter.write(img) videoWriter.release() # time.sleep(3) video_playtime = compute_video_playtime(f_v) if video_playtime - audio_spend > bear_error_second: os_delay_factor -= mystep gen_video(os_delay_factor=os_delay_factor, mystep=0.05, audio_spend=t_spend) elif audio_spend - video_playtime > bear_error_second: os_delay_factor += mystep gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=t_spend) else: os._exit(123) gen_video(os_delay_factor=1, mystep=0.05, bear_error_second=0.5, audio_spend=t_spend)