Insert or Extract Videos in PowerPoint in Java

Andrew Wilson
3 min readMar 14, 2024

Inserting a video in PowerPoint is one way to enhance your presentations. It will make your document more appealing to your audience. This article will share how to insert videos in PowerPoint or extract videos from PowerPoint in Java using a free API.

Java Library for Processing PowerPoint Presentations

To insert or extract videos from PowerPoint presentations, we will need to use the free Java library — Free Spire.Presentation for Java.

Below are two methods to install it.
● Download the free API and unzip it, and then we can maually import the Spire.Presentation.jar to our projects as dependency.

● Install from maven repository by adding the following dependency to your pom.xml:

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Java: Insert a Video into a PowerPoint Presentation

Free Spire.Presentation for Java offers the ISlide.getShapes().appendVideoMedia(String, Rectangle2D) method to insert a video into a slide. The following is the code sample to insert video:

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class InsertVideo {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a PowerPoint document
presentation.loadFromFile("windmills.pptx");

//Get the first slide
ISlide slide = presentation.getSlides().get(0);

//Add description text
Rectangle2D.Double rec_title = new Rectangle2D.Double(150, 360, 160, 50);
IAutoShape shape_title = slide.getShapes().appendShape(ShapeType.RECTANGLE, rec_title);
shape_title.getLine().setFillType(FillFormatType.NONE);
shape_title.getFill().setFillType(FillFormatType.NONE);
ParagraphEx para_title = new ParagraphEx();
para_title.setText("Video:");
para_title.setAlignment(TextAlignmentType.CENTER);
para_title.getTextRanges().get(0).setLatinFont(new TextFont("Arial Black"));
para_title.getTextRanges().get(0).setFontHeight(32);
para_title.getTextRanges().get(0).isBold(TriState.TRUE);
para_title.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
para_title.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.gray);
shape_title.getTextFrame().getParagraphs().append(para_title);

//Add a video to the first slide
Rectangle2D.Double videoRect = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 120, 310, 260, 180);
IVideo video = slide.getShapes().appendVideoMedia("windmill.mp4", videoRect);
//Set a thumbnail image for the video
video.getPictureFill().getPicture().setUrl("video.png");

//Save the result document
presentation.saveToFile("InsertVideo.pptx", FileFormat.PPTX_2013);
}
}

Output:

Java to insert video in pptx

Java: Extract Videos from a PowerPoint Presentation

To extract the existing videos, you can first find the video shapes, and then save the videos to disk using IVideo.getEmbeddedVideoData().saveToFile() method. The following is the code sample to extract video:

import com.spire.presentation.*;

public class ExtractVideo {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a PowerPoint document
presentation.loadFromFile("InsertVideo.pptx");

int i = 0;
//Specify the output file path
String videoPath = String.format("Videos/Video{0}.mp4", i);

//Loop through all slides in the document
for (Object slideObj : presentation.getSlides()) {
ISlide slide = (ISlide) slideObj;
//Loop through all shapes on each slide
for (Object shapeObj : slide.getShapes()) {
IShape shape = (IShape) shapeObj;
//Check if the shape is of IVideo type
if (shape instanceof IVideo) {
//Save the video to the specified path
((IVideo) shape).getEmbeddedVideoData().saveToFile(videoPath);
i++;
}
}
}
}
}

Output:

Java to extract video from pptx

The above two code examples shows insert or extract video from a PPTX file. If you need to work with audio or other elements in PowerPoint using Java, you can check out the demos here.

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.