Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Embed Video in PowerPoint Presentations using C#
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);
}
// 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);
}
// 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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment