Chuyển đổi Bản trình bày PowerPoint sang Video trên Android

Giới thiệu

Bằng cách chuyển đổi bản trình bày PowerPoint sang video, bạn sẽ nhận được

  • Tăng khả năng tiếp cận: Tất cả các thiết bị (bất kể nền tảng) đều được cài sẵn trình phát video theo mặc định so với các ứng dụng mở bản trình bày, vì vậy người dùng dễ dàng mở hoặc phát video hơn.
  • Tiếp cận rộng hơn: Thông qua video, bạn có thể tiếp cận lượng lớn khán giả và cung cấp cho họ thông tin mà nếu dùng bản trình bày có thể cảm thấy nhàm chán. Hầu hết các khảo sát và thống kê cho thấy mọi người xem và tiêu thụ video nhiều hơn các dạng nội dung khác, và họ thường ưu tiên nội dung này.

Chuyển đổi PowerPoint sang Video trong Aspose.Slides

Aspose.Slides hỗ trợ chuyển đổi bản trình bày sang video.

  • Sử dụng Aspose.Slides để tạo ra một bộ khung (từ các slide của bản trình bày) tương ứng với một FPS nhất định (khung hình trên giây)
  • Sử dụng công cụ bên thứ ba như ffmpeg (cho java) để tạo video dựa trên các khung hình.

Chuyển đổi PowerPoint sang Video

  1. Thêm đoạn này vào tệp POM của bạn:
   <dependency>
     <groupId>net.bramp.ffmpeg</groupId>
     <artifactId>ffmpeg</artifactId>
     <version>0.7.0</version>
   </dependency>
  1. Tải ffmpeg tại đây.

  2. Chạy mã Java chuyển PowerPoint sang video.

Đoạn mã Java này cho bạn thấy cách chuyển đổi một bản trình bày (gồm một hình và hai hiệu ứng hoạt ảnh) sang video:

Presentation presentation = new Presentation();
try {
    // Thêm một hình cười và sau đó tạo hoạt ảnh cho nó
    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();
    }

    // Cấu hình thư mục chứa các tệp nhị phân ffmpeg. Xem trang này: 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();
}

Hiệu ứng Video

Bạn có thể áp dụng hoạt ảnh cho các đối tượng trên slide và sử dụng chuyển tiếp giữa các slide.

Hoạt ảnh và chuyển tiếp làm cho trình chiếu hấp dẫn và thú vị hơn — và chúng cũng làm tương tự cho video. Hãy thêm một slide và chuyển tiếp nữa vào mã cho bản trình bày trước:

// Thêm một hình cười và tạo hoạt ảnh cho nó

// ...

// Thêm một slide mới và chuyển tiếp có hoạt ảnh

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 cũng hỗ trợ hoạt ảnh cho văn bản. Vì vậy chúng ta sẽ tạo hoạt ảnh cho các đoạn văn trên đối tượng, chúng sẽ xuất hiện lần lượt (với độ trễ đặt là một giây):

Presentation presentation = new Presentation();
try {
    // Thêm văn bản và hoạt ảnh
    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 Passage("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();
    }

    // Cấu hình thư mục chứa các tệp nhị phân ffmpeg. Xem trang này: 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();
}

Các lớp Chuyển đổi Video

Để cho phép bạn thực hiện các tác vụ chuyển đổi PowerPoint sang video, Aspose.Slides cung cấp các lớp PresentationAnimationsGeneratorPresentationPlayer.

PresentationAnimationsGenerator cho phép bạn đặt kích thước khung cho video (sẽ được tạo sau) thông qua constructor của nó. Nếu bạn truyền một thể hiện của bản trình bày, Presentation.SlideSize sẽ được sử dụng và nó tạo ra các hoạt ảnh mà PresentationPlayer sử dụng.

Khi các hoạt ảnh được tạo, một sự kiện NewAnimation được tạo cho mỗi hoạt ảnh tiếp theo, có tham số IPresentationAnimationPlayer. Lớp này đại diện cho một trình phát cho một hoạt ảnh riêng.

Để làm việc với IPresentationAnimationPlayer, thuộc tính Duration (thời gian đầy đủ của hoạt ảnh) và phương thức SetTimePosition được sử dụng. Mỗi vị trí hoạt ảnh được đặt trong khoảng 0 đến duration, và sau đó phương thức GetFrame sẽ trả về một BufferedImage tương ứng với trạng thái hoạt ảnh tại thời điểm đó:

Presentation presentation = new Presentation();
try {
    // Thêm một hình cười và tạo hoạt ảnh cho nó
    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); // trạng thái hoạt ảnh ban đầu
            try {
                // bitmap trạng thái hoạt ảnh ban đầu
                animationPlayer.getFrame().save("firstFrame.png", ImageFormat.Png);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            animationPlayer.setTimePosition(animationPlayer.getDuration()); // trạng thái cuối cùng của hoạt ảnh
            try {
                // khung hình cuối cùng của hoạt ảnh
                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();
}

Để làm cho tất cả các hoạt ảnh trong một bản trình bày chạy đồng thời, lớp PresentationPlayer được sử dụng. Lớp này nhận một thể hiện PresentationAnimationsGenerator và FPS cho các hiệu ứng trong constructor và sau đó gọi sự kiện FrameTick cho tất cả các hoạt ảnh để chúng được phát:

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

Sau đó các khung đã tạo có thể được biên dịch để tạo video. Xem phần Convert PowerPoint to Video.

Các hoạt ảnh và hiệu ứng được hỗ trợ

Entrance:

Loại Hoạt ảnh 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:

Loại Hoạt ảnh 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:

Loại Hoạt ảnh 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:

Loại Hoạt ảnh Aspose.Slides PowerPoint
Lines supported supported
Arcs supported supported
Turns supported supported
Shapes supported supported
Loops supported supported
Custom Path supported supported

Câu hỏi thường gặp

Có thể chuyển đổi các bản trình bày được bảo vệ bằng mật khẩu không?

Có, Aspose.Slides cho phép làm việc với bản trình bày được bảo vệ bằng mật khẩu. Khi xử lý các tệp này, bạn cần cung cấp mật khẩu đúng để thư viện có thể truy cập nội dung của bản trình bày.

Aspose.Slides có hỗ trợ sử dụng trong các giải pháp đám mây không?

Có, Aspose.Slides có thể được tích hợp vào các ứng dụng và dịch vụ đám mây. Thư viện được thiết kế để hoạt động trong môi trường máy chủ, đảm bảo hiệu suất cao và khả năng mở rộng cho việc xử lý hàng loạt các tệp.

Có bất kỳ giới hạn kích thước nào cho bản trình bày khi chuyển đổi không?

Aspose.Slides có khả năng xử lý các bản trình bày gần như bất kỳ kích thước nào. Tuy nhiên, khi làm việc với các tệp rất lớn, có thể cần thêm tài nguyên hệ thống, và đôi khi nên tối ưu hóa bản trình bày để cải thiện hiệu suất.