• 专家知识


    from aip import AipSpeech
    
    bd_k_l = ['11059852', '5Kk01GtG2fjCwpzEkwdn0mjw', 'bp6Wyx377Elq7RsCQZzTBgGUFzLm8G2A']
    APP_ID, API_KEY, SECRET_KEY = bd_k_l
    
    
    def gen_bd_mp3(uid, str_):
        mp3_dir = 'C:\Users\sas\PycharmProjects\produce_video\mymp3\'
        mp3_dir = 'D:\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, 'semHAND', uid, '.mp3')
            #  with open('auido.b.mp3', 'wb') as f:
            with open(f_w, 'wb') as f:
                f.write(result)
    
    
    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]
    
    uid_d = []
    f, uid_n = 'html.parp.txt', 0
    sen_l = ['。', '
    ']
    
    whole_s = ''
    with open(f, 'r', encoding='utf-8') as fr:
        for i in fr:
            whole_s = '{}{}'.format(whole_s, i)
    
    # 设置分句的标志符号;可以根据实际需要进行修改
    # cutlist = "。!?".decode('utf-8')
    
    cutlist = ['
    ', '	', '。', ';', '?', '.', ';', '?', '...', '、、、', ':', '!', '!']
    cutlist = ['
    ', '	', '。', ';', '?', '.', '?', '...', '、、、', '!', '!']
    
    
    # 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
    
    
    sentence_l = Cut(list(cutlist), list(whole_s))
    sentence_l_noblank = []
    for i in sentence_l:
        if i != '
    ':
            sentence_l_noblank.append(i.replace('
    ', ''))
    
    '''
    在标点符号正确的情况下分割出自然的句子
    '''
    
    '''
    以句子为单位生成声音
    '''
    
    from aip import AipSpeech
    
    bd_k_l = ['11059852', '5Kk01GtG2fjCwpzEkwdn0mjw', 'bp6Wyx377Elq7RsCQZzTBgGUFzLm8G2A']
    APP_ID, API_KEY, SECRET_KEY = bd_k_l
    
    
    def gen_bd_mp3(uid, str_):
        mp3_dir = 'C:\Users\sas\PycharmProjects\produce_video\mymp3\'
        mp3_dir = 'D:\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, 'oneSen', uid, '.mp3')
            #  with open('auido.b.mp3', 'wb') as f:
            with open(f_w, 'wb') as f:
                f.write(result)
    
    
    uid_d = {}
    uid, uid_n = 43020157, 0
    uid_d[uid] = {}
    uid_d[uid]['sen_d'], uid_d[uid]['img_l'] = {}, []
    for i in sentence_l_noblank:
        uid_sen = '{}{}{}'.format(uid, '_', uid_n)
        # gen_bd_mp3(uid_sen, i)
        uid_n += 1
        mp3_dir = 'D:\mymp3\'
        f_w = '{}{}{}{}'.format(mp3_dir, 'oneSen', uid_sen, '.mp3')
        uid_d[uid]['sen_d'][f_w] = i
    
    import glob
    
    f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg')
    imgs = glob.glob(f_img_d)
    uid_d[uid]['img_l'] = []
    for i in imgs:
        if 'logo' in i:
            if 'uid' in i:
                #      print(i)
                uid_d[uid]['img_logo_uid'] = i
            else:
                uid_d[uid]['img_logo_our'] = i
        else:
            uid_d[uid]['img_l'].append(i)
    
    for i in uid_d[uid]:
        print(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, logo_f_uid, imgs = uid_d[uid]['img_logo_our'], uid_d[uid]['img_logo_uid'], uid_d[uid]['img_l']
    img_size_d = {}
    w_h_size_min = [12345, 12345]
    for i in imgs:
        img = cv2.imread(i)
        w_h_s = '{},{}'.format(img.shape[1], img.shape[0])
        w, h = [int(i) for i in w_h_s.split(',')]
        if w * h < w_h_size_min[0] * w_h_size_min[1]:
            w_h_size_min = [w, h]
        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 = w_h_size_min
    os_sep = os.sep
    
    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
    
    
    def resize_rescale_pilimg(img_f, w_h_tuple=(mode_img_size_wh[0], mode_img_size_wh[1]), mid_factor=1):
        img_n, img_type = img_f.split('.')[-2], img_f.split('.')[-1]
        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')
        img_type = 'PNG'
        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", encoding="utf-8")
    import cv2
    import numpy as np
    import math
    
    br_step = math.floor((mode_img_size_wh[0]) * 0.0185)
    
    audio_spend = 0
    f_v = '{}{}{}{}'.format('D:\myv\', uid, int(time.time()), '.avi')
    fps, fourcc = 20, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
    videoWriter = cv2.VideoWriter(f_v, fourcc, fps, (mode_img_size_wh[0], mode_img_size_wh[1]))
    img_seq = 0
    for f_mp3 in uid_d[uid]['sen_d']:
        screen_str = uid_d[uid]['sen_d'][f_mp3]
        screen_str_l = []
        br_step_times = math.ceil(len(screen_str) / br_step)
    
        for i in range(br_step_times):
            myrow = screen_str[i * br_step:(i + 1) * br_step]
            screen_str_l.append(myrow)
        screen_str = '
    '.join(screen_str_l)
    
        imgname = uid_d[uid]['img_l'][img_seq % len(uid_d[uid]['img_l'])]
        img_seq += 1
        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)  # 加载图像
        #   img2 = cv2.imread(logo_f)  # logo
        img2 = cv2.imread(logo_f_uid)  # 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的该区域赋值回原来的地方
    
        # img3 = cv2.imread(logo_f_uid)  # logo
        img3 = cv2.imread(logo_f)  # logo our在右边
        rows, cols, channels = img3.shape
        rows1, cols1, channels1 = img1.shape
        roi = img1[0:rows, cols1 - cols:cols1]  # 取img1的这个区域来处理
        img3gray = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)  # 建立logo的二值图,也建立相反的二值图
        ret, mask = cv2.threshold(img3gray, 175, 255, cv2.THRESH_BINARY)  # 二值化
        mask_inv = cv2.bitwise_not(mask)  # 做非操作,黑的变白,白的变黑,黑色0,白色255
        img1_bg = cv2.bitwise_and(roi, roi, mask=mask)  # 与操作 ,参数输入,输出,与mask做and操作,黑色的被填充
        img3_fg = cv2.bitwise_and(img3, img3, mask=mask_inv)  # 与操作
        dst = cv2.add(img1_bg, img3_fg)  # 相加
        img1[0:rows, cols1 - cols:cols1] = dst  # 把添加了logo的该区域赋值回原来的地方
    
        frame = img1
        frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_pil = Image.fromarray(frame_cv2)  # 转为PIL的图片格式
        font_size = math.floor((mode_img_size_wh[0]) * 0.040)
        font = ImageFont.truetype("simhei.ttf", font_size, encoding="utf-8")
        f_x, f_y = math.floor((mode_img_size_wh[0]) * 0.06), math.floor(
            mode_img_size_wh[1] * 0.85) - br_step_times * font_size
        ImageDraw.Draw(frame_pil).text((f_x, f_y), screen_str, (255, 0, 0), font)
        frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
        img = frame_cv2
    
        EasyID3.valid_keys["comment"] = "COMM::'XXX'"
        id3info = MP3(f_mp3, ID3=EasyID3)
        t_spend = id3info.info.length
    
        audio_spend += t_spend
        print(audio_spend)
        myinterval = t_spend
        print(myinterval, '---------------', screen_str)
        # this_time = time.time()
        fps_loop_times = math.ceil(t_spend * fps)
        for fps_loop in range(fps_loop_times):
            videoWriter.write(img)
            #
            # while time.time() - this_time < myinterval:
            #     videoWriter.write(img)
            #     wt = math.floor(1 / fps * 1000)
            #     cv2.waitKey(wt)
    
    videoWriter.release()
    time.sleep(1)
    

      

    # pass_dbid_l = [i.split(':45 ')[-1].split('_')[0] for i in s.split('
    ')[1:-2]]
    pass_dbid_l = []
    d = 0
    # import os
    # os_sep = os.sep
    # banner_img_dir = '{}{}{}'.format('D:\mybanner', os_sep, '*.*')
    # import glob
    # banner_imgs = glob.glob(banner_img_dir)
    # un_d={}
    # l=[]
    # for banner_img in banner_imgs:
    #     un=banner_img.split(os_sep)[-1].split('_')[0]
    #     if un not in un_d:
    #         un_d[un]={}
    #         un_d[un]['banner_img_l']=[]
    #     un_d[un]['banner_img_l'].append(banner_img)
    #
    # for un in un_d:
    #     if len( un_d[un]['banner_img_l'])>2:
    #         l.append(un)
    # s='"{}"'.format('","'.join(l))
    # s='{}'.format(','.join(l))
    # print(s)
    #
    # import os
    # os._exit(343)
    
    f = 'uid.username.txt'
    uid_d = {}
    with open(f, 'r', encoding='utf-8') as fr:
        for i in fr:
            uid, un = i.replace('
    ', '').split('	')
            uid_d[uid] = {}
            uid_d[uid]['username'] = un
            uid_d[uid]['banner_img_l'] = []
    import os
    
    os_sep = os.sep
    banner_img_dir = '{}{}{}'.format('D:\mybanner', os_sep, '*.*')
    import glob
    
    banner_imgs = glob.glob(banner_img_dir)
    
    for uid in uid_d:
        un = uid_d[uid]['username']
        for i in banner_imgs:
            if un in i:
                uid_d[uid]['banner_img_l'].append(i)
    
    uid_d_filter = {}
    l = []
    for uid in uid_d:
        if len(uid_d[uid]['banner_img_l']) > 2:
            uid_d_filter[uid] = {}
            uid_d_filter[uid] = uid_d[uid]
            l.append(uid)
    
    # print(','.join(l))
    
    import os
    
    # os._exit(4535)
    del uid_d
    
    dd = 9
    
    '''
    CHK-BANNER
    '''
    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]
    # 设置分句的标志符号;可以根据实际需要进行修改
    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
    
    
    '''
    在标点符号正确的情况下分割出自然的句子
    以句子为单位生成声音
    '''
    from aip import AipSpeech
    
    bd_k_l = ['11131400', 'fGtMoYr1vVUbdvSGPTtknunt', 'x5IGwqiSu6C6EQQ6IhFAN6OZpGsOMK4W']
    # bd_k_l = ['11059852', '5Kk01GtG2fjCwpzEkwdn0mjw', 'bp6Wyx377Elq7RsCQZzTBgGUFzLm8G2A']
    APP_ID, API_KEY, SECRET_KEY = bd_k_l
    
    
    def gen_bd_mp3(uid, str_, f_w):
        mp3_dir = 'C:\Users\sas\PycharmProjects\produce_video\mymp3\'
        mp3_dir = 'D:\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, 'oneSen0419', uid, '.mp3')
            #  with open('auido.b.mp3', 'wb') as f:
            with open(f_w, 'wb') as f:
                f.write(result)
            print('ok', f_w)
    
    
    img_dir = 'D:\mybanner\'
    img_dir = 'D:\mybanner\'
    from bs4 import *
    
    xls = 'XLS0419'
    f = 'db.data.snap.txt'
    uid_d = {}
    '''
    uid_d[uid]['sen_d'], uid_d[uid]['img_l'] = {}, []
    uid_d[uid]['img_logo_uid'] = i
    uid_d[uid]['img_logo_our'] = i
    
    '''
    import glob
    
    f_img_d = '{}{}{}'.format(img_dir, os_sep, '*.jpg')
    imgs = glob.glob(f_img_d)
    img_dir_logo = 'D:\mylogo\'
    f_img_d = '{}{}{}'.format(img_dir_logo, os_sep, '*.jpg')
    imgs_logo = glob.glob(f_img_d)
    our_logo_f = 'g3logo.jpg'
    mp3_dir = 'D:\mymp3\'
    f_mp3_d = '{}{}{}'.format(mp3_dir, os_sep, '*.mp3')
    mpp3s = glob.glob(f_mp3_d)
    import random
    import time
    
    with open(f, 'r', encoding='utf-8') as fr:
        for i in fr:
            ll = i.split(xls)
            dbid, uid, username, html_ = [i.replace('	', '') for i in ll]
    
            print('---------------')
            # if uid not in uid_d_filter:
            #     print(uid)
            #     continue
    
            uid_d[uid] = {}
            uid_d[uid]['dbid'], uid_d[uid]['username'], uid_d[uid]['str'] = dbid, username, html_
            uid_d[uid]['sen_d'], uid_d[uid]['img_l'] = {}, []
            uid_d[uid]['img_logo_our'] = '{}{}'.format(img_dir_logo, our_logo_f)
            myhtml = 'D:\myhtml\{}tmp.html'.format(random.randint(123, 999))
            with open(myhtml, 'w', encoding='utf-8') as fw:
                fw.write(html_)
            with open(myhtml, 'r', encoding='utf-8') as myhtml_o:
                bs = BeautifulSoup(myhtml_o, 'html.parser')
                txt = bs.text
    
                for i_img in imgs_logo:
                    if uid in i_img:
                        uid_d[uid]['img_logo_uid'] = i_img
                        break
                        # logo_url = 'http://img.a.g3user.com/site/34034975/logo.jpg'.replace('34034975', uid)
                        #   spider_webimg_dl_return_local_img_path(img_dir, logo_url, uid, 'logo')
    
                        # img_url = [i.attrs['src'] for i in bs.find_all('img')]
                        # uid_n = 0
                        # for i in bs.find_all('img'):
                        #     img_url = i.attrs['src']
                        #     # spider_webimg_dl_return_local_img_path(img_dir, img_url, uid, uid_n)
                        #     uid_n += 1
                        #
                        #     for i_img in imgs:
                        #         if uid in i_img:
                        #             uid_d[uid]['img_l'].append(i_img)
            uid_d[uid]['img_l'] = uid_d_filter[uid]['banner_img_l']
            sentence_l = Cut(list(cutlist), list(txt))
            sentence_l_noblank = []
            for i in sentence_l:
                if i != '
    ' and len(i.replace(' ', '')) > 0:
                    sentence_l_noblank.append(i.replace('
    ', ''))
            uid_n = 0
            for screen_str in sentence_l_noblank:
                uid_sen = '{}{}{}'.format(uid, '_', uid_n)
                f_w = '{}{}{}'.format(mp3_dir, uid_sen, '.mp3')
                # gen_bd_mp3(uid_sen, screen_str, f_w)
                #   time.sleep(1)
                uid_d[uid]['sen_d'][f_w] = screen_str
                uid_n += 1
                if uid_n % 5 == 0:
                    #  time.sleep(3)
                    pass
                    # time.sleep(1)
    
    import os, time, glob, math
    import imageio
    
    imageio.plugins.ffmpeg.download()
    from mutagen.easyid3 import EasyID3
    from mutagen.mp3 import MP3
    from PIL import Image, ImageDraw, ImageFont
    import cv2
    import numpy as np
    
    
    def resize_rescale_pilimg(img_f, w_h_tuple, mid_factor=1):
        img_n, img_type = img_f.split('.')[-2], img_f.split('.')[-1]
        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')
        img_type = 'PNG'
        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
    
    
    for uid in uid_d:
    
        if uid_d[uid]['dbid'] in pass_dbid_l:
            continue
    
        if int(uid) % 2 == 0:
            continue
        print(uid)
        if 'img_logo_uid' not in uid_d[uid]:
            continue
        logo_f, logo_f_uid, imgs = uid_d[uid]['img_logo_our'], uid_d[uid]['img_logo_uid'], uid_d[uid]['img_l']
    
        res_v_f_dir = 'D:\myvonline'
        f_img_d = '{}{}{}'.format(res_v_f_dir, os_sep, '*.mp4')
        vs = glob.glob(f_img_d)
        chk_v = '{}{}{}{}'.format(res_v_f_dir, os_sep, uid_d[uid]['dbid'], '.mp4')
        t_spend_per_imgperiod = 5
        res_v = '{}{}{}{}{}{}'.format(res_v_f_dir, os_sep, uid_d[uid]['dbid'], '_', t_spend_per_imgperiod, '.mp4')
    
        # chk_v = res_v
        # print(chk_v)
        # if chk_v in vs:
        #     continue
    
        img_size_d = {}
        w_h_size_min = [12345, 12345]
        for i in imgs:
            img = cv2.imread(i)
            w_h_s = '{},{}'.format(img.shape[1], img.shape[0])
            w, h = [int(i) for i in w_h_s.split(',')]
            if w * h < w_h_size_min[0] * w_h_size_min[1]:
                w_h_size_min = [w, h]
            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(',')]
        # if mode_img_size_wh[0] * mode_img_size_wh[1] > w_h_size_min[0] * w_h_size_min[1]:
        #     mode_img_size_wh = w_h_size_min
    
        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]
    
        br_step_test_l = [0.0185, 0.0195, 0.02, 0.028]
        br_step = math.floor((mode_img_size_wh[0]) * br_step_test_l[-1])
        audio_spend = 0
        for f_mp3 in uid_d[uid]['sen_d']:
            screen_str = uid_d[uid]['sen_d'][f_mp3]
            # 没有保存的必要
            uid_sen = f_mp3.split(os_sep)[-1].split('.')[0]
            had = False
            mp3_dir = 'D:\mymp3\'
            f_mp3_d = '{}{}{}'.format(mp3_dir, os_sep, '*.mp3')
            mpp3s = glob.glob(f_mp3_d)
            print(f_mp3)
            if f_mp3 not in mpp3s:
                if len(screen_str.replace(' ', '')) == 0:
                    screen_str = '{}{}'.format(screen_str, '谢谢。')
                print(screen_str)
                gen_bd_mp3(uid_sen, screen_str, f_mp3)
                time.sleep(2)
            EasyID3.valid_keys["comment"] = "COMM::'XXX'"
            try:
                id3info = MP3(f_mp3, ID3=EasyID3)
            except Exception as e:
                print(e)
                continue
            audio_spend += id3info.info.length
    
        f_v = '{}{}{}{}'.format('D:\myv\', uid, int(time.time()), '.avi')
        fps, fourcc = 20, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
        videoWriter = cv2.VideoWriter(f_v, fourcc, fps, (mode_img_size_wh[0], mode_img_size_wh[1]))
        img_l_len = len(uid_d[uid]['img_l'])
        t_spend_per_imgperiod = 5
        t_spend_per_imgperiod_mod = audio_spend % t_spend_per_imgperiod
        imgperiod_times = math.floor(audio_spend / t_spend_per_imgperiod)
        per_imgperiod_times = t_spend_per_imgperiod * fps
        per_imgperiod_mod_times = math.ceil((audio_spend - imgperiod_times * t_spend_per_imgperiod) * fps)
        imgname = ''
        for myloop in range(imgperiod_times):
            imgname_ori = uid_d[uid]['img_l'][myloop % img_l_len]
            imgname = uid_d[uid]['img_l'][myloop % img_l_len]
            print(imgname)
            frame = cv2.imread(imgname)
            print(frame.shape[1], frame.shape[0])
            if (frame.shape[1], frame.shape[0]) != (mode_img_size_wh[0], mode_img_size_wh[1]):
                imgname = resize_rescale_pilimg(imgname, (mode_img_size_wh[0], mode_img_size_wh[1]))
                frame = cv2.imread(imgname)
            else:
                pass
            img1 = cv2.imread(imgname)  # 加载图像
            img2 = cv2.imread(logo_f_uid)  # 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的该区域赋值回原来的地方
            img3 = cv2.imread(logo_f)  # logo our在右边
            rows, cols, channels = img3.shape
            rows1, cols1, channels1 = img1.shape
            roi = img1[0:rows, cols1 - cols:cols1]  # 取img1的这个区域来处理
            img3gray = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)  # 建立logo的二值图,也建立相反的二值图
            ret, mask = cv2.threshold(img3gray, 175, 255, cv2.THRESH_BINARY)  # 二值化
            mask_inv = cv2.bitwise_not(mask)  # 做非操作,黑的变白,白的变黑,黑色0,白色255
            img1_bg = cv2.bitwise_and(roi, roi, mask=mask)  # 与操作 ,参数输入,输出,与mask做and操作,黑色的被填充
            img3_fg = cv2.bitwise_and(img3, img3, mask=mask_inv)  # 与操作
            dst = cv2.add(img1_bg, img3_fg)  # 相加
            img1[0:rows, cols1 - cols:cols1] = dst  # 把添加了logo的该区域赋值回原来的地方
            frame = img1
            frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_pil = Image.fromarray(frame_cv2)  # 转为PIL的图片格式
            font_size = math.floor((mode_img_size_wh[0]) * 0.040)
            # font = ImageFont.truetype("simhei.ttf", font_size, encoding="utf-8")
            # f_x, f_y = math.floor((mode_img_size_wh[0]) * 0.06), math.floor(
            #     mode_img_size_wh[1] * 0.85) - br_step_times * font_size
            #
            # screen_str = ''
            #
            # ImageDraw.Draw(frame_pil).text((f_x, f_y), screen_str, (255, 0, 0), font)
            frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
            img = frame_cv2
            for fps_loop in range(per_imgperiod_times):
                videoWriter.write(img)
        print('last')
        print(imgname_ori)
        new_l = uid_d[uid]['img_l']
        new_l.remove(imgname_ori)
        imgname = random.choice(new_l)
        frame = cv2.imread(imgname)
        print(frame.shape[1], frame.shape[0])
        if (frame.shape[1], frame.shape[0]) != (mode_img_size_wh[0], mode_img_size_wh[1]):
            imgname = resize_rescale_pilimg(imgname, (mode_img_size_wh[0], mode_img_size_wh[1]))
            frame = cv2.imread(imgname)
        else:
            pass
        img1 = cv2.imread(imgname)  # 加载图像
        img2 = cv2.imread(logo_f_uid)  # 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的该区域赋值回原来的地方
        img3 = cv2.imread(logo_f)  # logo our在右边
        rows, cols, channels = img3.shape
        rows1, cols1, channels1 = img1.shape
        roi = img1[0:rows, cols1 - cols:cols1]  # 取img1的这个区域来处理
        img3gray = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)  # 建立logo的二值图,也建立相反的二值图
        ret, mask = cv2.threshold(img3gray, 175, 255, cv2.THRESH_BINARY)  # 二值化
        mask_inv = cv2.bitwise_not(mask)  # 做非操作,黑的变白,白的变黑,黑色0,白色255
        img1_bg = cv2.bitwise_and(roi, roi, mask=mask)  # 与操作 ,参数输入,输出,与mask做and操作,黑色的被填充
        img3_fg = cv2.bitwise_and(img3, img3, mask=mask_inv)  # 与操作
        dst = cv2.add(img1_bg, img3_fg)  # 相加
        img1[0:rows, cols1 - cols:cols1] = dst  # 把添加了logo的该区域赋值回原来的地方
        frame = img1
        frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_pil = Image.fromarray(frame_cv2)  # 转为PIL的图片格式
        font_size = math.floor((mode_img_size_wh[0]) * 0.040)
        # font = ImageFont.truetype("simhei.ttf", font_size, encoding="utf-8")
        # f_x, f_y = math.floor((mode_img_size_wh[0]) * 0.06), math.floor(
        #     mode_img_size_wh[1] * 0.85) - br_step_times * font_size
        #
        # screen_str = ''
        #
        # ImageDraw.Draw(frame_pil).text((f_x, f_y), screen_str, (255, 0, 0), font)
        frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
        img = frame_cv2
    
        for fps_loop in range(per_imgperiod_mod_times):
            videoWriter.write(img)
    
        videoWriter.release()
        ffmpeg_f_dir = 'D:\myv\ffmpeg-20180331-be502ec-win64-static\bin\ffmpeg.exe'
        res_mp3_f_dir = 'D:\myv\ffmpeg-20180331-be502ec-win64-static\bin\'
        res_v_f_dir = 'D:\myvonline'
        mp3_l = [k for k in uid_d[uid]['sen_d']]
    
        mystep = 5
        loop_times = math.ceil(len(mp3_l) / mystep)
        mp3_2_l = []
        for mp3cut in range(loop_times):
            tmp_l = mp3_l[mp3cut * mystep:(mp3cut + 1) * mystep]
            res_mp3_cut = '{}{}{}{}{}{}{}'.format(res_mp3_f_dir, int(time.time()), '_', uid, 'mp3cut', mp3cut, '.mp3')
            d = '{}{}{}{}{}'.format(ffmpeg_f_dir, '  -i "concat:', '|'.join(tmp_l), '"  -acodec copy ', res_mp3_cut)
            print(d)
            os.system(d)
            time.sleep(3)
            mp3_2_l.append(res_mp3_cut)
    
        res_mp3 = '{}{}{}{}{}'.format(res_mp3_f_dir, int(time.time()), '_', uid, '.mp3')
        #    d = '{}{}{}{}{}'.format(ffmpeg_f_dir, '  -i "concat:', '|'.join(mp3_l), '"  -acodec copy ', res_mp3)
        d = '{}{}{}{}{}'.format(ffmpeg_f_dir, '  -i "concat:', '|'.join(mp3_2_l), '"  -acodec copy ', res_mp3)
        print(d)
        os.system(d)
        #  time.sleep(audio_spend)
        res_v = '{}{}{}{}{}'.format(res_v_f_dir, int(time.time()), '_', uid, '.mp4')
        res_v = '{}{}{}{}{}{}'.format(res_v_f_dir, os_sep, uid_d[uid]['dbid'], '_', t_spend_per_imgperiod, '.mp4')
        d = '{}{}{}{}{}{}{}'.format(ffmpeg_f_dir, ' -i ', res_mp3, ' -i ', f_v, ' ', res_v)
        print(d)
        os.system(d)
        #   time.sleep(audio_spend)
        # time.sleep(30)
        print('fffffffffff')
    

      

    -vframes number 设置转换多少桢(frame)的视频

    帧速率(fps) (可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97)

    mp3_time*fps=vframes
     
  • 相关阅读:
    2.性能测试类型或方法
    3.性能测试通过标准
    下一个问题主题是啥呢?内网渗透深化?Java安全
    ThinkPHP5 RCE在PHP7下getshell
    泛微ecology OA数据库配置信息泄露
    渗透经验之SQL注入深化
    匿名伪造邮件发送脚本
    payload免杀之Installutil.exe&csc.exe利用
    记一次草鸡垃圾的域渗透
    内网渗透之frp使用
  • 原文地址:https://www.cnblogs.com/rsapaper/p/8876209.html
Copyright © 2020-2023  润新知