Videos are common elements in PowerPoint, sometimes developers need to extract videos from PowerPoint documents and save them to disk programmatically. Spire.Presentation provides developers many flexible functions to manipulate PowerPoint documents including extract Videos. This article will explain how to achieve this task in C# and VB.NET using Spire.Presentation.
Note: Before start, please download and install Spire.Presentation correctly, after that add a reference to Spire.Presentation.dll in your project.
Detail steps:
Step 1: Instantiate an object of Presentation class and load the sample document.
Presentation presentation = new Presentation(); presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");
Step 2: Loop through all shapes on the slides, find out the videos and then call VideoData.SaveToFile(string fileName) method to save them to disk.
int i = 0; foreach (ISlide slide in presentation.Slides) { foreach (IShape shape in slide.Shapes) { if (shape is IVideo) { (shape as IVideo).EmbeddedVideoData.SaveToFile(string.Format(@"Video\Video-{0}.mp4",i)); i++; } } }
Below are the extracted videos from the PowerPoint document:
Full code:
using Spire.Presentation; namespace Extract_Video { class Program { static void Main(string[] args) { Presentation presentation = new Presentation(); presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx"); int i = 0; foreach (ISlide slide in presentation.Slides) { foreach (IShape shape in slide.Shapes) { if (shape is IVideo) { (shape as IVideo).EmbeddedVideoData.SaveToFile(string.Format(@"Video\Video-{0}.mp4",i)); i++; } } } } } }
Imports Spire.Presentation Namespace Extract_Video Class Program Private Shared Sub Main(args As String()) Dim presentation As New Presentation() presentation.LoadFromFile("C:\Users\Administrator\Desktop\Sample.pptx") Dim i As Integer = 0 For Each slide As ISlide In presentation.Slides For Each shape As IShape In slide.Shapes If TypeOf shape Is IVideo Then TryCast(shape, IVideo).EmbeddedVideoData.SaveToFile(String.Format("Video\Video-{0}.mp4", i)) i += 1 End If Next Next End Sub End Class End Namespace