
当您将 PowerPoint 转换为视频时,您的演示文稿或内容将获得更高的可访问性和更多的受众。 MP4 视频格式是一种非常流行的文件格式,因此与 PPT 相比,更多人会发现打开或播放您的视频更容易://docs.fileformat.com/presentation/ppt/) 文件。此外,与其他形式的内容相比,大多数人观看和消费视频的次数更多,因此与演示文稿相比,您的视频可能会吸引更多的观众。
用 Java 将 PowerPoint 转为视频
读完本文后,您将了解如何使用 Java 将 PowerPoint 转换为视频。
将 PPT 转换为视频的 Java API

要以编程方式将 PowerPoint 演示文稿转换为视频,您需要:
- 从演示幻灯片生成一组帧的 API。我们推荐 Aspose.Slides for Java。 Aspose.Slides for Java 是一种流行的 API,用于创建、编辑、转换和操作 PowerPoint 演示文稿(无需 Microsoft PowerPoint 或 Office)。要安装 Aspose.Slides for Java,请参阅安装。
- 另一个 API 用于根据生成的帧创建视频。我们推荐 ffmpeg(用于 Java)。
信息:Aspose 开发了一个免费的 PowerPoint 到视频转换器,允许您从演示文稿创建令人惊叹的视频。该转换器本质上是 PowerPoint 到视频转换过程的实时实现。
在 Java 中将 PPT 转换为视频
- 将此添加到您的 POM 文件中:
<dependency>
<groupId>net.bramp.ffmpeg</groupId>
<artifactId>ffmpeg</artifactId>
<version>0.7.0</version>
</dependency>
此处 下载 ffmpeg。
运行 PowerPoint 到视频 Java 代码。
此 Java 代码向您展示了如何将 PPT 转换为视频:
Presentation presentation = new Presentation();
try {
// 添加微笑形状,然后对其进行动画处理
IAutoShape smile = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.SmileyFace, 110, 20, 500, 500);
ISequence mainSequence = presentation.getSlides().get_Item(0).getTimeline().getMainSequence();
IEffect effectIn = mainSequence.addEffect(smile, EffectType.Fly, EffectSubtype.TopLeft, EffectTriggerType.AfterPrevious);
IEffect effectOut = mainSequence.addEffect(smile, EffectType.Fly, EffectSubtype.BottomRight, EffectTriggerType.AfterPrevious);
effectIn.getTiming().setDuration(2f);
effectOut.setPresetClassType(EffectPresetClassType.Exit);
final int fps = 33;
ArrayList<String> frames = new ArrayList<String>();
PresentationAnimationsGenerator animationsGenerator = new PresentationAnimationsGenerator(presentation);
try
{
PresentationPlayer player = new PresentationPlayer(animationsGenerator, fps);
try {
player.setFrameTick((sender, arguments) ->
{
try {
String frame = String.format("frame_%04d.png", sender.getFrameIndex());
ImageIO.write(arguments.getFrame(), "PNG", new java.io.File(frame));
frames.add(frame);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
animationsGenerator.run(presentation.getSlides());
} finally {
if (player != null) player.dispose();
}
} finally {
if (animationsGenerator != null) animationsGenerator.dispose();
}
// 配置 ffmpeg 二进制文件文件夹。请参阅此页面:https://github.com/rosenbjerg/FFMpegCore#installation
FFmpeg ffmpeg = new FFmpeg("path/to/ffmpeg");
FFprobe ffprobe = new FFprobe("path/to/ffprobe");
FFmpegBuilder builder = new FFmpegBuilder()
.addExtraArgs("-start_number", "1")
.setInput("frame_%04d.png")
.addOutput("output.avi")
.setVideoFrameRate(FFmpeg.FPS_24)
.setFormat("avi")
.done();
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
executor.createJob(builder).run();
} catch (IOException e) {
e.printStackTrace();
}
在视频中应用效果和动画
PowerPoint 允许您将动画应用于演示文稿的内容,还可以在幻灯片之间使用过渡。这些效果使演示文稿(尤其是幻灯片形式)更具吸引力和趣味性。当您将 PowerPoint 演示文稿转换为视频时,在生成的视频中使用类似的效果是有意义的,而 Aspose.Slides 允许您精确地做到这一点。
为了演示视频中效果和动画的使用,让我们以这种方式在上一节的演示代码中添加另一张幻灯片和过渡:
// 添加微笑形状并为其设置动画
// ...
// 添加新的幻灯片和动画过渡
ISlide newSlide = presentation.getSlides().addEmptySlide(presentation.getSlides().get_Item(0).getLayoutSlide());
newSlide.getBackground().setType(BackgroundType.OwnBackground);
newSlide.getBackground().getFillFormat().setFillType(FillType.Solid);
newSlide.getBackground().getFillFormat().getSolidFillColor().setColor(Color.MAGENTA);
newSlide.getSlideShowTransition().setType(TransitionType.Push);
然后我们为对象上的段落设置动画,使这些对象一个接一个地出现(出现之间的延迟设置为一秒):
Presentation presentation = new Presentation();
try {
// 添加文本和动画
IAutoShape autoShape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 210, 120, 300, 300);
Paragraph para1 = new Paragraph();
para1.getPortions().add(new Portion("Aspose Slides for Java"));
Paragraph para2 = new Paragraph();
para2.getPortions().add(new Portion("convert PowerPoint Presentation with text to video"));
Paragraph para3 = new Paragraph();
para3.getPortions().add(new Portion("paragraph by paragraph"));
IParagraphCollection paragraphCollection = autoShape.getTextFrame().getParagraphs();
paragraphCollection.add(para1);
paragraphCollection.add(para2);
paragraphCollection.add(para3);
paragraphCollection.add(new Paragraph());
ISequence mainSequence = presentation.getSlides().get_Item(0).getTimeline().getMainSequence();
IEffect effect1 = mainSequence.addEffect(para1, EffectType.Appear, EffectSubtype.None, EffectTriggerType.AfterPrevious);
IEffect effect2 = mainSequence.addEffect(para2, EffectType.Appear, EffectSubtype.None, EffectTriggerType.AfterPrevious);
IEffect effect3 = mainSequence.addEffect(para3, EffectType.Appear, EffectSubtype.None, EffectTriggerType.AfterPrevious);
IEffect effect4 = mainSequence.addEffect(para3, EffectType.Appear, EffectSubtype.None, EffectTriggerType.AfterPrevious);
effect1.getTiming().setTriggerDelayTime(1f);
effect2.getTiming().setTriggerDelayTime(1f);
effect3.getTiming().setTriggerDelayTime(1f);
effect4.getTiming().setTriggerDelayTime(1f);
final int fps = 33;
ArrayList<String> frames = new ArrayList<String>();
PresentationAnimationsGenerator animationsGenerator = new PresentationAnimationsGenerator(presentation);
try
{
PresentationPlayer player = new PresentationPlayer(animationsGenerator, fps);
try {
player.setFrameTick((sender, arguments) ->
{
try {
String frame = String.format("frame_%04d.png", sender.getFrameIndex());
ImageIO.write(arguments.getFrame(), "PNG", new java.io.File(frame));
frames.add(frame);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
animationsGenerator.run(presentation.getSlides());
} finally {
if (player != null) player.dispose();
}
} finally {
if (animationsGenerator != null) animationsGenerator.dispose();
}
// 配置 ffmpeg 二进制文件文件夹。请参阅此页面:https://github.com/rosenbjerg/FFMpegCore#installation
FFmpeg ffmpeg = new FFmpeg("path/to/ffmpeg");
FFprobe ffprobe = new FFprobe("path/to/ffprobe");
FFmpegBuilder builder = new FFmpegBuilder()
.addExtraArgs("-start_number", "1")
.setInput("frame_%04d.png")
.addOutput("output.avi")
.setVideoFrameRate(FFmpeg.FPS_24)
.setFormat("avi")
.done();
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
executor.createJob(builder).run();
} catch (IOException e) {
e.printStackTrace();
}
获得免费许可证
如果您想不受限制地尝试 Aspose.Slides 的功能,我们建议您获得一个免费的临时许可证。
结论
我们相信您现在知道如何将 PPT 转换为视频,以及如何在您的作品中应用动画、过渡和其他效果。
要了解有关 Aspose.Slides 功能 的更多信息,请参阅我们的文档.如果您有任何问题,可以在我们的论坛 上发帖。