先决条件

要使用 MoviePyVideoTools,您需要安装 moviepy 及其依赖项 ffmpeg
pip install moviepy ffmpeg
对字幕工作流的重要性: create_srtembed_captions 工具需要视频音频的转录。MoviePyVideoTools 本身不执行语音到文本的转换。您通常会使用另一个工具,例如带有 transcribe_audio 函数的 OpenAITools,来生成转录(通常是 SRT 格式),然后由这些工具使用。

示例

以下示例演示了一个完整的流程,代理将 MoviePyVideoToolsOpenAITools 结合使用来:
  1. 从视频文件中提取音频
  2. 使用 OpenAI 的语音到文本功能转录音频
  3. 从转录生成 SRT 字幕文件
  4. 将字幕嵌入到带有单词级别高亮的视频中
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.moviepy_video import MoviePyVideoTools
from agno.tools.openai import OpenAITools

video_tools = MoviePyVideoTools(
    process_video=True, generate_captions=True, embed_captions=True
)

openai_tools = OpenAITools()

video_caption_agent = Agent(
    name="Video Caption Generator Agent",
    model=OpenAIChat(
        id="gpt-4o",
    ),
    tools=[video_tools, openai_tools],
    description="You are an AI agent that can generate and embed captions for videos.",
    instructions=[
        "When a user provides a video, process it to generate captions.",
        "Use the video processing tools in this sequence:",
        "1. Extract audio from the video using extract_audio",
        "2. Transcribe the audio using transcribe_audio",
        "3. Generate SRT captions using create_srt",
        "4. Embed captions into the video using embed_captions",
    ],
    markdown=True,
)


video_caption_agent.print_response(
    "Generate captions for {video with location} and embed them in the video"
)

工具包功能

这些是 MoviePyVideoTools 提供的函数:
FunctionDescription
extract_audio从视频文件中提取音频轨道并将其保存到指定的输出路径。
create_srt将给定的转录(预期为 SRT 格式)保存到指定输出路径的 .srt 文件。
embed_captions将来自 SRT 文件的字幕嵌入到视频中,创建一个带有单词级别高亮的新的视频文件。

工具包参数

这些参数将传递给 MoviePyVideoTools 构造函数:
ParameterTypeDefaultDescription
process_videoboolTrue启用 extract_audio 工具。
generate_captionsboolTrue启用 create_srt 工具。
embed_captionsboolTrue启用 embed_captions 工具。

开发者资源