在 Android 上將 PowerPoint 簡報轉換為影片

簡介

透過將 PowerPoint 簡報轉換為影片,您可獲得

  • 提升可近性: 所有裝置(不論平台)預設皆內建影片播放器,相較於需要開啟簡報的應用程式,使用者更容易開啟或播放影片。
  • 更廣的受眾: 透過影片,您可觸及大量觀眾,並向他們傳遞在簡報中可能顯得冗長的資訊。大多數調查與統計顯示,人們觀看與消費影片的比例高於其他內容形式,且普遍偏好此類內容。

Aspose.Slides 中的 PowerPoint 轉影片轉換

Aspose.Slides 支援簡報轉影片的轉換。

  • 使用 Aspose.Slides 產生一組畫格(取自簡報投影片),其對應特定的 FPS(每秒畫格數)。
  • 使用第三方工具如 ffmpegfor java)根據這些畫格建立影片。

將 PowerPoint 轉為影片

  1. 將以下內容加入您的 POM 檔案:
   <dependency>
     <groupId>net.bramp.ffmpeg</groupId>
     <artifactId>ffmpeg</artifactId>
     <version>0.7.0</version>
   </dependency>
  1. 在此下載 ffmpeghere

  2. 執行 PowerPoint 轉影片的 Java 程式碼。

以下 Java 程式碼示範如何將包含圖形與兩個動畫效果的簡報轉換為影片:

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());
                    arguments.getFrame().save(frame, ImageFormat.Png);
                    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();
}

影片效果

您可以對投影片上的物件套用動畫,並在投影片之間使用轉場效果。

動畫與轉場讓投影片放映更具吸引力與趣味性——對影片同樣適用。讓我們為前述簡報的程式碼加入另一張投影片與轉場:

// 新增一個笑臉形狀並為其設定動畫

// ...

// 新增一個投影片並設定動畫轉場

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);

Aspose.Slides 亦支援文字動畫。此範例對物件上的段落進行動畫,使其依序出現(延遲設為一秒):

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());
                    arguments.getFrame().save(frame, ImageFormat.Png);
                    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 轉影片的任務,Aspose.Slides 提供 PresentationAnimationsGeneratorPresentationPlayer 類別。

PresentationAnimationsGenerator 允許您透過建構函式設定稍後將建立之影片的畫格大小。若傳入簡報實例,將使用 Presentation.SlideSize,並產生供 PresentationPlayer 使用的動畫。

產生動畫時,會為每個後續動畫產生 NewAnimation 事件,該事件帶有 IPresentationAnimationPlayer 參數。此類別代表單一動畫的播放器。

使用 IPresentationAnimationPlayer 時,會使用 Duration(動畫的完整持續時間)屬性與 SetTimePosition 方法。每個動畫位置設定於 0 到 duration 的範圍內,然後 GetFrame 方法會回傳對應於該時刻動畫狀態的 BufferedImage:

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);

    PresentationAnimationsGenerator animationsGenerator = new PresentationAnimationsGenerator(presentation);
    try {
        animationsGenerator.setNewAnimation(animationPlayer ->
        {
            System.out.println(String.format("Animation total duration: %f", animationPlayer.getDuration()));
            animationPlayer.setTimePosition(0); // 初始動畫狀態
            try {
                // 初始動畫狀態之位圖
                animationPlayer.getFrame().save("firstFrame.png", ImageFormat.Png);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            animationPlayer.setTimePosition(animationPlayer.getDuration()); // 動畫的最終狀態
            try {
                // 動畫的最後一幀
                animationPlayer.getFrame().save("lastFrame.png", ImageFormat.Png);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    } finally {
        if (animationsGenerator != null) animationsGenerator.dispose();
    }
} finally {
    if (presentation != null) presentation.dispose();
}

若要讓簡報中的所有動畫一次性播放,使用 PresentationPlayer 類別。此類別在建構函式中接受一個 PresentationAnimationsGenerator 實例與特效的 FPS,然後對所有動畫呼叫 FrameTick 事件以啟動播放:

Presentation presentation = new Presentation("animated.pptx");
try {
    PresentationAnimationsGenerator animationsGenerator = new PresentationAnimationsGenerator(presentation);
    try {
        PresentationPlayer player = new PresentationPlayer(animationsGenerator, 33);
        try {
            player.setFrameTick((sender, arguments) ->
            {
                try {
                    arguments.getFrame().save("frame_" + sender.getFrameIndex() + ".png", ImageFormat.Png);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
            animationsGenerator.run(presentation.getSlides());
        } finally {
            if (player != null) player.dispose();
        }
    } finally {
        if (animationsGenerator != null) animationsGenerator.dispose();
    }
} finally {
    if (presentation != null) presentation.dispose();
}

接著,將產生的畫格編譯即可製作影片。請參閱Convert PowerPoint to Video 章节。

支援的動畫與效果

Entrance:

動畫類型 Aspose.Slides PowerPoint
Appear not supported supported
Fade supported supported
Fly In supported supported
Float In supported supported
Split supported supported
Wipe supported supported
Shape supported supported
Wheel supported supported
Random Bars supported supported
Grow & Turn not supported supported
Zoom supported supported
Swivel supported supported
Bounce supported supported

Emphasis:

動畫類型 Aspose.Slides PowerPoint
Pulse not supported supported
Color Pulse not supported supported
Teeter supported supported
Spin supported supported
Grow/Shrink not supported supported
Desaturate not supported supported
Darken not supported supported
Lighten not supported supported
Transparency not supported supported
Object Color not supported supported
Complementary Color not supported supported
Line Color not supported supported
Fill Color not supported supported

Exit:

動畫類型 Aspose.Slides PowerPoint
Disappear not supported supported
Fade supported supported
Fly Out supported supported
Float Out supported supported
Split supported supported
Wipe supported supported
Shape supported supported
Random Bars supported supported
Shrink & Turn not supported supported
Zoom supported supported
Swivel supported supported
Bounce supported supported

Motion Paths:

動畫類型 Aspose.Slides PowerPoint
Lines supported supported
Arcs supported supported
Turns supported supported
Shapes supported supported
Loops supported supported
Custom Path supported supported

常見問題

是否可以轉換受密碼保護的簡報?

是的,Aspose.Slides 支援處理受密碼保護的簡報。在處理此類檔案時,您需要提供正確的密碼,才能讓程式庫存取簡報內容。

Aspose.Slides 是否支援在雲端解決方案中使用?

是的,Aspose.Slides 可整合至雲端應用與服務。此程式庫設計為在伺服器環境中運行,確保批次檔案處理的高效能與可擴充性。

在轉換過程中,簡報的大小是否有限制?

Aspose.Slides 能處理幾乎任何大小的簡報。然而,若處理極大檔案,可能需要額外的系統資源,且建議適度最佳化簡報以提升效能。