Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Embed Video in PowerPoint Presentations using Java
// Create a presentation or load an exising one
Presentation pres = new Presentation("presentation");
try {
// add videoFrame
IVideoFrame videoFrame = pres.getSlides().get_Item(0).getShapes().addVideoFrame(
10, 10, 427, 240, "https://www.youtube.com/embed/Tj75Arhq5ho");
videoFrame.setPlayMode(VideoPlayModePreset.Auto);
// load thumbnail
String thumbnailUri = "http://img.youtube.com/vi/Tj75Arhq5ho/hqdefault.jpg";
URL url;
// Set thumbnail
try {
url = new URL(thumbnailUri);
videoFrame.getPictureFormat().getPicture().setImage(pres.getImages().addImage(url.openStream()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Save presentation
pres.save("out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
// Load presentation or create a new one
Presentation pres = new Presentation();
try {
// Get the first slide
ISlide sld = pres.getSlides().get_Item(0);
// Add video inside presentation
IVideo vid = pres.getVideos().addVideo(new FileInputStream(new File("Wildlife.mp4")));
// Add video frame
IVideoFrame vf = sld.getShapes().addVideoFrame(50, 150, 300, 350, vid);
// Set video to video frame
vf.setEmbeddedVideo(vid);
// Set play mode and volume of the video
vf.setPlayMode(VideoPlayModePreset.Auto);
vf.setVolume(AudioVolumeMode.Loud);
// Write the PPTX file to disk
pres.save("VideoFrame.pptx", SaveFormat.Pptx);
} catch (Exception e) {
} finally {
if (pres != null) pres.dispose();
}
// Load presentation
Presentation pres = new Presentation("VideoSample.pptx");
try {
// Loop through slides
for (ISlide slide : pres.getSlides())
{
// Loop through shapes
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();
//Get 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment