视频生成 量产 win 转 linux ffmpeg linux 安装 对批量视频的尽可能短时间生成

 

 

环境准备

 

 

Welcome to aliyun Elastic Compute Service!

[root@mytest ~]# pip install baidu-aip
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: baidu-aip in ./anaconda3/lib/python3.5/site-packages (2.2.2.0)
Requirement already satisfied: requests in ./anaconda3/lib/python3.5/site-packages (from baidu-aip) (2.14.2)
You are using pip version 10.0.0, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@mytest ~]# pip install bs4
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: bs4 in ./anaconda3/lib/python3.5/site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in ./anaconda3/lib/python3.5/site-packages (from bs4) (4.5.1)
You are using pip version 10.0.0, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@mytest ~]# pip install --upgrade pip
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pip
  Downloading http://mirrors.aliyun.com/pypi/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
    100% |????????????????????????????????| 1.3MB 38.0MB/s 
Installing collected packages: pip
  Found existing installation: pip 10.0.0
    Uninstalling pip-10.0.0:
      Successfully uninstalled pip-10.0.0
Successfully installed pip-10.0.1
[root@mytest ~]# pip install imageio
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: imageio in ./anaconda3/lib/python3.5/site-packages (2.3.0)
Requirement already satisfied: pillow in ./anaconda3/lib/python3.5/site-packages (from imageio) (3.3.1)
Requirement already satisfied: numpy in ./anaconda3/lib/python3.5/site-packages (from imageio) (1.13.1)
[root@mytest ~]# python
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import imageio
>>> imageio.plugins.ffmpeg.download()
Imageio: 'ffmpeg-linux64-v3.3.1' was not found on your computer; downloading it now.
Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg-linux64-v3.3.1 (43.8 MB)
Downloading: 606208/45929032 bytes (1.3%)Error while fetching file: The read operation timed out.
Try 2. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg-linux64-v3.3.1 (43.8 MB)
Downloading: 15040512/45929032 bytes (32.7%)Error while fetching file: The read operation timed out.
Try 3. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg-linux64-v3.3.1 (43.8 MB)
Downloading: 45929032/45929032 bytes (100.0%)
  Done
File saved as /root/.imageio/ffmpeg/ffmpeg-linux64-v3.3.1.
>>> 




[root@mytest ~]# cd /root/.imageio/ffmpeg/
[root@mytest ffmpeg]# ll -as
total 44868
    4 drwxr-xr-x 2 root root     4096 Apr 28 10:17 .
    4 drwxr-xr-x 3 root root     4096 Apr 28 09:45 ..
44860 -rw-r--r-- 1 root root 45929032 Apr 28 10:17 ffmpeg-linux64-v3.3.1
[root@mytest ffmpeg]# pwd
/root/.imageio/ffmpeg
[root@mytest ffmpeg]# ./ffmpeg-linux64-v3.3.1 -i 0.mp3 -i a.avi myunix.mp4
-bash: ./ffmpeg-linux64-v3.3.1: Permission denied
[root@mytest ffmpeg]# ll -as
total 313084
     4 drwxr-xr-x 2 root root      4096 Apr 28 10:23 .
     4 drwxr-xr-x 3 root root      4096 Apr 28 09:45 ..
    20 -rw-r--r-- 1 root root     19944 Apr 28 10:23 0.mp3
268196 -rw-r--r-- 1 root root 274626116 Apr 28 10:22 a.avi
 44860 -rw-r--r-- 1 root root  45929032 Apr 28 10:17 ffmpeg-linux64-v3.3.1
[root@mytest ffmpeg]# chmod 777 ffmpeg-linux64-v3.3.1
[root@mytest ffmpeg]# ll -as
total 313084
     4 drwxr-xr-x 2 root root      4096 Apr 28 10:23 .
     4 drwxr-xr-x 3 root root      4096 Apr 28 09:45 ..
    20 -rw-r--r-- 1 root root     19944 Apr 28 10:23 0.mp3
268196 -rw-r--r-- 1 root root 274626116 Apr 28 10:22 a.avi
 44860 -rwxrwxrwx 1 root root  45929032 Apr 28 10:17 ffmpeg-linux64-v3.3.1
[root@mytest ffmpeg]# ./ffmpeg-linux64-v3.3.1 -i 0.mp3 -i a.avi myunix.mp4
ffmpeg version N-86111-ga441aa90e8-static http://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.1 (Debian 5.4.1-8) 20170304
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-5 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg
  libavutil      55. 63.100 / 55. 63.100
  libavcodec     57. 96.101 / 57. 96.101
  libavformat    57. 72.101 / 57. 72.101
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 89.101 /  6. 89.101
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
[mp3 @ 0x4ce7540] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '0.mp3':
  Duration: 00:00:09.97, start: 0.000000, bitrate: 16 kb/s
    Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 16 kb/s
Input #1, avi, from 'a.avi':
  Duration: 00:01:55.00, start: 0.000000, bitrate: 19104 kb/s
    Stream #1:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj420p(pc, bt470bg/unknown/unknown), 1200x414 [SAR 1:1 DAR 200:69], 19108 kb/s, 20 fps, 20 tbr, 20 tbn, 20 tbc
Stream mapping:
  Stream #1:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
  Stream #0:0 -> #0:1 (mp3 (native) -> aac (native))
Press [q] to stop, [?] for help
No pixel format specified, yuvj420p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x4d05700] using SAR=1/1
[libx264 @ 0x4d05700] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x4d05700] profile High, level 3.1
[libx264 @ 0x4d05700] 264 - core 148 r333 90a61ec - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=20 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'myunix.mp4':
  Metadata:
    encoder         : Lavf57.72.101
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuvj420p(pc, progressive), 1200x414 [SAR 1:1 DAR 200:69], q=-1--1, 20 fps, 10240 tbn, 20 tbc
    Metadata:
      encoder         : Lavc57.96.101 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 16000 Hz, mono, fltp, 69 kb/s
    Metadata:
      encoder         : Lavc57.96.101 aac
frame= 2300 fps=240 q=-1.0 Lsize=    2089kB time=00:01:54.85 bitrate= 149.0kbits/s speed=  12x    
video:1972kB audio:86kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.509317%
[libx264 @ 0x4d05700] frame I:23    Avg QP:13.44  size: 84249
[libx264 @ 0x4d05700] frame P:575   Avg QP:14.62  size:    58
[libx264 @ 0x4d05700] frame B:1702  Avg QP:22.87  size:    28
[libx264 @ 0x4d05700] consecutive B-frames:  1.0%  0.0%  3.0% 96.0%
[libx264 @ 0x4d05700] mb I  I16..4:  6.7% 74.6% 18.7%
[libx264 @ 0x4d05700] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4:  0.4%  0.0%  0.0%  0.0%  0.0%    skip:99.6%
[libx264 @ 0x4d05700] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  0.1%  0.0%  0.0%  direct: 0.0%  skip:99.9%  L0:11.1% L1:88.9% BI: 0.0%
[libx264 @ 0x4d05700] 8x8 transform intra:74.6% inter:69.0%
[libx264 @ 0x4d05700] coded y,uvDC,uvAC intra: 79.1% 83.5% 80.1% inter: 0.0% 0.1% 0.0%
[libx264 @ 0x4d05700] i16 v,h,dc,p: 41% 30%  6% 24%
[libx264 @ 0x4d05700] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 24% 10%  4%  6%  5%  7%  6%  8%
[libx264 @ 0x4d05700] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 26% 24%  8%  6%  8%  7%  7%  7%  7%
[libx264 @ 0x4d05700] i8c dc,h,v,p: 40% 27% 21% 12%
[libx264 @ 0x4d05700] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x4d05700] ref P L0: 98.4%  0.6%  0.8%  0.2%
[libx264 @ 0x4d05700] ref B L0: 77.3% 21.3%  1.4%
[libx264 @ 0x4d05700] ref B L1: 99.0%  1.0%
[libx264 @ 0x4d05700] kb/s:140.41
[aac @ 0x4d06ba0] Qavg: 32328.777
[root@mytest ffmpeg]# ll -as
total 315176
     4 drwxr-xr-x 2 root root      4096 Apr 28 10:25 .
     4 drwxr-xr-x 3 root root      4096 Apr 28 09:45 ..
    20 -rw-r--r-- 1 root root     19944 Apr 28 10:23 0.mp3
268196 -rw-r--r-- 1 root root 274626116 Apr 28 10:22 a.avi
 44860 -rwxrwxrwx 1 root root  45929032 Apr 28 10:17 ffmpeg-linux64-v3.3.1
  2092 -rw-r--r-- 1 root root   2139319 Apr 28 10:25 myunix.mp4
[root@mytest ffmpeg]# 

 

 

 

图文素材到位、声音到位、批量合成  。。。。

 各个 模块 剥离

 

代码拆开 

无限循环 

对批量视频的尽可能短时间生成 

 

import os

os_sep = os.sep
save_dir = '/data/xiaole_dl_img/dlDBimg'
mybanner_dir, mylogo_dir, mymp3_dir, myv_dir, myhtml_dir, myv_tmp_dir = '{}{}{}'.format(save_dir, os_sep,
                                                                                        'mybanner'), '{}{}{}'.format(
    save_dir,
    os_sep,
    'mylogo'), '{}{}{}'.format(
    save_dir, os_sep, 'mymp3'), '{}{}{}'.format(save_dir, os_sep, 'myv'), '{}{}{}'.format(save_dir, os_sep,
                                                                                          'myhtml'), '{}{}{}'.format(
    save_dir, os_sep, 'myv_tmp')
f = 'uid.username.txt'
uid_d = {}
with open(f, 'r', encoding='utf-8') as fr:
    for i in fr:
        uid, un = i.replace('\n', '').split('\t')
        uid_d[uid] = {}
        uid_d[uid]['username'] = un
        uid_d[uid]['banner_img_l'] = []
banner_img_dir = '{}{}{}'.format(mybanner_dir, os_sep, '*.*g')
#print(banner_img_dir)
import glob

banner_imgs = glob.glob(banner_img_dir)
#print(banner_imgs)
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:
    if len(uid_d[uid]['banner_img_l']) > 0:
        uid_d_filter[uid] = {}
        uid_d_filter[uid] = uid_d[uid]
        l.append(uid)
del uid_d

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 = ['\n', '\t', '。', ';', '?', '.', ';', '?', '...', '、、、', ':', '!', '!']
cutlist = ['\n', '\t', '。', ';', '?', '.', '?', '...', '、、、', '!', '!']


# 检查某字符是否分句标志符号的函数;如果是,返回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 = '{}{}'.format(mymp3_dir, os_sep)
    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 = '{}{}'.format(mybanner_dir, os_sep)
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)
f_img_d = '{}{}{}'.format(mylogo_dir, os_sep, '*.jpg')
imgs_logo = glob.glob(f_img_d)
our_logo_f = 'g3logo.jpg'
f_mp3_d = '{}{}{}'.format(mymp3_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)
        print(ll)
        dbid, uid, username, html_ = [i.replace('\t', '') for i in ll]
        print('---------------')
        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(mylogo_dir, os_sep, our_logo_f)
        # myhtml = 'D:\\myhtml\\{}tmp.html'.format(random.randint(123, 999))
        myhtml = '{}{}{}tmp.html'.format(myhtml_dir, os_sep, 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 != '\n' and len(i.replace(' ', '')) > 0:
                sentence_l_noblank.append(i.replace('\n', ''))
        uid_n = 0
        for screen_str in sentence_l_noblank:
            uid_sen = '{}{}{}'.format(uid, '_', uid_n)
            f_w = '{}{}{}{}'.format(mymp3_dir, os_sep, '0428UNIX', 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:

    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'
    res_v_f_dir = myv_dir
    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(mymp3_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')
    f_v = '{}{}{}{}'.format(myv_dir, 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
    t_spend_per_imgperiod_times = math.floor(audio_spend / t_spend_per_imgperiod)
    fps_loop_times_per_imgperiod = math.ceil(t_spend_per_imgperiod * fps)

    imgname = ''
    for myloop in range(t_spend_per_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
        fps_loop_times = t_spend_per_imgperiod * fps
        for fps_loop in range(fps_loop_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
    fps_loop_times = math.ceil(t_spend_per_imgperiod_mod * fps)
    fps_loop_times = max(fps_loop_times, 1)
    for fps_loop in range(fps_loop_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'
    res_v_f_dir =myv_dir
    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')

  

 

 

 

ssh://root@101.21.1.2:22/usr/bin/python -u /data/xiaole_dl_img/product.v.one.banner.fixedperiod.genv.nv.test.py
---------------
/data/xiaole_dl_img/dlDBimg/myv/49855227_5TEST.mp4
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230708_58430.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20171016172703_47076.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230708_58430.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20171016172703_47076.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230708_58430.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20171016172703_47076.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
last
/data/xiaole_dl_img/dlDBimg/mybanner/my_BANNER_c2_20180126230709_39819.jpg
1200 414
/root/.imageio/ffmpeg/ffmpeg-linux64-v3.3.1  -i "concat:/data/xiaole_dl_img/dlDBimg/mymp3/0428UNIX50001655_49855227_0.mp3"  -acodec copy /data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904948_50001655mp3cut0.mp3
ffmpeg version N-86111-ga441aa90e8-static http://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.1 (Debian 5.4.1-8) 20170304
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-5 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg
  libavutil      55. 63.100 / 55. 63.100
  libavcodec     57. 96.101 / 57. 96.101
  libavformat    57. 72.101 / 57. 72.101
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 89.101 /  6. 89.101
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
[mp3 @ 0x5312480] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'concat:/data/xiaole_dl_img/dlDBimg/mymp3/0428UNIX50001655_49855227_0.mp3':
  Duration: 00:01:07.14, start: 0.000000, bitrate: 16 kb/s
    Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 16 kb/s
Output #0, mp3, to '/data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904948_50001655mp3cut0.mp3':
  Metadata:
    TSSE            : Lavf57.72.101
    Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 16 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
size=     131kB time=00:01:07.10 bitrate=  16.0kbits/s speed=7.53e+03x    
video:0kB audio:131kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.167560%
/root/.imageio/ffmpeg/ffmpeg-linux64-v3.3.1  -i "concat:/data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904948_50001655mp3cut0.mp3"  -acodec copy /data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904951_50001655.mp3
ffmpeg version N-86111-ga441aa90e8-static http://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.1 (Debian 5.4.1-8) 20170304
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-5 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg
  libavutil      55. 63.100 / 55. 63.100
  libavcodec     57. 96.101 / 57. 96.101
  libavformat    57. 72.101 / 57. 72.101
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 89.101 /  6. 89.101
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
Input #0, mp3, from 'concat:/data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904948_50001655mp3cut0.mp3':
  Metadata:
    encoder         : Lavf57.72.101
  Duration: 00:01:07.14, start: 0.033063, bitrate: 16 kb/s
    Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 16 kb/s
Output #0, mp3, to '/data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904951_50001655.mp3':
  Metadata:
    TSSE            : Lavf57.72.101
    Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 16 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
size=     131kB time=00:01:07.07 bitrate=  16.0kbits/s speed=7.69e+03x    
video:0kB audio:131kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.167560%
/data/xiaole_dl_img/dlDBimg/myv/49855227_5TEST.mp4
/root/.imageio/ffmpeg/ffmpeg-linux64-v3.3.1 -i /data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904951_50001655.mp3 -i /data/xiaole_dl_img/dlDBimg/myv_middle500016551524904938TEST.avi /data/xiaole_dl_img/dlDBimg/myv/49855227_5TEST.mp4
ffmpeg version N-86111-ga441aa90e8-static http://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.1 (Debian 5.4.1-8) 20170304
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-5 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg
  libavutil      55. 63.100 / 55. 63.100
  libavcodec     57. 96.101 / 57. 96.101
  libavformat    57. 72.101 / 57. 72.101
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 89.101 /  6. 89.101
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
Input #0, mp3, from '/data/xiaole_dl_img/dlDBimg/mymp3_middle/1524904951_50001655.mp3':
  Metadata:
    encoder         : Lavf57.72.101
  Duration: 00:01:07.14, start: 0.033063, bitrate: 16 kb/s
    Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 16 kb/s
Input #1, avi, from '/data/xiaole_dl_img/dlDBimg/myv_middle500016551524904938TEST.avi':
  Duration: 00:01:07.15, start: 0.000000, bitrate: 30906 kb/s
    Stream #1:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj420p(pc, bt470bg/unknown/unknown), 1200x414 [SAR 1:1 DAR 200:69], 30925 kb/s, 20 fps, 20 tbr, 20 tbn, 20 tbc
Stream mapping:
  Stream #1:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
  Stream #0:0 -> #0:1 (mp3 (native) -> aac (native))
Press [q] to stop, [?] for help
No pixel format specified, yuvj420p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x565b1c0] using SAR=1/1
[libx264 @ 0x565b1c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x565b1c0] profile High, level 3.1
[libx264 @ 0x565b1c0] 264 - core 148 r333 90a61ec - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=20 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/data/xiaole_dl_img/dlDBimg/myv/49855227_5TEST.mp4':
  Metadata:
    encoder         : Lavf57.72.101
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuvj420p(pc, progressive), 1200x414 [SAR 1:1 DAR 200:69], q=-1--1, 20 fps, 10240 tbn, 20 tbc
    Metadata:
      encoder         : Lavc57.96.101 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 16000 Hz, mono, fltp, 69 kb/s
    Metadata:
      encoder         : Lavc57.96.101 aac
frame= 1343 fps=175 q=-1.0 Lsize=    2242kB time=00:01:07.13 bitrate= 273.6kbits/s speed=8.74x    
video:1633kB audio:573kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.640778%
[libx264 @ 0x565b1c0] frame I:10    Avg QP:15.61  size:162770
[libx264 @ 0x565b1c0] frame P:336   Avg QP:15.84  size:    56
[libx264 @ 0x565b1c0] frame B:997   Avg QP:20.75  size:    25
[libx264 @ 0x565b1c0] consecutive B-frames:  0.7%  0.1%  2.0% 97.1%
[libx264 @ 0x565b1c0] mb I  I16..4:  1.6% 77.2% 21.2%
[libx264 @ 0x565b1c0] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4:  0.4%  0.0%  0.0%  0.0%  0.0%    skip:99.6%
[libx264 @ 0x565b1c0] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  0.0%  0.0%  0.0%  direct: 0.0%  skip:100.0%  L0:32.1% L1:67.9% BI: 0.0%
[libx264 @ 0x565b1c0] 8x8 transform intra:77.2% inter:36.3%
[libx264 @ 0x565b1c0] coded y,uvDC,uvAC intra: 95.6% 92.3% 86.8% inter: 0.0% 0.1% 0.0%
[libx264 @ 0x565b1c0] i16 v,h,dc,p: 16% 26% 23% 35%
[libx264 @ 0x565b1c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 24% 17%  5%  6%  6%  8%  7%  9%
[libx264 @ 0x565b1c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 18% 10%  8%  9%  8%  9%  9%  9%
[libx264 @ 0x565b1c0] i8c dc,h,v,p: 45% 24% 18% 13%
[libx264 @ 0x565b1c0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x565b1c0] ref P L0: 98.5%  0.1%  1.1%  0.4%
[libx264 @ 0x565b1c0] ref B L0: 67.5% 32.5%
[libx264 @ 0x565b1c0] ref B L1: 89.6% 10.4%
[libx264 @ 0x565b1c0] kb/s:199.16
[aac @ 0x565c660] Qavg: 48848.047
fffffffffff

Process finished with exit code 0

 

import glob
import os
import random
import time, math
import logging

start_time = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))
os_sep = os.sep
this_file_abspath, this_file_name = os.path.dirname(os.path.abspath(__file__)), os.path.abspath(__file__).split(os_sep)[
    -1]
logf = this_file_name + '.log'
try:
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s[thread:%(thread)d][process:%(process)d]',
                        datefmt='%a, %d %b %Y %H:%M:%S',
                        filename=logf,
                        filemode='a')
except Exception as e:
    s = '%s%s%s' % ('logging.basicConfig EXCEPTION ', time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time())), e)
    with open(logf, 'a') as fo:
        fo.write(s)
        os._exit(4002)
logging.info('START')

save_dir = '/data/xiaole_dl_img/dlDBimg'
mybanner_dir, mylogo_dir, mymp3_dir, myv_dir, myhtml_dir, myv_tmp_dir = '{}{}{}'.format(save_dir, os_sep,
                                                                                        'mybanner'), '{}{}{}'.format(
    save_dir,
    os_sep,
    'mylogo'), '{}{}{}'.format(
    save_dir, os_sep, 'mymp3'), '{}{}{}'.format(save_dir, os_sep, 'myv'), '{}{}{}'.format(save_dir, os_sep,
                                                                                          'myhtml'), '{}{}{}'.format(
    save_dir, os_sep, 'myv_tmp')

# 0428UNIX50005499_32.mp3
today_s = '0428UNIX'
f_mp3_d = '{}{}{}'.format(mymp3_dir, os_sep, '*.mp3')
mp3s = glob.glob(f_mp3_d)
mp3s_uid_set = set([i.split(os_sep)[-1].split('_')[0].split(today_s)[-1] for i in mp3s])

f = 'uid.username.txt'
un_uid_d = {}
with open(f, 'r', encoding='utf-8') as fr:
    for i in fr:
        uid, un = i.replace('\n', '').split('\t')
        un_uid_d[un] = uid

had_banner_uid_l = []
# beijingshifang_BANNER_c2_20161031091307_30624.jpg
f_banner_d = '{}{}{}'.format(mybanner_dir, os_sep, '*.*g')
banners = glob.glob(f_banner_d)
banners_un_set = set([i.split(os_sep)[-1].split('_BANNER_')[0] for i in banners])
for un in banners_un_set:
    if un in un_uid_d:
        uid = un_uid_d[un]
        if uid not in had_banner_uid_l:
            had_banner_uid_l.append(uid)
banners_uid_set = set(had_banner_uid_l)
# 34031923logo.jpg
f_logo_d = '{}{}{}'.format(mylogo_dir, os_sep, '*.*g')
logos = glob.glob(f_logo_d)
logos_uid_set = set([i.split(os_sep)[-1].split('logo.')[0] for i in logos])

banners_logos_set = banners_uid_set & logos_uid_set

gen_d, uid_d = {}, {}

banner_img_dir = '{}{}{}'.format(mybanner_dir, os_sep, '*.*g')
banner_imgs = glob.glob(banner_img_dir)

for uid in banners_logos_set:
    uid_d[uid] = {}
    un, logo_f_uid, imgs_l = '', '', []
    for un_ in un_uid_d:
        if un_uid_d[un_] == uid:
            un = un_
    for i in banners:
        if un in i:
            imgs_l.append(i)
    for i in logos:
        if uid in i:
            logo_f_uid = i
    uid_d[uid]['un'], uid_d[uid]['logo_f_uid'], uid_d[uid]['imgs_l'] = un, logo_f_uid, imgs_l

from bs4 import *

xls = 'XLS0419'
f = 'db.data.snap.txt'
f = 'db.data.snap428am.txt'

myp, myp_num = 5, 7
with open(f, 'r', encoding='utf-8') as fr:
    for i in fr:
        ll = i.split(xls)

        dbid, uid, username, html_ = [i.replace('\t', '') for i in ll]

        if uid not in uid_d:
            continue

        if int(dbid) % myp_num != myp:
            # if int(dbid) != 49855227:
            continue

        # print(ll)

        # if uid not in banners_logos_set:
        #     print('------------NOT in banners_logos_set')
        #     continue
        #
        # if uid not in mp3s_uid_set:
        #     print('------------uid not in mp3s_uid_set')
        #     continue

        gen_d[dbid] = {}
        gen_d[dbid]['uid'], gen_d[dbid]['mp3'] = uid, {}
        print('---------------')
        myhtml = '{}{}{}tmp.html'.format(myhtml_dir, os_sep, 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
        # GOOD!!!!   字幕同步
        # sentence_l = Cut(list(cutlist), list(txt))
        # sentence_l_noblank = []
        # for i in sentence_l:
        #     if i != '\n' and len(i.replace(' ', '')) > 0:
        #         sentence_l_noblank.append(i.replace('\n', ''))
        # GOOD!!!!
        str_per_len = 300
        r_ = math.ceil(len(txt) / str_per_len)
        sentence_l_noblank = [txt[i * str_per_len:(i + 1) * str_per_len] for i in range(r_)]
        uid_n = 0
        for screen_str in sentence_l_noblank:
            uid_sen = '{}{}{}{}{}'.format(uid, '_', dbid, '_', uid_n)
            f_w = '{}{}{}{}{}'.format(mymp3_dir, os_sep, '0428UNIX', uid_sen, '.mp3')
            # print(screen_str)
            gen_d[dbid]['mp3'][f_w] = ''
            try:
                # gen_bd_mp3(uid_sen, screen_str, f_w)
                pass
            except Exception as e:
                l = [str(i) for i in [uid, uid_d[uid]['dbid'], e, screen_str]]
                log_s = '||'.join(l)
                logging.exception(log_s)
                print(log_s)

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


logo_f = '{}{}{}'.format(mylogo_dir, os_sep, 'g3logo.jpg')
for dbid in gen_d:
    s = '----------myp', myp, 'myp_num', myp_num, '-------------------'
    print(s)
    logging.info(s)
    uid, mp3_l = gen_d[dbid]['uid'], gen_d[dbid]['mp3']
    logo_f_uid, imgs_l = uid_d[uid]['logo_f_uid'], uid_d[uid]['imgs_l']
    res_v_f_dir = myv_dir
    f_img_d = '{}{}{}'.format(res_v_f_dir, os_sep, '*.mp4')
    vs = glob.glob(f_img_d)
    t_spend_per_imgperiod = 5
    res_v = '{}{}{}{}{}{}'.format(res_v_f_dir, os_sep, dbid, '_', t_spend_per_imgperiod, '.mp4')
    print(res_v)
    if res_v in vs:
        continue
    img_size_d = {}
    w_h_size_min = [12345, 12345]
    first_one = imgs_l.append(imgs_l[int(time.time()) % len(imgs_l)])
    imgs_l = [i for i in sorted(imgs_l, reverse=True)]

    img_break = False
    for i in imgs_l:
        img = cv2.imread(i)
        try:
            w_h_s = '{},{}'.format(img.shape[1], img.shape[0])
        except Exception as e:
            l = [str(ii) for ii in [dbid, i, e, 'img_break']]
            log_s = '||'.join(l)
            logging.exception(log_s)
            print(log_s)
            img_break = True
            break

        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
    if img_break:
        continue
    # 取众数
    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

    if mode_img_size_wh[0] * mode_img_size_wh[1] > 1200 * 414:
        mode_img_size_wh = [1200, 414]
    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 mp3_l:
        # gen mp3 --SMALL-DATA
        EasyID3.valid_keys["comment"] = "COMM::'XXX'"
        try:
            id3info = MP3(f_mp3, ID3=EasyID3)
        except Exception as e:
            l = [str(i) for i in [dbid, f_mp3, e]]
            log_s = '||'.join(l)
            logging.exception(log_s)
            print(log_s)
            continue
        audio_spend += id3info.info.length
        #  f_v = '{}{}{}{}'.format('D:\\myv\\', uid, int(time.time()), '.avi')
    f_v = '{}{}{}{}'.format(myv_dir.replace('myv', 'myv_middle'), uid, int(time.time()), 'TEST.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]))
    imgs_l_len = len(uid_d[uid]['imgs_l'])
    t_spend_per_imgperiod = 5
    t_spend_per_imgperiod_mod = audio_spend % t_spend_per_imgperiod
    t_spend_per_imgperiod_times = math.floor(audio_spend / t_spend_per_imgperiod)
    fps_loop_times_per_imgperiod = math.ceil(t_spend_per_imgperiod * fps)
    imgname = ''
    imgname_ori = ''

    img_break = False

    for myloop in range(t_spend_per_imgperiod_times):
        imgname_ori = uid_d[uid]['imgs_l'][myloop % imgs_l_len]
        imgname = uid_d[uid]['imgs_l'][myloop % imgs_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的这个区域来处理
        try:
            img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)  # 建立logo的二值图,也建立相反的二值图
        except Exception as e:
            l = [str(ii) for ii in [dbid, img2, e, 'img_break']]
            log_s = '||'.join(l)
            logging.exception(log_s)
            print(log_s)
            img_break = True
            break

        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
        fps_loop_times = t_spend_per_imgperiod * fps
        for fps_loop in range(fps_loop_times):
            videoWriter.write(img)
    if img_break:
        continue

    print('last')
    print(imgname_ori)
    if imgname_ori == '':
        print('__________________')
        l = [str(i) for i in [dbid, '', '']]
        log_s = '||'.join(l)
        logging.exception(log_s)
        print(log_s)
        continue
    new_l = uid_d[uid]['imgs_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
    fps_loop_times = math.ceil(t_spend_per_imgperiod_mod * fps)
    fps_loop_times = max(fps_loop_times, 1)
    for fps_loop in range(fps_loop_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'
    # res_v_f_dir =myv_dir
    #
    ffmpeg_f_dir = '/root/.imageio/ffmpeg/ffmpeg-linux64-v3.3.1'
    res_mp3_f_dir = '{}{}'.format(mymp3_dir.replace('mymp3', 'mymp3_middle'), os_sep)
    res_v_f_dir = myv_dir

    mp3_l_l = [i for i in mp3_l]
    mp3_l = mp3_l_l
    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, os_sep, uid_d[uid]['dbid'], '_', t_spend_per_imgperiod, 'TEST.mp4')
    print(res_v)
    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')

  

 

posted @ 2018-04-28 10:34  papering  阅读(1440)  评论(0编辑  收藏  举报