Chuyển đổi bản trình chiếu PowerPoint sang video bằng JavaScript
Giới thiệu
Bằng cách chuyển đổi bản trình chiếu PowerPoint của bạn sang video, bạn sẽ có
- Tăng khả năng truy cập: Tất cả các thiết bị (bất kể nền tảng) đều được trang bị sẵn các trình phát video so với các ứng dụng mở bản trình chiếu, 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 một lượng lớn khán giả và truyền tải thông tin mà nếu dùng bản trình chiếu có thể sẽ gâ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 loại 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 chiếu sang video.
- Sử dụng Aspose.Slides để tạo một tập hợp các khung hình (từ các slide của bản trình chiếu) tương ứng với một FPS nhất định (khung hình mỗi giây)
- Sử dụng công cụ 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
-
Tải ffmpeg tại đây.
-
Chạy mã JavaScript chuyển PowerPoint sang video.
Mã JavaScript này cho bạn thấy cách chuyển đổi một bản trình chiếu (chứa một hình và hai hiệu ứng hoạt hình) sang video:
var presentation = new aspose.slides.Presentation();
try {
// Thêm một hình mặt cười và sau đó tạo hoạt hình cho nó
var smile = presentation.getSlides().get_Item(0).getShapes().addAutoShape(aspose.slides.ShapeType.SmileyFace, 110, 20, 500, 500);
var mainSequence = presentation.getSlides().get_Item(0).getTimeline().getMainSequence();
var effectIn = mainSequence.addEffect(smile, aspose.slides.EffectType.Fly, aspose.slides.EffectSubtype.TopLeft, aspose.slides.EffectTriggerType.AfterPrevious);
var effectOut = mainSequence.addEffect(smile, aspose.slides.EffectType.Fly, aspose.slides.EffectSubtype.BottomRight, aspose.slides.EffectTriggerType.AfterPrevious);
effectIn.getTiming().setDuration(2.0);
effectOut.setPresetClassType(aspose.slides.EffectPresetClassType.Exit);
final var fps = 33;
var frames = java.newInstanceSync("java.util.ArrayList");
var animationsGenerator = new aspose.slides.PresentationAnimationsGenerator(presentation);
try {
var player = new aspose.slides.PresentationPlayer(animationsGenerator, fps);
try {
player.setFrameTick((sender, arguments) -> {
try {
var frame = java.callStaticMethodSync("java.lang.String", "format", "frame_%04d.png", sender.getFrameIndex());
arguments.getFrame().save(frame, aspose.slides.ImageFormat.Png);
frames.add(frame);
} catch (e) {console.log(e);
throw java.newInstanceSync("java.lang.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
var ffmpeg = java.newInstanceSync("FFmpeg", "path/to/ffmpeg");
var ffprobe = java.newInstanceSync("FFprobe", "path/to/ffprobe");
var builder = java.newInstanceSync("FFmpegBuilder").addExtraArgs("-start_number", "1").setInput("frame_%04d.png").addOutput("output.avi").setVideoFrameRate(java.getStaticFieldValue("FFmpeg", "FPS_24")).setFormat("avi").done();
var executor = java.newInstanceSync("FFmpegExecutor", ffmpeg, ffprobe);
executor.createJob(builder).run();
} catch (e) {console.log(e);
console.log(e);
}
Hiệu ứng Video
Bạn có thể áp dụng hoạt hì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 hình và chuyển tiếp làm cho bài thuyết trình trở nên sinh động và thú vị—và chúng cũng làm điều tương tự cho video. Hãy thêm một slide và chuyển tiếp khác vào mã cho bản trình chiếu trước:
// Thêm một hình mặt cười và tạo hoạt hình cho nó
// ...
// Thêm một slide mới và chuyển tiếp hoạt hình
var newSlide = presentation.getSlides().addEmptySlide(presentation.getSlides().get_Item(0).getLayoutSlide());
newSlide.getBackground().setType(aspose.slides.BackgroundType.OwnBackground);
newSlide.getBackground().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
newSlide.getBackground().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "MAGENTA"));
newSlide.getSlideShowTransition().setType(aspose.slides.TransitionType.Push);
Aspose.Slides cũng hỗ trợ hoạt hình cho văn bản. Vì vậy chúng tôi hoạt hình các đoạn văn trên các đối tượng, chúng sẽ xuất hiện lần lượt (với độ trễ được đặt là một giây):
var presentation = new aspose.slides.Presentation();
try {
// Thêm văn bản và hoạt ảnh
var autoShape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 210, 120, 300, 300);
var para1 = new aspose.slides.Paragraph();
para1.getPortions().add(new aspose.slides.Portion("Aspose Slides for Node.js via Java"));
var para2 = new aspose.slides.Paragraph();
para2.getPortions().add(new aspose.slides.Portion("convert PowerPoint Presentation with text to video"));
var para3 = new aspose.slides.Paragraph();
para3.getPortions().add(new aspose.slides.Portion("paragraph by paragraph"));
var paragraphCollection = autoShape.getTextFrame().getParagraphs();
paragraphCollection.add(para1);
paragraphCollection.add(para2);
paragraphCollection.add(para3);
paragraphCollection.add(new aspose.slides.Paragraph());
var mainSequence = presentation.getSlides().get_Item(0).getTimeline().getMainSequence();
var effect1 = mainSequence.addEffect(para1, aspose.slides.EffectType.Appear, aspose.slides.EffectSubtype.None, aspose.slides.EffectTriggerType.AfterPrevious);
var effect2 = mainSequence.addEffect(para2, aspose.slides.EffectType.Appear, aspose.slides.EffectSubtype.None, aspose.slides.EffectTriggerType.AfterPrevious);
var effect3 = mainSequence.addEffect(para3, aspose.slides.EffectType.Appear, aspose.slides.EffectSubtype.None, aspose.slides.EffectTriggerType.AfterPrevious);
var effect4 = mainSequence.addEffect(para3, aspose.slides.EffectType.Appear, aspose.slides.EffectSubtype.None, aspose.slides.EffectTriggerType.AfterPrevious);
effect1.getTiming().setTriggerDelayTime(1.0);
effect2.getTiming().setTriggerDelayTime(1.0);
effect3.getTiming().setTriggerDelayTime(1.0);
effect4.getTiming().setTriggerDelayTime(1.0);
final var fps = 33;
var frames = java.newInstanceSync("java.util.ArrayList");
var animationsGenerator = new aspose.slides.PresentationAnimationsGenerator(presentation);
try {
var player = new aspose.slides.PresentationPlayer(animationsGenerator, fps);
try {
player.setFrameTick((sender, arguments) -> {
try {
var frame = java.callStaticMethodSync("java.lang.String", "format", "frame_%04d.png", sender.getFrameIndex());
arguments.getFrame().save(frame, aspose.slides.ImageFormat.Png);
frames.add(frame);
} catch (e) {console.log(e);
throw java.newInstanceSync("java.lang.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
var ffmpeg = java.newInstanceSync("FFmpeg", "path/to/ffmpeg");
var ffprobe = java.newInstanceSync("FFprobe", "path/to/ffprobe");
var builder = java.newInstanceSync("FFmpegBuilder").addExtraArgs("-start_number", "1").setInput("frame_%04d.png").addOutput("output.avi").setVideoFrameRate(java.getStaticFieldValue("FFmpeg", "FPS_24")).setFormat("avi").done();
var executor = java.newInstanceSync("FFmpegExecutor", ffmpeg, ffprobe);
executor.createJob(builder).run();
} catch (e) {console.log(e);
console.log(e);
}
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 PresentationAnimationsGenerator và PresentationPlayer.
PresentationAnimationsGenerator cho phép bạn đặt kích thước khung cho video (sẽ được tạo sau này) thông qua hàm khởi tạo của nó. Nếu bạn truyền một thể hiện của bản trình chiếu, Presentation.getSlideSize sẽ được sử dụng và nó tạo ra các hoạt hình mà PresentationPlayer sử dụng.
Khi các hoạt hình được tạo, một sự kiện NewAnimation được sinh ra cho mỗi hoạt hình tiếp theo, có tham số trình phát hoạt hình bản trình chiếu. Tham số này là một lớp đại diện cho trình phát cho một hoạt hình riêng biệt.
Để làm việc với trình phát hoạt hình bản trình chiếu, phương thức getDuration (độ dài toàn bộ của hoạt hình) và phương thức setTimePosition được sử dụng. Mỗi vị trí hoạt hì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 hình tại thời điểm đó:
var presentation = new aspose.slides.Presentation();
try {
// Thêm một hình mặt cười và tạo hoạt hình cho nó
var smile = presentation.getSlides().get_Item(0).getShapes().addAutoShape(aspose.slides.ShapeType.SmileyFace, 110, 20, 500, 500);
var mainSequence = presentation.getSlides().get_Item(0).getTimeline().getMainSequence();
var effectIn = mainSequence.addEffect(smile, aspose.slides.EffectType.Fly, aspose.slides.EffectSubtype.TopLeft, aspose.slides.EffectTriggerType.AfterPrevious);
var effectOut = mainSequence.addEffect(smile, aspose.slides.EffectType.Fly, aspose.slides.EffectSubtype.BottomRight, aspose.slides.EffectTriggerType.AfterPrevious);
effectIn.getTiming().setDuration(2.0);
effectOut.setPresetClassType(aspose.slides.EffectPresetClassType.Exit);
var animationsGenerator = new aspose.slides.PresentationAnimationsGenerator(presentation);
try {
animationsGenerator.setNewAnimation(animationPlayer -> {
console.log(java.callStaticMethodSync("java.lang.String", "format", "Animation total duration: %f", animationPlayer.getDuration()));
animationPlayer.setTimePosition(0);// trạng thái ban đầu của hoạt hình
try {
// bitmap trạng thái ban đầu của hoạt hình
animationPlayer.getFrame().save("firstFrame.png", aspose.slides.ImageFormat.Png);
} catch (e) {console.log(e);
throw java.newInstanceSync("java.lang.RuntimeException", e);
}
animationPlayer.setTimePosition(animationPlayer.getDuration());// trạng thái cuối cùng của hoạt hình
try {
// khung cuối cùng của hoạt hình
animationPlayer.getFrame().save("lastFrame.png", aspose.slides.ImageFormat.Png);
} catch (e) {console.log(e);
throw java.newInstanceSync("java.lang.RuntimeException", e);
}
});
} finally {
if (animationsGenerator != null) {
animationsGenerator.dispose();
}
}
} finally {
if (presentation != null) {
presentation.dispose();
}
}
Để làm cho tất cả các hoạt hình trong một bản trình chiếu chạy đồ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 các 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 hình để chúng được phát:
var presentation = new aspose.slides.Presentation("animated.pptx");
try {
var animationsGenerator = new aspose.slides.PresentationAnimationsGenerator(presentation);
try {
var player = new aspose.slides.PresentationPlayer(animationsGenerator, 33);
try {
player.setFrameTick((sender, arguments) -> {
try {
arguments.getFrame().save(("frame_" + sender.getFrameIndex()) + ".png", aspose.slides.ImageFormat.Png);
} catch (e) {console.log(e);
throw java.newInstanceSync("java.lang.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 Chuyển đổi PowerPoint sang Video.
Các hoạt hình và hiệu ứng được hỗ trợ
Mở đầu:
| Loại hoạt hình | Aspose.Slides | PowerPoint |
|---|---|---|
| Xuất hiện | ![]() |
![]() |
| Mờ dần | ![]() |
![]() |
| Bay vào | ![]() |
![]() |
| Trôi vào | ![]() |
![]() |
| Tách | ![]() |
![]() |
| Lau | ![]() |
![]() |
| Hình dạng | ![]() |
![]() |
| Bánh xe | ![]() |
![]() |
| Thanh ngẫu nhiên | ![]() |
![]() |
| Phóng to & Xoay | ![]() |
![]() |
| Thu phóng | ![]() |
![]() |
| Quay tròn | ![]() |
![]() |
| Bị nẩy | ![]() |
![]() |
Nhấn mạnh:
| Loại hoạt hình | Aspose.Slides | PowerPoint |
|---|---|---|
| Nhịp tim | ![]() |
![]() |
| Nhịp tim màu | ![]() |
![]() |
| Lắc lư | ![]() |
![]() |
| Xoay | ![]() |
![]() |
| Phóng to/Thu nhỏ | ![]() |
![]() |
| Mờ màu | ![]() |
![]() |
| Tối hơn | ![]() |
![]() |
| Sáng hơn | ![]() |
![]() |
| Độ trong suốt | ![]() |
![]() |
| Màu đối tượng | ![]() |
![]() |
| Màu bổ sung | ![]() |
![]() |
| Màu đường viền | ![]() |
![]() |
| Màu nền | ![]() |
![]() |
Kết thúc:
| Loại hoạt hình | Aspose.Slides | PowerPoint |
|---|---|---|
| Biến mất | ![]() |
![]() |
| Mờ dần | ![]() |
![]() |
| Bay ra | ![]() |
![]() |
| Trôi ra | ![]() |
![]() |
| Tách | ![]() |
![]() |
| Lau | ![]() |
![]() |
| Hình dạng | ![]() |
![]() |
| Thanh ngẫu nhiên | ![]() |
![]() |
| Thu nhỏ & Xoay | ![]() |
![]() |
| Thu phóng | ![]() |
![]() |
| Quay tròn | ![]() |
![]() |
| Bị nẩy | ![]() |
![]() |
Đường di chuyển:
| Loại hoạt hình | Aspose.Slides | PowerPoint |
|---|---|---|
| Đường thẳng | ![]() |
![]() |
| Cung | ![]() |
![]() |
| Xoay vòng | ![]() |
![]() |
| Hình dạng | ![]() |
![]() |
| Vòng lặp | ![]() |
![]() |
| Đường tùy chỉnh | ![]() |
![]() |
Câu hỏi thường gặp
Có thể chuyển đổi các bản trình chiếu được bảo vệ bằng mật khẩu không?
Có, Aspose.Slides cho phép làm việc với các bản trình chiếu đượ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 chiếu.
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 bản trình chiếu khi chuyển đổi không?
Aspose.Slides có khả năng xử lý các bản trình chiếu với 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 được khuyến nghị tối ưu hóa bản trình chiếu để cải thiện hiệu năng.

