Manage Video Frames in Presentations Using Java
Introduction
A well-placed video in a presentation can make your message more compelling and increase engagement levels with your audience.
PowerPoint allows you to add videos to a slide in a presentation in two ways:
- Add or embed a local video (stored on your machine)
- Add an online video (from a web source such as YouTube).
To allow you to add videos (video objects) to a presentation, Aspose.Slides provides the IVideo interface, IVideoFrame interface, and other relevant types.
Create Embedded Video Frames
If the video file you want to add to your slide is stored locally, you can create a video frame to embed the video in your presentation.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Add an IVideo object and pass the video file path to embed the video with the presentation.
- Add an IVideoFrame object to create a frame for the video.
- Save the modified presentation.
This Java code shows you how to add a video stored locally to a presentation:
// Instantiates the Presentation class
Presentation pres = new Presentation("pres.pptx");
try {
// Loads the video
FileInputStream fileStream = new FileInputStream("Wildlife.mp4");
IVideo video = pres.getVideos().addVideo(fileStream, LoadingStreamBehavior.KeepLocked);
// Gets the first slide and adds a videoframe
pres.getSlides().get_Item(0).getShapes().addVideoFrame(10, 10, 150, 250, video);
// Saves the presentation to disk
pres.save("pres-with-video.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
if (pres != null) pres.dispose();
}
Alternatively, you can add a video by passing its file path directly to the addVideoFrame(float x, float y, float width, float height, IVideo video) method:
Presentation pres = new Presentation();
try {
ISlide sld = pres.getSlides().get_Item(0);
IVideoFrame vf = sld.getShapes().addVideoFrame(50, 150, 300, 150, "video1.avi");
} finally {
if (pres != null) pres.dispose();
}
Create Video Frames with Video from Web Sources
Microsoft PowerPoint 2013 and newer support YouTube videos in presentations. If the video you want to use is available online (e.g. on YouTube), you can add it to your presentation through its web link.
- Create an instance of Presentation class
- Get a slide’s reference through its index.
- Add an IVideo object and pass the link to the video.
- Set a thumbnail for the video frame.
- Save the presentation.
This Java code shows you how to add a video from the web to a slide in a PowerPoint presentation:
// Instantiates a Presentation object that represents a presentation file
Presentation pres = new Presentation();
try {
addVideoFromYouTube(pres, "Tj75Arhq5ho");
pres.save("out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
private static void addVideoFromYouTube(Presentation pres, String videoID)
{
// Adds a videoFrame
IVideoFrame videoFrame = pres.getSlides().get_Item(0).getShapes().addVideoFrame(
10, 10, 427, 240, "https://www.youtube.com/embed/" + videoID);
videoFrame.setPlayMode(VideoPlayModePreset.Auto);
// Loads thumbnail
String thumbnailUri = "http://img.youtube.com/vi/" + videoID + "/hqdefault.jpg";
URL url;
try {
url = new URL(thumbnailUri);
videoFrame.getPictureFormat().getPicture().setImage(pres.getImages().addImage(url.openStream()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Manage Video Captions
Aspose.Slides allows you to manage closed captions for video frames in PowerPoint presentations. Captions are stored in WebVTT format and are exposed through the IVideoFrame.getCaptionTracks method.
Add Captions to a Video Frame
To add captions to a video frame:
- Create an instance of the Presentation class.
- Add a video to the presentation.
- Add an IVideoFrame object to a slide.
- Use the ICaptionsCollection returned by getCaptionTracks to add a WebVTT caption track.
- Save the modified presentation.
The following code shows you how to add captions to a video frame:
Presentation presentation = new Presentation();
try {
byte[] videoData = Files.readAllBytes(Paths.get("video.mp4"));
IVideo video = presentation.getVideos().addVideo(videoData);
ISlide slide = presentation.getSlides().get_Item(0);
IVideoFrame videoFrame = slide.getShapes().addVideoFrame(0, 0, 100, 100, video);
// Adds a new captions track from a WebVTT file.
videoFrame.getCaptionTracks().add("English", "track.vtt");
presentation.save("video_with_captions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
The ICaptionsCollection interface also provides an overload that lets you add captions from a stream.
Extract Captions from a Video Frame
To extract captions from a video frame:
- Load the presentation that contains the video.
- Find the target IVideoFrame object.
- Iterate through the caption tracks in the ICaptionsCollection.
- Save each caption track to a
.vttfile.
The following code shows you how to extract captions from a video frame:
Presentation presentation = new Presentation("video_with_captions.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
for (IShape shape : slide.getShapes()) {
if (shape instanceof IVideoFrame) {
IVideoFrame videoFrame = (IVideoFrame)shape;
for (ICaptions captionTrack : videoFrame.getCaptionTracks()) {
// Saves the captions track to a WebVTT file.
String filePath = captionTrack.getCaptionId().toString() + ".vtt";
Files.write(Paths.get(filePath), captionTrack.getBinaryData());
}
}
}
} finally {
presentation.dispose();
}
Each ICaptions object exposes the caption identifier, label, binary data, and caption text as a UTF-8 string.
Remove Captions from a Video Frame
To remove captions from a video frame:
- Load the presentation that contains the video.
- Get the target IVideoFrame object.
- Remove caption tracks from the ICaptionsCollection.
- Save the modified presentation.
The following code shows you how to remove all captions from a video frame:
Presentation presentation = new Presentation("video_with_captions.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IVideoFrame videoFrame = (IVideoFrame)slide.getShapes().get_Item(0);
// Removes all captions from the video frame.
videoFrame.getCaptionTracks().clear();
presentation.save("video_without_captions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
If you need to remove only one caption track, use the remove or removeAt methods instead of clear.
Extract Video from Slides
Besides adding videos to slides, Aspose.Slides allows you to extract videos embedded in presentations.
- Create an instance of the Presentation class to load the presentation containing the video.
- Iterate through all the ISlide objects.
- Iterate through all the IShape objects to find a VideoFrame.
- Save the video to disk.
This Java code shows you how to extract the video on a presentation slide:
// Instantiates a Presentation object that represents a presentation file
Presentation pres = new Presentation("VideoSample.pptx");
try {
for (ISlide slide : pres.getSlides())
{
for (IShape shape : slide.getShapes())
{
if (shape instanceof VideoFrame)
{
IVideoFrame vf = (IVideoFrame) shape;
String type = vf.getEmbeddedVideo().getContentType();
int ss = type.lastIndexOf('-');
byte[] buffer = vf.getEmbeddedVideo().getBinaryData();
//Gets the File Extension
int charIndex = type.indexOf("/");
type = type.substring(charIndex + 1);
FileOutputStream fop = new FileOutputStream("testing2." + type);
fop.write(buffer);
fop.flush();
fop.close();
}
}
}
} catch (IOException e) {
} finally {
if (pres != null) pres.dispose();
}
FAQ
Which video playback parameters can be changed for a VideoFrame?
You can control the playback mode (auto or on click) and looping. These options are available via the VideoFrame object’s properties.
Does adding a video affect the PPTX file size?
Yes. When you embed a local video, the binary data is included in the document, so the presentation size grows in proportion to the file size. When you add an online video, a link and a thumbnail are embedded, so the size increase is smaller.
Can I replace the video in an existing VideoFrame without changing its position and size?
Yes. You can swap the video content within the frame while preserving the shape’s geometry; this is a common scenario for updating media in an existing layout.
Can the content type (MIME) of an embedded video be determined?
Yes. An embedded video has a content type that you can read and use, for example when saving it to disk.