Chuyển đổi PPT thành video trong Java

API Java đa nền tảng mạnh mẽ để chuyển đổi PowerPoint sang video bằng mã Java

Chuyển đổi PowerPoint sang video bằng Aspose.Slides

Aspose.Slides for Java là một thư viện Java mạnh mẽ được sử dụng để tạo, chỉnh sửa và thao tác với bản trình bày, đồng thời chuyển đổi bản trình bày PowerPoint sang các tài liệu và video khác . Trong trường hợp này, để chuyển đổi PowerPoint thành video, bạn cần sử dụng Aspose.Slides cùng với ffmpeg.

Đây là cách hoạt động của quá trình chuyển đổi PPT sang video: Aspose.Slides được sử dụng để tạo một tập hợp các khung (từ các trang trình bày) và sau đó FFMpegCore (ffmpeg) được sử dụng để tạo video dựa trên các khung.

Cách chuyển đổi PPT sang video

  1. Cài đặt Aspose.Slides cho Java bằng cách làm theo hướng dẫn tại đây . Tải xuống ffmpeg tại đây.

  2. Thêm phần này vào tệp POM của bạn:

       <phụ thuộc>
         <groupId>net.bramp.ffmpeg</groupId>
         <artifactId>ffmpeg</artifactId>
         <version>0.7.0</version>
       </phụ thuộc>
    

  3. Sao chép, dán, rồi chạy mã Java PowerPoint sang video.

Chuyển đổi PowerPoint sang video trong Java

Sử dụng mã Java này để chuyển đổi PPT thành video:

Mã Java để chuyển đổi PowerPoint thành video


Presentation presentation = new Presentation();
try {
    // Adds a smile shape and then animates it
    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();
    }

    // Configure ffmpeg binaries folder. See this page: 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 chuyển đổi được hỗ trợ khác

Bạn cũng có thể chuyển đổi PowerPoint sang các tệp ở các định dạng khác