与 PowerPoint 演示文稿(PPT 或 PPTX)相比,视频 ( MP4) 是一种更流行的文件格式,在绝大多数设备和平台上都更容易打开。因此,当您将 PowerPoint 转换为视频时,您的内容将变得更易于在更多观众中访问和共享。
在C++中将PPT转换为视频
在本文中,我们打算向您展示如何使用 C++ 将 PPT 转换为视频。
获取两个将 PPT 转换为视频的 API
首先,要以编程方式将 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 转换为视频
- 将 ffmpeg.exe 的路径添加到环境变量 PATH 中。
- 运行 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 功能 的更多信息,请参阅我们的文档.如果您有任何问题,可以在我们的论坛 上发帖。