-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
LinightKira
wants to merge
1
commit into
geekan:main
Choose a base branch
from
LinightKira:小说推文by宏伟
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "\u5C0F\u8BF4\u63A8\u6587by\u5B8F\u4F1F"
Open
小说推文AgentExample #935
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
# return Novel.query.get(nid) #此处可以对接到数据库 | ||
print('nid:', nid) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
,单词之间用下划线 _ 分隔,并且所有字母都是小写的