Embed Video in PowerPoint Presentations using C#

Embed Video in PowerPoint using C#

Video frames are used in PowerPoint presentations to demonstrate something or to attract the audience. Often, the videos are used to save time and make the presentations more effective. In this article, you will learn how to work with videos in presentations programmatically. Particularly, the article will cover how to embed or extract a video in a PowerPoint presentation using C#.

.NET API to Embed Video in PowerPoint Presentations

In order to embed or extract videos in PowerPoint presentations, we will use Aspose.Slides for .NET. The API is designed to create and manipulate PowerPoint and OpenOffice documents. You can either install the API via NuGet or download its DLL.

PM> Install-Package Aspose.Slides.NET

Embed a Video in PowerPoint Presentation using C#

The following are the steps to embed a video in a PowerPoint presentation using C#.

The following code sample shows how to embed a video in a PowerPoint presentation using C#.

// Instantiate Presentation class that represents the PPTX
using (Presentation pres = new Presentation())
{
// Get the first slide
ISlide sld = pres.Slides[0];
// Add video to presentation
IVideo vid = pres.Videos.AddVideo(new FileStream("Wildlife.mp4", FileMode.Open));
// Add video frame
IVideoFrame vf = sld.Shapes.AddVideoFrame(50, 150, 300, 350, vid);
// Assign video to video frame
vf.EmbeddedVideo = vid;
// Set play mode and volume of the video
vf.PlayMode = VideoPlayModePreset.Auto;
vf.Volume = AudioVolumeMode.Loud;
// Write the PPTX file to disk
pres.Save("VideoFrame_out.pptx", SaveFormat.Pptx);
}
view raw embed-video.cs hosted with ❤ by GitHub

Embed Video in Presentations from a Web Source

You can also embed a video in the PowerPoint presentations from a web source. The following are the steps to achieve this.

The following code sample shows how to embed a video into the presentation from a web source.

using (Presentation pres = new Presentation())
{
// Video ID
string videoId = "Tj75Arhq5ho";
// Add video frame
IVideoFrame videoFrame = pres.Slides[0].Shapes.AddVideoFrame(10, 10, 427, 240, "https://www.youtube.com/embed/" + videoId);
videoFrame.PlayMode = VideoPlayModePreset.Auto;
// Load thumbnail
using (WebClient client = new WebClient())
{
string thumbnailUri = "http://img.youtube.com/vi/" + videoId + "/hqdefault.jpg";
videoFrame.PictureFormat.Picture.Image = pres.Images.AddImage(client.DownloadData(thumbnailUri));
}
// Save presentation
pres.Save("AddVideoFrameFromWebSource_out.pptx", SaveFormat.Pptx);
}

Extract Video from a PowerPoint Presentation in C#

Aspose.Slides for .NET also allows you to extract a video from a presentation. Below are the simple steps to achieve this.

The following code sample shows how to extract videos from a PowerPoint presentation using C#.

// Load a presentation file
Presentation presentation = new Presentation("Video.pptx");
// Loop through slides in the presentation
foreach (ISlide slide in presentation.Slides)
{
// Loop through shapes
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is VideoFrame)
{
// Extract and save video
IVideoFrame vf = shape as IVideoFrame;
String type = vf.EmbeddedVideo.ContentType;
int ss = type.LastIndexOf('/');
type = type.Remove(0, type.LastIndexOf('/') + 1);
Byte[] buffer = vf.EmbeddedVideo.BinaryData;
using (FileStream stream = new FileStream("NewVideo_out." + type, FileMode.Create, FileAccess.Write, FileShare.Read))
{
stream.Write(buffer, 0, buffer.Length);
}
}
}
}

Get a Free API License

You can use Aspose.Slides for .NET without evaluation limitations by requesting a temporary license.

Conclusion

In this article, you have learned how to embed videos in PowerPoint presentations using C#. Moreover, you have seen how to extract videos from a presentation programmatically. In addition to this, you can visit the documentation to explore more about Aspose.Slides for .NET. Also, you can post your queries to our forum.

See Also