Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小说推文AgentExample #935

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/novel_to_video/actions/get_novel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# @Date : 2024/2/5 16:28
# @Author : 宏伟(散人)
# @Desc :

from MetaGPT.metagpt.actions import Action


class Novel:
id = 0
title = ''
alias = ''
chapters = []

def __init__(self, id, title, alias, chapters):
self.id = id
self.title = title
self.alias = alias
self.chapters = chapters


class Chapter:
title = ''
content = ''

def __init__(self, title, content):
self.title = title
self.content = content

def GetNovelById(nid):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数命名需要改成snake case命名,比如get_novel_by_id,单词之间用下划线 _ 分隔,并且所有字母都是小写的

# return Novel.query.get(nid) #此处可以对接到数据库
print('nid:', nid)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

日志用metagpt.logs的logger来打印。

from metagpt.logs import logger

logger.debug("xxx")
logger.info("xxx")
logger.warning("xxx")

chapter1 = Chapter(title='Chapter 1', content='This is the content of Chapter 1.')
chapter2 = Chapter(title='Chapter 2', content='This is the content of Chapter 2.')
novel = Novel(id=1, title='Novel 1', alias='N1', chapters=[chapter1, chapter2])
return novel


class GetNovel(Action):
def __init__(self, name: str = "", *args, **kwargs):
super().__init__(**kwargs)

async def run(self, novel_id: str = '', *args, **kwargs) -> Novel:
novel = GetNovelById(novel_id)
return novel




116 changes: 116 additions & 0 deletions examples/novel_to_video/actions/make_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# -*- coding: utf-8 -*-
# @Date : 2024/2/5 16:28
# @Author : 宏伟(散人)
# @Desc :

import asyncio
import os

import cv2
from moviepy.audio.AudioClip import concatenate_audioclips
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.video.io.VideoFileClip import VideoFileClip
from metagpt.actions import Action

from MetaGPT.examples.novel_to_video.roles.ai_novel_video import WORKSPACE_DIR


class MakeVideo(Action):
# 定义工作文件夹路径
workspace: str = ""

def __init__(self, name: str = "", workspace: str = '', *args, **kwargs):
super().__init__(**kwargs)
self.workspace = workspace

async def run(self, *args, **kwargs) -> str:
image_folder = self.workspace+'/image'
audio_folder = self.workspace+'/tts'
# 获取图片和音频文件的名称
# print('image_folder:', image_folder)
# print('audio_folder:', audio_folder)
image_names = os.listdir(image_folder)
audio_names = os.listdir(audio_folder)
output_file = self.workspace + '/output_temp.mp4'
output_file_final = self.workspace + '/output_final.mp4'
# 初始化视频写入器
frame_width = 768
frame_height = 512

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_video = cv2.VideoWriter(output_file, fourcc, 30, (frame_width, frame_height), True)

for i in range(len(audio_names)):
print(f'Processing {i}/{len(audio_names)}')
img_path = os.path.join(image_folder, image_names[i])
audio_path = os.path.join(audio_folder, audio_names[i])
print('img_path:', img_path)
print('audio_path:', audio_path)

# 读取图片和音频
audio = AudioFileClip(audio_path)
audio_duration = audio.duration # 音频的时长(秒)
audio_fps = audio.fps # 音频的采样率(Hz)
# print('audio_duration:', audio_duration)
# print('audio_fps:', audio_fps) # 一般来说,音频的采样率是44100Hz,但可能因文件而异

num_frames = int(audio_duration * 30)

for _ in range(num_frames):
img = cv2.imread(img_path)
output_video.write(img)

# 释放写入器
output_video.release()

# 写入音频
audio_clips = []
previous_end_time = 0
video_clip = VideoFileClip(output_file)
for i in range(len(audio_names)):
# print(f'Processing {i}/{len(audio_names)}')
audio_path = os.path.join(audio_folder, audio_names[i])
# print('audio_path:', audio_path)

# 读取图片和音频
audio_clip = AudioFileClip(audio_path)
audio_duration = audio_clip.duration # 音频的时长(秒)
audio_fps = audio_clip.fps # 音频的采样率(Hz)
# print('audio_duration:', audio_duration)
# print('audio_fps:', audio_fps) # 一般来说,音频的采样率是44100Hz,但可能因文件而异

if i == 0:
audio_clip = audio_clip.set_start(0)
else:
audio_clip = audio_clip.set_start(previous_end_time)

previous_end_time += audio_duration

# 将音频片段存储在列表中
audio_clips.append(audio_clip)

# 将音频合成到视频
# 使用 concatenate_audio clips 方法连接音频片段
final_audio = concatenate_audioclips(audio_clips)

# 将最终音频附加到视频
video_clip = video_clip.set_audio(final_audio)

# 保存输出视频
video_clip.write_videofile(output_file_final, codec='libx264', audio_codec='aac')

# 删除临时文件
if os.path.exists(output_file):
os.remove(output_file)

return output_file_final


async def main():
action = MakeVideo(workspace=WORKSPACE_DIR)
res = await action.run()
print(res)


if __name__ == '__main__':
asyncio.run(main())