將 PPT 轉換為視頻

與 PowerPoint 演示文稿(PPTPPTX)相比,視頻 ( MP4) 是一種更流行的文件格式,在絕大多數設備和平台上都更容易打開。因此,當您將 PowerPoint 轉換為視頻時,您的內容將變得更易於在更多觀眾中訪問和共享。

在C++中將PPT轉換為視頻

在本文中,我們打算向您展示如何使用 C++ 將 PPT 轉換為視頻。

獲取兩個將 PPT 轉換為視頻的 API

在 PowerPoint 中創建視頻

首先,要以編程方式將 PowerPoint 演示文稿轉換為視頻,您需要獲取這兩個 API:

  • Aspose.Slides for C++. This API allows you to generate a set of frames based on the slides in your PowerPoint presentation. Aspose.Slides for C++ is a popular API for creating, editing, converting, and manipulating PowerPoint presentations (without Microsoft PowerPoint or Office). See this Installation article to install it.
  • ffmpeg。此 API 允許您根據生成的幻燈片創建視頻。 此處 下載。

信息:您可能需要查看 Aspose 免費 PowerPoint 到視頻轉換器,因為它是描述的 PowerPoint 到視頻操作的實時實現在本文中。該轉換器允許用戶從演示文稿中創建令人驚嘆的視頻。

在 C++ 中將 PPT 轉換為視頻

  1. 將 ffmpeg.exe 的路徑添加到環境變量 PATH 中。
  2. 運行 PowerPoint 到視頻 C++ 代碼。

此 C++ 代碼向您展示瞭如何將 PPT 轉換為視頻:

void OnFrameTick(System::SharedPtr<PresentationPlayer> sender, System::SharedPtr<FrameTickEventArgs> args)
{
    System::String fileName = System::String::Format(u"frame_{0}.png", sender->get_FrameIndex());
    args->GetFrame()->Save(fileName);
}

void Run()
{
    auto presentation = System::MakeObject<Presentation>();
    auto slide = presentation->get_Slide(0);

    // 添加微笑形狀,然後對其進行動畫處理
    System::SharedPtr<IAutoShape> smile = slide->get_Shapes()->AddAutoShape(ShapeType::SmileyFace, 110.0f, 20.0f, 500.0f, 500.0f);
    auto sequence = slide->get_Timeline()->get_MainSequence();
    System::SharedPtr<IEffect> effectIn = sequence->AddEffect(smile, EffectType::Fly, EffectSubtype::TopLeft, EffectTriggerType::AfterPrevious);
    System::SharedPtr<IEffect> effectOut = sequence->AddEffect(smile, EffectType::Fly, EffectSubtype::BottomRight, EffectTriggerType::AfterPrevious);
    effectIn->get_Timing()->set_Duration(2.0f);
    effectOut->set_PresetClassType(EffectPresetClassType::Exit);

    const int32_t fps = 33;

    auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
    auto player = System::MakeObject<PresentationPlayer>(animationsGenerator, fps);
    player->FrameTick += OnFrameTick;
    animationsGenerator->Run(presentation->get_Slides());

    const System::String ffmpegParameters = System::String::Format(
        u"-loglevel {0} -framerate {1} -i {2} -y -c:v {3} -pix_fmt {4} {5}",
        u"warning", m_fps, "frame_%d.png", u"libx264", u"yuv420p", "video.mp4");
    auto ffmpegProcess = System::Diagnostics::Process::Start(u"ffmpeg", ffmpegParameters);
    ffmpegProcess->WaitForExit();
}

在視頻中應用效果和動畫

動畫和效果提高了演示文稿和幻燈片的吸引力和全神貫注率。它們對視頻具有相同的效果,尤其是從 PowerPoint 演示文稿創建的視頻,因此您可能希望在視頻中使用效果。 Aspose.Slides 允許您通過提供 PresentationAnimationsGenerator、[PresentationPlayer](https:// reference.aspose.com/slides/cpp/class/aspose.slides.export.presentationplayer/),以及其他相關類和類型。

例如,如果您決定在視頻中使用動畫和效果,您可以通過以下方式向演示代碼添加另一張幻燈片和過渡:

// 添加微笑形狀並為其設置動畫

// ...

// 添加新的幻燈片和動畫過渡

System::SharedPtr<ISlide> newSlide = presentation->get_Slides()->AddEmptySlide(presentation->get_Slide(0)->get_LayoutSlide());

System::SharedPtr<IBackground> slideBackground = newSlide->get_Background();

slideBackground->set_Type(BackgroundType::OwnBackground);

auto fillFormat = slideBackground->get_FillFormat();

fillFormat->set_FillType(FillType::Solid);

fillFormat->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Indigo());

newSlide->get_SlideShowTransition()->set_Type(TransitionType::Push);

然後您可以將動畫設置應用於對像上的段落,使這些對像一個接一個地出現(出現之間的延遲設置為一秒):

void OnFrameTick(System::SharedPtr<PresentationPlayer> sender, System::SharedPtr<FrameTickEventArgs> args)
{
    System::String fileName = System::String::Format(u"frame_{0}.png", sender->get_FrameIndex());
    args->GetFrame()->Save(fileName);
}

void Run()
{
    auto presentation = System::MakeObject<Presentation>();
    auto slide = presentation->get_Slide(0);

    // 添加文本和動畫
    System::SharedPtr<IAutoShape> autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 210.0f, 120.0f, 300.0f, 300.0f);
    System::SharedPtr<Paragraph> para1 = System::MakeObject<Paragraph>();
    para1->get_Portions()->Add(System::MakeObject<Portion>(u"Aspose Slides for C++"));
    System::SharedPtr<Paragraph> para2 = System::MakeObject<Paragraph>();
    para2->get_Portions()->Add(System::MakeObject<Portion>(u"convert PowerPoint Presentation with text to video"));

    System::SharedPtr<Paragraph> para3 = System::MakeObject<Paragraph>();
    para3->get_Portions()->Add(System::MakeObject<Portion>(u"paragraph by paragraph"));
    auto paragraphs = autoShape->get_TextFrame()->get_Paragraphs();
    paragraphs->Add(para1);
    paragraphs->Add(para2);
    paragraphs->Add(para3);
    paragraphs->Add(System::MakeObject<Paragraph>());

    auto sequence = slide->get_Timeline()->get_MainSequence();
    System::SharedPtr<IEffect> effect = sequence->AddEffect(para1, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    System::SharedPtr<IEffect> effect2 = sequence->AddEffect(para2, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    System::SharedPtr<IEffect> effect3 = sequence->AddEffect(para3, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    System::SharedPtr<IEffect> effect4 = sequence->AddEffect(para3, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    effect->get_Timing()->set_TriggerDelayTime(1.0f);
    effect2->get_Timing()->set_TriggerDelayTime(1.0f);
    effect3->get_Timing()->set_TriggerDelayTime(1.0f);
    effect4->get_Timing()->set_TriggerDelayTime(1.0f);

    // 將幀轉換為視頻
    const int32_t fps = 33;

    auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
    auto player = System::MakeObject<PresentationPlayer>(animationsGenerator, fps);
    
    player->FrameTick += OnFrameTick;
    animationsGenerator->Run(presentation->get_Slides());

    const System::String ffmpegParameters = System::String::Format(
        u"-loglevel {0} -framerate {1} -i {2} -y -c:v {3} -pix_fmt {4} {5}",
        u"warning", m_fps, "frame_%d.png", u"libx264", u"yuv420p", "video.mp4");
    auto ffmpegProcess = System::Diagnostics::Process::Start(u"ffmpeg", ffmpegParameters);
    ffmpegProcess->WaitForExit();
}

獲得免費許可證

如果您想不受限制地嘗試 Aspose.Slides 的功能,我們建議您獲得一個免費的臨時許可證

結論

完成本文中的操作後,我們相信您現在知道如何在 C++ 中將 PPT 轉換為視頻

要了解有關 Aspose.Slides 功能 的更多信息,請參閱我們的文檔.如果您有任何問題,可以在我們的論壇 上發帖。

也可以看看