xiaoxue 发表于 2018-9-8 13:22:29

爬虫生成视频字幕然后youtube

本帖最后由 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 < 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 == '.':
                                 continue
                         L.append(os.path.join(root, file))
    return L


if len(sys.argv) < 2:
      print "filename is Null..."
      exit()
outfilename = sys.argv




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( for i in xrange(0, len(text), n)])
namei = 0
for i in xrange(0, len(text), n):
      audio = client.synthesis(text)
      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 +' ./tmp/j5.mp3')
else:      
      runmp3str = "ffmpeg -y "
      for i in audios:
                runmp3str = runmp3str + "-i " + i + " "
      runmp3str = runmp3str + " -filter_complex ' concat=n=" + str(audiosz) + ":v=0:a=1 ' -map '' ./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 \"loop=-1:size=500:,setpts=N/FRAME_RATE/TB,scale=1280x720;scale=1240x680, setpts=PTS-STARTPTS,format=yuva420p;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 ' scale=1280x720,fade=t=out:st=8:d=2 ; fade=t=in:st=0:d=2 ; concat=n=2:v=1:a=1' -map '' -threads 4 ./tmp/" + outfilename
print runmp3str
os.system(runmp3str)


pt.mp4 是片头 与test.py 放到 一个目录上传 用的 youtube_upload 到github.com自取。
写的非常的乱。 能用的拿去用吧。

Black_industrie 发表于 2018-9-14 18:00:50

再整个画中画吧。不然很容易被人举报。画中画的话我这边封号概率小

xiaoxue 发表于 2018-9-8 13:25:18

本帖最后由 xiaoxue 于 2021-10-19 08:14 编辑

爬虫

...



青鸟 发表于 2018-9-9 17:32:55

这个不错,貌似采集的是公众号的文章生成视屏?

xiaoxue 发表于 2018-9-15 00:47:24

Black_industrie 发表于 2018-9-14 18:00
再整个画中画吧。不然很容易被人举报。画中画的话我这边封号概率小

是画中画 用的fade。

黄伟 发表于 2018-10-3 13:43:19

我想弱弱的问一下,youtube是怎么审核视频是不是原创的或者有没有侵权

Goodbye 发表于 2018-10-10 13:05:58

黄伟 发表于 2018-10-3 13:43
我想弱弱的问一下,youtube是怎么审核视频是不是原创的或者有没有侵权

你传上去之后系统会进行判断

purecafe2015 发表于 2021-8-2 16:32:12

这玩意CC字幕加录屏不是更好。
页: [1]
查看完整版本: 爬虫生成视频字幕然后youtube