Learn how to embed or extract video into/from PowerPoint presentations using Java: https://blog.aspose.com/2021/09/20/embed-or-extract-videos-in-powerpoint-using-java/
Last active
September 20, 2021 13:51
-
-
Save aspose-com-gists/5c16c66ab53f4a56c8a1391764741097 to your computer and use it in GitHub Desktop.
Embed Video in PowerPoint Presentations using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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