Manage Video Frames in Presentations Using C++

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 an 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 C++ code shows you how to add a video stored locally to a presentation:

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IVideo.h>
#include <DOM/IVideoCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <LoadingStreamBehavior.h>
#include <system/io/file_stream.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

// Loads the video
System::SharedPtr<System::IO::FileStream> fileStream = System::MakeObject<System::IO::FileStream>(u"Wildlife.mp4", System::IO::FileMode::Open, System::IO::FileAccess::Read);
System::SharedPtr<IVideo> video = pres->get_Videos()->AddVideo(fileStream, LoadingStreamBehavior::KeepLocked);

// Gets the first slide and adds a videoframe
pres->get_Slide(0)->get_Shapes()->AddVideoFrame(10.0f, 10.0f, 150.0f, 250.0f, video);

// Saves the presentation to disk
pres->Save(u"pres-with-video.pptx", SaveFormat::Pptx);

Alternatively, you can add a video by passing its file path directly to the AddVideoFrame() method:

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IVideoFrame.h>
#include <DOM/Presentation.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

System::SharedPtr<ISlide> sld = pres->get_Slide(0);
System::SharedPtr<IVideoFrame> vf = sld->get_Shapes()->AddVideoFrame(50.0f, 150.0f, 300.0f, 150.0f, u"video1.avi");

Create a Video Frame with Video from a Web Source

Newer versions of Microsoft PowerPoint support online 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 C++ code shows you how to add a video from the web to a slide in a PowerPoint presentation:

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/IVideoFrame.h>
#include <DOM/Presentation.h>
#include <DOM/VideoPlayModePreset.h>
#include <Export/SaveFormat.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

// The path to the documents directory.
const String outPath = u"../out/AddVideoFrameFromWebSource_out.pptx";
const String filePath = u"../templates/video1.avi";

// Instantiates a Presentation object that represents a presentation file
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// Accesses the first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// Adds a Video Frame 
System::SharedPtr<IVideoFrame> vf = slide->get_Shapes()->AddVideoFrame(10, 10, 427, 240,u"https://www.youtube.com/embed/Tj75Arhq5ho");

// Sets the Play Mode and Volume of the Video
vf->set_PlayMode(VideoPlayModePreset::Auto);

//Saves the presentation to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Trim a Video Frame

Aspose.Slides allows you to control which part of a video is played by setting the trim-from-start and trim-from-end values through IVideoFrame::set_TrimFromStart and IVideoFrame::set_TrimFromEnd. Both values are specified in milliseconds and define how much time is skipped from the beginning and end of the video, respectively. These settings change the video playback settings in the presentation; they do not cut or otherwise modify the embedded video binary data.

Set Trim Settings

To create a video frame and set its trim settings:

  1. Create an instance of the Presentation class.
  2. Add an IVideo object to the presentation.
  3. Add an IVideoFrame object to a slide.
  4. Set the trim-from-start and trim-from-end values through IVideoFrame::set_TrimFromStart and IVideoFrame::set_TrimFromEnd.
  5. Save the modified presentation.

The following code example skips the first 2.5 seconds and the last second of an embedded video during playback:

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IVideoCollection.h>
#include <DOM/IVideoFrame.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/file.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>();

auto videoData = File::ReadAllBytes(u"video.mp4");
auto video = presentation->get_Videos()->AddVideo(videoData);

auto slide = presentation->get_Slide(0);
auto videoFrame = slide->get_Shapes()->AddVideoFrame(50, 50, 640, 360, video);

videoFrame->set_TrimFromStart(2500.0f);
videoFrame->set_TrimFromEnd(1000.0f);

presentation->Save(u"video_with_trim.pptx", SaveFormat::Pptx);
presentation->Dispose();

Read Trim Settings

To inspect existing trim settings, load a presentation, find an IVideoFrame object among the shapes on the first slide, and read the values through IVideoFrame::get_TrimFromStart and IVideoFrame::get_TrimFromEnd.

The following code example finds the first video frame on the first slide and reports its trim settings in milliseconds:

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IVideoFrame.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/enumerator_adapter.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace System;

auto presentation = MakeObject<Presentation>(u"video_with_trim.pptx");

auto slide = presentation->get_Slide(0);
for (auto&& shape : System::IterateOver(slide->get_Shapes()))
{
    if (ObjectExt::Is<IVideoFrame>(shape))
    {
        auto videoFrame = ExplicitCast<IVideoFrame>(shape);
        auto trimFromStart = videoFrame->get_TrimFromStart();
        auto trimFromEnd = videoFrame->get_TrimFromEnd();

        Console::WriteLine(u"Trim from start: {0} ms", trimFromStart);
        Console::WriteLine(u"Trim from end: {0} ms", trimFromEnd);

        break;
    }
}

presentation->Dispose();

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::get_CaptionTracks method.

Add Captions to a Video Frame

To add captions to a video frame:

  1. Create an instance of the Presentation class.
  2. Add a video to the presentation.
  3. Add an IVideoFrame object to a slide.
  4. Use the ICaptionsCollection returned by get_CaptionTracks to add a WebVTT caption track.
  5. Save the modified presentation.

The following code shows you how to add captions to a video frame:

#include <DOM/ICaptionsCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IVideoCollection.h>
#include <DOM/IVideoFrame.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/file.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>();

auto videoData = File::ReadAllBytes(u"video.mp4");
auto video = presentation->get_Videos()->AddVideo(videoData);

auto slide = presentation->get_Slide(0);
auto videoFrame = slide->get_Shapes()->AddVideoFrame(0, 0, 100, 100, video);

// Adds a new captions track from a WebVTT file.
videoFrame->get_CaptionTracks()->Add(u"English", u"track.vtt");

presentation->Save(u"video_with_captions.pptx", SaveFormat::Pptx);
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:

  1. Load the presentation that contains the video.
  2. Find the target IVideoFrame object.
  3. Iterate through the caption tracks returned by get_CaptionTracks.
  4. Save each caption track to a .vtt file.

The following code shows you how to extract captions from a video frame:

#include <DOM/ICaptions.h>
#include <DOM/ICaptionsCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IVideoFrame.h>
#include <DOM/Presentation.h>
#include <system/enumerator_adapter.h>
#include <system/io/file.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>(u"video_with_captions.pptx");
auto slide = presentation->get_Slide(0);

for (auto&& shape : System::IterateOver(slide->get_Shapes()))
{
    if (ObjectExt::Is<IVideoFrame>(shape))
    {
        auto videoFrame = ExplicitCast<IVideoFrame>(shape);
        for (auto&& captionTrack : System::IterateOver(videoFrame->get_CaptionTracks()))
        {
            // Saves the captions track to a WebVTT file.
            auto filePath = captionTrack->get_CaptionId().ToString() + u".vtt";
            File::WriteAllBytes(filePath, captionTrack->get_BinaryData());
        }
    }
}

presentation->Dispose();

Each ICaptions object exposes the caption identifier, label, binary data, and caption data as a UTF-8 string.

Remove Captions from a Video Frame

To remove captions from a video frame:

  1. Load the presentation that contains the video.
  2. Get the target IVideoFrame object.
  3. Remove caption tracks from the collection returned by get_CaptionTracks.
  4. Save the modified presentation.

The following code shows you how to remove all captions from a video frame:

#include <DOM/ICaptionsCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IVideoFrame.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"video_with_captions.pptx");
auto slide = presentation->get_Slide(0);
auto videoFrame = ExplicitCast<IVideoFrame>(slide->get_Shape(0));

// Removes all captions from the video frame.
videoFrame->get_CaptionTracks()->Clear();

presentation->Save(u"video_without_captions.pptx", SaveFormat::Pptx);
presentation->Dispose();

If you need to remove only one caption track, use the Remove or RemoveAt methods instead of Clear.

Extract Video from a 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 C++ code shows you how to extract the video on a presentation slide:

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/IVideo.h>
#include <DOM/Presentation.h>
#include <DOM/VideoFrame.h>
#include <system/enumerator_adapter.h>
#include <system/io/file_access.h>
#include <system/io/file_mode.h>
#include <system/io/file_share.h>
#include <system/io/file_stream.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace System::IO;

// The path to the documents directory.
const System::String templatePath = u"../templates/Video.pptx";
const System::String outPath = u"../out/Video_out";

auto presentation = System::MakeObject<Presentation>(templatePath);
for (auto&& slide : System::IterateOver(presentation->get_Slides()))
{
    for (auto&& shape : System::IterateOver(slide->get_Shapes()))
    {
        if (System::ObjectExt::Is<VideoFrame>(shape))
        {
            System::SharedPtr<VideoFrame> vf = System::AsCast<VideoFrame>(shape);
            System::String type = vf->get_EmbeddedVideo()->get_ContentType();
            type = type.Remove(0, type.LastIndexOf('/') + 1);
            auto buffer = vf->get_EmbeddedVideo()->get_BinaryData();

            auto stream = System::MakeObject<System::IO::FileStream>(
                outPath + type, System::IO::FileMode::Create, System::IO::FileAccess::Write,
                System::IO::FileShare::Read);
            stream->Write(buffer, 0, buffer->get_Length());
        }
    }
}

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.