Chuyển đổi bài thuyết trình PowerPoint sang video trong Java

Giới thiệu

Bằng cách chuyển đổi bài thuyết trình PowerPoint hoặc OpenDocument của bạn sang video, bạn sẽ đượ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 trang bị trình phát video mặc định, giúp người dùng mở hoặc phát video dễ dàng hơn so với các ứng dụng trình chiếu truyền thống.

Phạm vi tiếp cận rộng hơn: Video cho phép bạn tiếp cận đối tượng lớn hơn và trình bày thông tin theo dạng hấp dẫn hơn. Các khảo sát và thống kê cho thấy mọi người thích xem và tiêu thụ nội dung video hơn so với các hình thức khác, làm cho thông điệp của bạn có tác động mạnh mẽ hơn.

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

Trong Aspose.Slides 22.11, chúng tôi đã triển khai hỗ trợ chuyển đổi bài thuyết trình sang video.

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

Chuyển đổi PowerPoint sang Video

  1. Add this to your POM file:
   <dependency>
     <groupId>net.bramp.ffmpeg</groupId>
     <artifactId>ffmpeg</artifactId>
     <version>0.7.0</version>
   </dependency>
  1. Tải ffmpeg ở đây.

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

Mã Java này cho bạn thấy cách chuyển đổi một bài thuyết trình (chứa 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 mặt cười và sau đó áp dụng hoạt ảnh
    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 các 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.

Các hoạt ảnh và chuyển tiếp làm cho slide trở nên hấp dẫn và thú vị hơn—và chúng cũng tạo ra hiệu quả tương tự cho video. Hãy thêm một slide và chuyển tiếp khác vào mã cho bài thuyết trình trước:

// Thêm một hình mặt cười và áp dụng hoạt ảnh

// ...

// Thêm một slide mới và chuyển tiếp 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 tôi sẽ thực hiện 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ễ được đặ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 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();
    }

    // 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 nhiệm vụ chuyển đổi PowerPoint sang video, Aspose.Slides cung cấp các lớp PresentationAnimationsGeneratorPresentationPlayer.

PresentationAnimationsGenerator cho phép bạn thiết lập kích thước khung cho video (sẽ được tạo sau) thông qua hàm khởi tạo. Nếu bạn truyền một thể hiện của bài thuyết trình, 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 sinh ra cho mỗi hoạt ảnh tiếp theo, có tham số IPresentationAnimationPlayer. Tham số này là một lớp đại diện cho trình phát hoạt ảnh riêng biệt.

Để làm việc với IPresentationAnimationPlayer, thuộc tính Duration (thời lượng đầ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, 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 mặt cười và áp dụng hoạt ảnh
    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 {
                // ảnh bitmap của 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 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ài thuyết trình chơi đồng thời, lớp PresentationPlayer được sử dụng. Lớp này nhận một thể hiện của PresentationAnimationsGenerator và FPS cho hiệu ứng trong hàm khởi tạo, 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 hình đã tạo có thể được biên dịch để tạo thành video. Xem phần Convert PowerPoint to Video.

Các Hoạt Ảnh và Hiệu Ứng Được Hỗ Trợ

Mở đầu:

Loại Hoạt Ảnh Aspose.Slides PowerPoint
Hiện không hỗ trợ được hỗ trợ
Mờ dần được hỗ trợ được hỗ trợ
Bay vào được hỗ trợ được hỗ trợ
Nổi lên được hỗ trợ được hỗ trợ
Tách được hỗ trợ được hỗ trợ
Xoá được hỗ trợ được hỗ trợ
Hình dạng được hỗ trợ được hỗ trợ
Bánh xe được hỗ trợ được hỗ trợ
Thanh ngẫu nhiên được hỗ trợ được hỗ trợ
Phát triển & Xoay không hỗ trợ được hỗ trợ
Phóng to được hỗ trợ được hỗ trợ
Quay quanh được hỗ trợ được hỗ trợ
Nảy được hỗ trợ được hỗ trợ

Nhấn mạnh:

Loại Hoạt Ảnh Aspose.Slides PowerPoint
Xung không hỗ trợ được hỗ trợ
Xung màu không hỗ trợ được hỗ trợ
Lắc được hỗ trợ được hỗ trợ
Quay được hỗ trợ được hỗ trợ
Mở rộng/Thu nhỏ không hỗ trợ được hỗ trợ
Giảm bão hòa không hỗ trợ được hỗ trợ
Làm tối không hỗ trợ được hỗ trợ
Làm sáng không hỗ trợ được hỗ trợ
Trong suốt không hỗ trợ được hỗ trợ
Màu đối tượng không hỗ trợ được hỗ trợ
Màu bổ sung không hỗ trợ được hỗ trợ
Màu đường không hỗ trợ được hỗ trợ
Màu tô không hỗ trợ được hỗ trợ

Kết thúc:

Loại Hoạt Ảnh Aspose.Slides PowerPoint
Biến mất không hỗ trợ được hỗ trợ
Mờ dần được hỗ trợ được hỗ trợ
Bay ra được hỗ trợ được hỗ trợ
Nổi ra được hỗ trợ được hỗ trợ
Tách được hỗ trợ được hỗ trợ
Xoá được hỗ trợ được hỗ trợ
Hình dạng được hỗ trợ được hỗ trợ
Thanh ngẫu nhiên được hỗ trợ được hỗ trợ
Thu nhỏ & Xoay không hỗ trợ được hỗ trợ
Phóng to được hỗ trợ được hỗ trợ
Quay quanh được hỗ trợ được hỗ trợ
Nảy được hỗ trợ được hỗ trợ

Đường chuyển động:

Loại Hoạt Ảnh Aspose.Slides PowerPoint
Đường thẳng được hỗ trợ được hỗ trợ
Cung được hỗ trợ được hỗ trợ
Xoắn được hỗ trợ được hỗ trợ
Hình dạng được hỗ trợ được hỗ trợ
Vòng lặp được hỗ trợ được hỗ trợ
Đường tùy chỉnh được hỗ trợ được hỗ trợ

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

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

Có, Aspose.Slides cho phép làm việc với password-protected presentations. Khi xử lý các tệp như vậ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ài thuyết trình.

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 năng cao và khả năng mở rộng cho việc xử lý hàng loạt các tệp.

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

Aspose.Slides có khả năng xử lý các bài thuyết trình có kích thước gần như bất kỳ. 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ài thuyết trình để cải thiện hiệu suất.