一句话生成视频(python) 最近想运营个抖音号, 但是制作视频是个很麻烦的事情.
所以就想到运营个名言警句和新闻的抖音号. 因为名言警句和新闻是可以爬的, 这样自动生成视频.
1.文字自动生成图片居中,
2.自动把文字读出来生成语音,
3.把图片和文字合成mp4文件; 下面是抖音号, 可以点开url看下视频效果: 1.生成图片, 文字居中sum_width = 1125 sum_height = 2436 def create_pic(number, text): text = handle_text(text) if len(text) > 0: im = Image.new('RGB', (sum_width, sum_height), (255, 255, 255)) font = ImageFont.truetype(os.path.join("fonts", "msyh.ttf"), 150) dr = ImageDraw.Draw(im) w, h = dr.textsize(text, font) dr.text(((sum_width - w) / 2, (sum_height - h) / 2), text, font=font, fill='#000000') # im.show() im.save('pic/' + number + '.png') def handle_text(text): try: result = '' array = cut(text, 6) for v in array: result = result + v + '\n' array = result.split('。') result = array[0] + '。\n' + array[1] return result except: return '' def cut(obj, sec): return [obj[i:i+sec] for i in range(0,len(obj),sec)] 2.文字生成语音 使用最niubi最像人的ai合成语音库(azure TTS API)
github def create_audio(number, text): cmd = 'python3 -m aspeak -t "{}" -l zh-CN -o audios/{}.mp3 --mp3'.format(text, number) os.system(cmd) 3.图片+语音合成视频import cv2from moviepy.editor import VideoFileClip, AudioFileClipfrom mutagen.mp3 import MP3 def create_video(number): pic_path = ('pic/' + number + '.png') audio_path = ('audios/' + number + '.mp3') video_path = ('videos/' + number + '.mp4') pic_frame = cv2.imread(pic_path) img_size = (sum_width, sum_height) fourcc = cv2.VideoWriter_fourcc(*'mp4v') videoWriter = cv2.VideoWriter(video_path, fourcc, 1, img_size) audio_time_count = get_audio_time_count(number) print('audio time: ', audio_time_count) for i in range(audio_time_count): videoWriter.write(pic_frame) videoWriter.release() video = VideoFileClip(video_path) video_clip = video.set_audio(AudioFileClip(audio_path)) video_clip.write_videofile(video_path) def get_audio_time_count(number): audio = MP3('audios/' + number + '.mp3') time_count = int(audio.info.length) return time_count
想法是美好的, 实际上上传了十多个视频等了2天并没有带来太多观看
下次尝试下其他的视频类型. (比如其他知识类的视频) 如果有流量后期把app自动上传也做了, 没流量就不白费功夫了.
|