Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 18, 2021 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/1ac63941dc836f9c33282db0bedfb3d0 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/1ac63941dc836f9c33282db0bedfb3d0 to your computer and use it in GitHub Desktop.
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