python利用AI自动化生成视频

一、通过spacy分词

import spacy
import os

nlp = spacy.load('zh_core_web_sm')
with open('input.txt', 'r', encoding='utf-8') as f:
    text = f.read()
doc = nlp(text)
res = [sent.text.strip() for sent in doc.sents if sent.text.strip()]

idx = 1
txt_zn_folder = 'txt_zn/'
for content in res:
    file_path = os.path.join(txt_zn_folder, str(idx) + '.txt')
    idx += 1
    with open(file_path, 'w') as f:
        f.write(content)

二、通过百度翻译api翻译成英文

import requests
import hashlib
import random
import json
import os
import time 

def translate(text):
    url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
    app_id = ''
    app_key = ''
    salt = random.randint(10000, 99999)
    sign = hashlib.md5((app_id + text + str(salt) + app_key).encode('utf-8')).hexdigest()
    
    data = {
        'q': text,
        'from': 'zh',
        'to': 'en',
        'appid': app_id,
        'salt': salt,
        'sign': sign,
    }
    
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
    
    response = requests.post(url, data=data, headers=headers)
    result = response.json()
    
    if 'trans_result' in result:
        translation = result['trans_result'][0]['dst']
        return translation
    else:
        return None

txt_zn_folder = 'txt_zn/'
txt_zn_files = sorted(os.listdir(txt_zn_folder))
txt_en_folder = 'txt_en/'
idx = 1
for file in txt_zn_files:
    with open(txt_zn_folder + file, 'r', encoding='utf-8') as f:
        text = f.read().strip()
        translation = translate(text)
        print(translation)
        file_path = os.path.join(txt_en_folder, str(idx) + '.txt')
        idx = idx+1
        with open (file_path, 'w') as file:
            file.write(translation)
        time.sleep(1)

三、通过stable diffusion webui 的api模式,利用英文分词生成AI图片

import base64
import json
from requests import post
import os

def submit_post(url, data):
    return post(url, data=json.dumps(data))

def save_encoded_image(b64_image, output):
    with open(output, 'wb') as image_file:
        image_file.write(base64.b64decode(b64_image))

if __name__ == '__main__':
    url = "http://127.0.0.1:7861/sdapi/v1/txt2img"
    image_folder = 'image/'
    txt_en_folder = 'txt_en/'
    txt_en_files = sorted(os.listdir(txt_en_folder))
    idx = 1
    for file in txt_en_files:
        with open(txt_en_folder + file, 'r', encoding='utf-8') as f:
            text = f.read()
            data = {
            'prompt': text,
            'negative_promopt':'',
            'sampler_index': 'DPM++ SDE',
            'seed': 1234,
            'steps': 20,
            'width': 512,
            'height': 512,
            'cfg_scale': 8
            }
            rsp = submit_post(url, data)
            save_encoded_image(rsp.json()['images'][0], image_folder + str(idx) +'.png')
            idx = idx + 1    

四、通过edge_tts生成中文语音

import edge_tts
import asyncio
import os

if __name__ == '__main__':
    txt_zn_folder = 'txt_zn/'
    txt_zn_files = sorted(os.listdir(txt_zn_folder))
    voice_folder = 'audio/'
    idx = 1
    for file in txt_zn_files:
        data = ''
        with open (txt_zn_folder + file, 'r', encoding='utf-8') as f:
            data = f.read()
        print(data)
        voice = 'zh-CN-YunxiNeural'
        rate = '-4%'
        file_path = os.path.join(voice_folder, str(idx) + '.mp3')
        idx = idx + 1
        async def fun():
            tts = edge_tts.Communicate(text=data, voice=voice, rate=rate)
            await tts.save(file_path)
        
        asyncio.run(fun())

五、通过moviepy合并图片和音频,生成短视频

from moviepy.editor import *

# 指定图片文件夹和音频文件夹路径
image_folder = 'image/'
audio_folder = 'audio/'

# 加载图片文件夹中的所有图片,并按照名称排序
image_files = sorted(os.listdir(image_folder))
# 加载音频文件夹中的所有音频,并按照名称排序
audio_files = sorted(os.listdir(audio_folder))

if len(audio_files) != len(image_files):
    print("音频和图片文件数量不同")
    exit()

image_clips = []
audio_clips = []
for i in range(len(audio_files)):
    audio_tmp = AudioFileClip(audio_folder + audio_files[i])
    image_clips.append(ImageClip(image_folder + image_files[i], duration=audio_tmp.duration))
    audio_clips.append(audio_tmp)

# 创建视频剪辑
video = concatenate_videoclips([image.set_audio(audio) for image, audio in zip(image_clips, audio_clips)])

# 输出视频文件
output_path = 'output_path.mp4'
video.write_videofile(output_path, fps=24, codec='libx264')

六、Demo

https://www.douyin.com/video/7237875486997712187

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值