网站的开发环境怎么写,wordpress自动采集发布,徐州手机网站制作,徐州网站建设xlecChatGPT Stable Diffusion 百度AI MoviePy 实现文字生成视频#xff0c;小说转视频#xff0c;自媒体神器#xff01;(二) 前言
最近大模型频出#xff0c;但是对于我们普通人来说#xff0c;如何使用这些AI工具来辅助我们的工作呢#xff0c;或者参与进入我们的生活…ChatGPT Stable Diffusion 百度AI MoviePy 实现文字生成视频小说转视频自媒体神器(二) 前言
最近大模型频出但是对于我们普通人来说如何使用这些AI工具来辅助我们的工作呢或者参与进入我们的生活就着现在比较热门的几个AI写个一个提高生产力工具现在在逻辑上已经走通了后面会针对web页面、后台进行优化。 github链接 B站教程视频 https://www.bilibili.com/video/BV18M4y1H7XN/ 第三步、调用百度语音合成包进行语音合成 这里不是智能用百度的API合成想谷歌的阿里云的都可以只是我比较熟悉百度的API ps~: 关键是免费 class Main:client_id client_idclient_secret client_secretdef create_access_token(self):url fhttps://aip.baidubce.com/oauth/2.0/token?grant_typeclient_credentialsclient_id{self.client_id}client_secret{self.client_secret}payload headers {Content-Type: application/json,Accept: application/json}response requests.request(POST, url, headersheaders, datapayload)print(-----------向百度获取 access_token API 发起请求了-----------)access_token response.json()access_token.update({time: datetime.now().strftime(%Y-%m-%d)})with open(access_token.json, w) as f:json.dump(access_token, f)return access_tokendef get_access_token(self):if os.path.exists(access_token.json):with open(access_token.json, r) as f:data json.load(f)time data.get(time)if time and (datetime.now() - datetime.strptime(time, %Y-%m-%d)).days 29:return self.create_access_token()return datareturn self.create_access_token()def text_to_audio(self, text: str, index: int):url https://tsn.baidu.com/text2audiotext text.encode(utf8)FORMATS {3: mp3, 4: pcm, 5: pcm, 6: wav}FORMAT FORMATS[6]data {# 合成的文本文本长度必须小于1024GBK字节。建议每次请求文本不超过120字节约为60个汉字或者字母数字。tex: text,# access_tokentok: self.get_access_token().get(access_token),# 用户唯一标识用来计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码长度为60字符以内cuid: hex(uuid.getnode()),# 客户端类型选择web端填写固定值1ctp: 1,# 固定值zh。语言选择,目前只有中英文混合模式填写固定值zhlan: zh,# 语速取值0-15默认为5中语速spd: 5,# 音调取值0-15默认为5中语调pit: 5,# 音量基础音库取值0-9精品音库取值0-15默认为5中音量取值为0时为音量最小值并非为无声vol: 5,# (基础音库) 度小宇1度小美0度逍遥基础3度丫丫4# (精品音库) 度逍遥精品5003度小鹿5118度博文106度小童110度小萌111度米朵103度小娇5per: 5003,# 3为mp3格式(默认) 4为pcm-16k5为pcm-8k6为wav内容同pcm-16k; 注意aue4或者6是语音识别要求的格式但是音频内容不是语音识别要求的自然人发音所以识别效果会受影响。aue: FORMAT}data urllib.parse.urlencode(data)response requests.post(url, data)if response.status_code 200:result_str response.contentsave_file str(index) . FORMATaudio file_path audioif not os.path.isdir(audio):os.mkdir(audio)audio_path f{audio}/ save_filewith open(audio_path, wb) as of:of.write(result_str)return audio_pathelse:return False当然了这个设计也是热拔插的以后这些数据都会做成动态的在页面用户可以调整也可以选择其他的API服务商 第四步、调用百度语音合成包进行语音合成 这里就比较麻烦了首先要搭建起 Stable Diffusion 的环境Window 用户我记得有一个 绘世 的软件一键就可以安装mac用户要去官网下载。 class Main:sd_url sd_urldef draw_picture(self, obj_list)::param obj_list::return: 图片地址列表picture_path_list []for index, obj in enumerate(obj_list):novel_dict {enable_hr: false,denoising_strength: 0,firstphase_width: 0,firstphase_height: 0,hr_scale: 2,hr_upscaler: string,hr_second_pass_steps: 0,hr_resize_x: 0,hr_resize_y: 0,prompt: {}.format(obj[prompt]),styles: [string],seed: -1,subseed: -1,subseed_strength: 0,seed_resize_from_h: -1,seed_resize_from_w: -1,sampler_name: DPM SDE Karras,batch_size: 1,n_iter: 1,steps: 50,cfg_scale: 7,width: 1024,height: 768,restore_faces: false,tiling: false,do_not_save_samples: false,do_not_save_grid: false,negative_prompt: obj[negative],eta: 0,s_churn: 0,s_tmax: 0,s_tmin: 0,s_noise: 1,override_settings: {},override_settings_restore_afterwards: true,script_args: [],sampler_index: DPM SDE Karras,script_name: ,send_images: true,save_images: true,alwayson_scripts: {}}html requests.post(self.sd_url, datajson.dumps(novel_dict))img_response json.loads(html.text)image_bytes base64.b64decode(img_response[images][0])image Image.open(io.BytesIO(image_bytes))# 图片存放new_path file_path pictureif not os.path.exists(new_path):os.makedirs(new_path)picture_name str(obj[index]) .pngimage_path os.path.join(new_path, picture_name)image.save(image_path)picture_path_list.append(image_path)print(f-----------生成第{index}张图片-----------)return picture_path_list后期我看看能不能引入 Midjuorney 的服务商或者他们官方的API ps ~ 做人没有梦想和咸鱼有什么区别 第五步、使用moviepy将图片和语音结合起来生成视频 moviepy中文文档 import os
from moviepy.editor import ImageSequenceClip, AudioFileClip, concatenate_videoclips
import numpy as npfrom config import file_pathclass Main:def merge_video(self, picture_path_list: list, audio_path_list: list, name: str)::param picture_path_list: 图片路径列表:param audio_path_list: 音频路径列表:return:clips []for index, value in enumerate(picture_path_list):audio_clip AudioFileClip(audio_path_list[index])img_clip ImageSequenceClip([picture_path_list[index]], audio_clip.duration)img_clip img_clip.set_position((center, center)).fl(self.fl_up, apply_to[mask]).set_duration(audio_clip.duration)clip img_clip.set_audio(audio_clip)clips.append(clip)print(f-----------生成第{index}段视频-----------)print(f-----------开始合成视频-----------)final_clip concatenate_videoclips(clips)new_parent file_path video/if not os.path.exists(new_parent):os.makedirs(new_parent)final_clip.write_videofile(new_parent name .mp4, fps24, audio_codecaac)def fl_up(self, gf, t):# 获取原始图像帧frame gf(t)# 进行滚动效果将图像向下滚动50像素height, width frame.shape[:2]scroll_y int(t * 10) # 根据时间t计算滚动的像素数new_frame np.zeros_like(frame)# 控制滚动的范围避免滚动超出图像的边界if scroll_y height:new_frame[:height - scroll_y, :] frame[scroll_y:, :]return new_frame暂时就先写到这里了后期努力添砖加瓦。 代码已经开源了。链接 有什么问题可以在github上或者博客介绍里来问我byebye~