Video Frame

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 Frame

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.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add an IVideo object and pass the video file path to embed the video with the presentation.
  4. Add an IVideoFrame object to create a frame for the video.
  5. Save the modified presentation.

This Python code shows you how to add a video stored locally to a presentation:

import aspose.slides as slides

with slides.Presentation(path + "pres.pptx") as pres:
    with open("Wildlife.mp4", "br") as fileStream:
        video = pres.videos.add_video(fileStream, slides.LoadingStreamBehavior.KEEP_LOCKED)

        # Gets the first slide and adds a videoframe
        pres.slides[0].shapes.add_video_frame(10, 10, 150, 250, video)

        # Saves the presentation to disk
        pres.save(path + "pres-with-video.pptx", slides.export.SaveFormat.PPTX)

Alternatively, you can add a video by passing its file path directly to the add_video_frame(x, y, width, height, fname) method:

import aspose.slides as slides

with slides.Presentation() as pres:
    sld = pres.slides[0]
    vf = sld.shapes.add_video_frame(50, 150, 300, 150, "video1.avi")

Create Video Frame with Video from Web Source

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.

  1. Create an instance of Presentation class
  2. Get a slide’s reference through its index.
  3. Add an IVideo object and pass the link to the video.
  4. Set a thumbnail for the video frame.
  5. Save the presentation.

This Python code shows you how to add a video from the web to a slide in a PowerPoint presentation:

import aspose.slides as slides
from urllib.request import urlopen

def add_video_from_youyube(pres, videoId):
    # Adds a videoFrame
    videoFrame = pres.slides[0].shapes.add_video_frame(10, 10, 427, 240, "https://www.youtube.com/embed/" + videoId)
    videoFrame.play_mode = slides.VideoPlayModePreset.AUTO

    # Loads thumbnail
    thumbnail_uri = "http://img.youtube.com/vi/" + videoId + "/hqdefault.jpg"
    f = urlopen(thumbnail_uri)
    videoFrame.picture_format.picture.image = pres.images.add_image(f.read())


with slides.Presentation() as pres:
    add_video_from_youyube(pres, "s5JbfQZ5Cc0")
    pres.save("AddVideoFrameFromWebSource_out.pptx", slides.export.SaveFormat.PPTX)

Extract Video From Slide

Besides adding videos to slides, Aspose.Slides allows you to extract videos embedded in presentations.

  1. Create an instance of the Presentation class to load the presentation containing the video.
  2. Iterate through all the ISlide objects.
  3. Iterate through all the IShape objects to find a VideoFrame.
  4. Save the video to disk.

This Python code shows you how to extract the video on a presentation slide:

import aspose.slides as slides

# Instantiates a Presentation object that represents a presentation file 
with slides.Presentation(path + "Video.pptx") as presentation:
    for shape in presentation.slides[0].shapes:
        if type(shape) is slides.VideoFrame:
            type = shape.embedded_video.content_type
            buffer = shape.embedded_video.binary_data
            with open("NewVideo_out." + type[type.rfind('/') + 1:len(type)], "wb") as stream:
                stream.write(buffer)