|
本帖最后由 xiaoxue 于 2019-3-4 20:50 编辑
#爬虫调用test.py 生成视频
- # -*- coding: utf-8 -*-
- from aip import AipSpeech
- import os
- import sys
- from PIL import Image, ImageDraw,ImageFont
- """ 你的 APPID AK SK """
- APP_ID = 'XXX'
- API_KEY = 'XXX'
- SECRET_KEY = 'XXX'
- client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
- def tupiandaxiao_old(file):
- try:
- im = Image.open(file)
- # 图片的宽度和高度
- img_size = im.size
- '''
- 裁剪:传入一个元组作为参数
- 元组里的元素分别是:(距离图片左边界距离x, 距离图片上边界距离y,距离图片左边界距离+裁剪框宽度x+w,距离图片上边界距离+裁剪框高度y+h)
- '''
- # 截取图片中一块宽和高都是250的
- print(file+"图片宽度和高度分别是{}".format(img_size))
- im = im.resize((1280, 720),Image.ANTIALIAS)
- im.save(file)
- except IOError:
- return False
- def tupiandaxiao(file):
- try:
- base_img = Image.new("RGB",(1280,720),"#000000")
- (w, h) = base_img.size
- tmp_img = Image.open(file)
- if tmp_img.size[1] < 720:
- tmp_img = tmp_img.resize((720, 720),Image.ANTIALIAS)
- (tmpw, tmph) = tmp_img.size
- neww = (w/2) - (tmpw/2)
- newh = (h/2) - (tmph/2)
- base_img.paste(tmp_img,(neww,newh,(neww+tmpw),(newh+tmph)))
- base_img.save(file)
- except IOError:
- return False
- def file_name(file_dir):
- L=[]
- for root, dirs, files in os.walk(file_dir):
- for file in files:
- if file[0] == '.':
- continue
- L.append(os.path.join(root, file))
- return L
- if len(sys.argv) < 2:
- print "filename is Null..."
- exit()
- outfilename = sys.argv[1]
- file = open('./text.txt','r')
- text = file.readlines()
- if text == '':
- print 'text is null!!!'
- exit()
- text = ''.join(text)
- print text
- text = unicode(text, 'utf-8')
- n = 500
- #print([text[i:i+n] for i in xrange(0, len(text), n)])
- namei = 0
- for i in xrange(0, len(text), n):
- audio = client.synthesis(text[i:i+n])
- file = open('./tmp/audio/'+str(namei)+'.mp3','wb')
- namei = namei + 1
- file.write(''.join(audio))
- file.close()
- pics = file_name('./tmp/pic/')
- picsz = len(pics)
- for pic in pics:
- tupiandaxiao_old(pic)
- audios = file_name('./tmp/audio/')
- audios = sorted(audios)
- audiosz = len(audios)
- if len(audios) <= 1:
- os.system('cp ' + audios[0] +' ./tmp/j5.mp3')
- else:
- runmp3str = "ffmpeg -y "
- for i in audios:
- runmp3str = runmp3str + "-i " + i + " "
- runmp3str = runmp3str + " -filter_complex '[0:0] [1:0] concat=n=" + str(audiosz) + ":v=0:a=1 [a]' -map '[a]' ./tmp/j5.mp3"
- print runmp3str
- os.system(runmp3str)
- timesec = os.popen('ffprobe -i ./tmp/j5.mp3 -show_entries format=duration -v quiet -of csv="p=0"').read().replace('\n','')
- framerate = picsz / float(timesec)
- print ('framerate:%s - timesec:%s' % (framerate,timesec))
- #os.system('cat ./tmp/pic/* |ffmpeg -y -f image2 -loop 1 -framerate ' + str(framerate) + ' -f image2pipe -i - -c:v libx264 -t ' + str(timesec) + ' ./tmp/output.mp4')
- runvideostr = 'cat ./tmp/pic/* |ffmpeg -loglevel warning -threads 2 -y -i ./tmp/j5.mp3 -f image2 -loop 1 -framerate ' + str(framerate) + ' -f image2pipe -i - -c:v libx264 -t ' + str(timesec) + ' -max_muxing_queue_size 99999 -r 25 -shortest ./tmp/outout2.mp4'
- print runvideostr
- os.system(runvideostr)
- runvideostr = "ffmpeg -loglevel warning -threads 2 -y -i ./tmp/outout2.mp4 -c:v libx264 -crf 22 -preset slow -pix_fmt yuv420p -c:a libmp3lame -ac 2 -ar 44100 -vf "drawtext=fontcolor=f9f1ea:shadowx=2:shadowy=2:fontsize=30:fontfile=msyh.ttf:textfile=text.txt:reload=1:y=h-line_h-52:x=w-(mod(8*n\\,w+tw)-tw/40)" -vcodec libx264 ./tmp/" + outfilename.replace('.mp4','.mkv')
- print runvideostr
- os.popen(runvideostr)
- runmp3str = "ffmpeg -loglevel warning -i ./tmp/" + outfilename.replace('.mp4','.mkv') + " -i pt.mkv -filter_complex "[1:v]loop=-1:size=500:,setpts=N/FRAME_RATE/TB,scale=1280x720[t1];[0:v]scale=1240x680, setpts=PTS-STARTPTS,format=yuva420p[t0];[t1][t0]overlay=20:20:shortest=1:enable='between(t\\,0.5,"+ str(timesec) +")'" -c:a copy -y -threads 4 ./tmp/output.mkv"
- print runmp3str
- os.system(runmp3str)
- runmp3str = "ffmpeg -loglevel warning -y -i pt.mp4 -i ./tmp/output.mkv -filter_complex '[0:v] scale=1280x720,fade=t=out:st=8:d=2 [v0]; [1:v] fade=t=in:st=0:d=2 [v1]; [v0][0:a][v1][1:a] concat=n=2:v=1:a=1[v]' -map '[v]' -threads 4 ./tmp/" + outfilename
- print runmp3str
- os.system(runmp3str)
复制代码
pt.mp4 是片头 与test.py 放到 一个目录上传 用的 youtube_upload 到github.com自取。
写的非常的乱。 能用的拿去用吧。
|
|