Learn how to embed or extract video into/from PowerPoint presentations using C#: https://blog.aspose.com/2021/09/17/embed-video-in-powerpoint-using-csharp/
Last active
September 18, 2021 05:25
-
-
Save aspose-com-gists/1ac63941dc836f9c33282db0bedfb3d0 to your computer and use it in GitHub Desktop.
Embed Video in PowerPoint Presentations using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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