将PowerPoint转换为视频
通过将PowerPoint演示文稿转换为视频,您可以获得
- 提高可访问性: 与需要打开演示文稿的应用程序相比,所有设备(无论平台)默认都配备有视频播放器,因此用户更容易打开或播放视频。
- 更广泛的受众: 通过视频,您可以接触到更大的人群,并用可能在演示中看起来无聊的信息进行目标定位。大多数调查和统计数据显示,人们观看和消费视频的频率高于其他形式的内容,他们通常更喜欢这种内容。
Aspose.Slides中的PowerPoint到视频转换
在Aspose.Slides 22.11中,我们实现了演示文稿转换为视频的支持。
- 使用Aspose.Slides生成一组与特定FPS(每秒帧数)对应的帧(来自演示文稿幻灯片)。
- 使用像
ffmpeg
这样的第三方实用程序根据这些帧创建视频。
将PowerPoint转换为视频
- 在这里下载ffmpeg。
- 将
ffmpeg.exe
的路径添加到环境变量PATH
中。 - 运行PowerPoint到视频的代码。
以下C++代码展示了如何将包含图形和两个动画效果的演示文稿转换为视频:
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();
}
视频效果
您可以对幻灯片上的对象应用动画,并在幻灯片之间使用过渡效果。
动画和过渡使幻灯片演示更具吸引力和趣味性——它们在视频中也发挥着同样的作用。让我们为之前的演示添加另一张幻灯片和过渡:
// 添加笑脸形状并对其进行动画处理
// ...
// 添加新幻灯片和动画过渡
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);
Aspose.Slides还支持文本动画。因此我们将段落进行动画处理,使其逐个出现(设置延迟为一秒):
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"带文本的PowerPoint演示文稿转换为视频"));
System::SharedPtr<Paragraph> para3 = System::MakeObject<Paragraph>();
para3->get_Portions()->Add(System::MakeObject<Portion>(u"逐段出现"));
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();
}
视频转换类
为了让您能够执行PowerPoint到视频转换任务,Aspose.Slides提供了PresentationAnimationsGenerator和PresentationPlayer类。
PresentationAnimationsGenerator允许您通过其构造函数设置视频的帧大小(随后将创建)。如果您传递一个演示文稿实例,则将使用Presentation.SlideSize
,并生成PresentationPlayer使用的动画。
当动画生成时,会为每个后续动画生成一个NewAnimation
事件,其中具有IPresentationAnimationPlayer参数。后者是表示单独动画播放器的类。
要与IPresentationAnimationPlayer一起工作,使用get_Duration(动画的总持续时间)属性和SetTimePosition方法。每个动画位置在0到持续时间范围内设置,然后GetFrame
方法将返回与该时刻动画状态相对应的Bitmap。
void OnNewAnimation(System::SharedPtr<IPresentationAnimationPlayer> animationPlayer)
{
System::Console::WriteLine(u"总动画时长:{0}", animationPlayer->get_Duration());
animationPlayer->SetTimePosition(0);
// 初始动画状态
System::SharedPtr<System::Drawing::Bitmap> bitmap = animationPlayer->GetFrame();
// 初始动画状态位图
animationPlayer->SetTimePosition(animationPlayer->get_Duration());
// 动画的最终状态
System::SharedPtr<System::Drawing::Bitmap> lastBitmap = animationPlayer->GetFrame();
// 动画的最后一帧
lastBitmap->Save(u"last.png");
}
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);
auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
animationsGenerator->NewAnimation += OnNewAnimation;
}
要使演示文稿中的所有动画同时播放,使用PresentationPlayer类。该类在其构造函数中采用PresentationAnimationsGenerator实例和效果的FPS,然后调用FrameTick
事件以使所有动画播放:
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>(u"animated.pptx");
auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
auto player = System::MakeObject<PresentationPlayer>(animationsGenerator, 33);
player->FrameTick += OnFrameTick;
animationsGenerator->Run(presentation->get_Slides());
}
然后可以将生成的帧编译以产生视频。请参见将PowerPoint转换为视频部分。
支持的动画和效果
进入:
动画类型 | Aspose.Slides | PowerPoint |
---|---|---|
出现 | ||
淡入 | ||
飞入 | ||
漂浮进入 | ||
分裂 | ||
擦除 | ||
形状 | ||
轮子 | ||
随机条 | ||
增长与转动 | ||
缩放 | ||
旋转 | ||
弹跳 |
强调:
动画类型 | Aspose.Slides | PowerPoint |
---|---|---|
脉动 | ||
颜色脉动 | ||
摇摆 | ||
旋转 | ||
增大/缩小 | ||
去饱和 | ||
变暗 | ||
变亮 | ||
透明度 | ||
对象颜色 | ||
互补颜色 | ||
线条颜色 | ||
填充颜色 |
退出:
动画类型 | Aspose.Slides | PowerPoint |
---|---|---|
消失 | ||
淡出 | ||
飞出 | ||
漂浮退出 | ||
分裂 | ||
擦除 | ||
形状 | ||
随机条 | ||
缩小与转动 | ||
缩放 | ||
旋转 | ||
弹跳 |
运动路径:
动画类型 | Aspose.Slides | PowerPoint |
---|---|---|
线条 | ||
弧线 | ||
转弯 | ||
形状 | ||
循环 | ||
自定义路径 |