News Category

Audio and Video

Audio and Video (5)

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:

How to Extract Videos from PowerPoint Documents in C#, VB.NET

Full code:

[C#]
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++;
                    }
                }
            }

        }
    }
}
[VB.NET]
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

Video is often used to create a more attractive and interesting effect in PowerPoint, there are two modes to play video in PowerPoint: on click and automatically. In general, the video would be played by clicking on it, people who want to play the video automatically needs to set the default play mode as auto style. This article will demonstrate how to set the play mode for video in PowerPoint using C#, VB.NET.

Note: Before start, please download and install Spire.Presentation correctly, after that add the Spire.Presentation.dll file as the reference of your project.

Detail steps overview:

Step 1: Initialize a new presentation instance and load the original document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");

Step 2: Find the video by looping through all the slides and set its play mode as auto.

foreach (ISlide slide in presentation.Slides)
{
    foreach (IShape shape in slide.Shapes)
        {
            if (shape is IVideo)
            {
                (shape as IVideo).PlayMode = VideoPlayMode.Auto;
            }
        }
}

Step 3: Save the file as Video.pptx.

presentation.SaveToFile("Video.pptx", FileFormat.Pptx2010);

Full codes:

[C#]
using Spire.Presentation;

namespace Set_Video_Play_Mode
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");
            foreach (ISlide slide in presentation.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape is IVideo)
                    {
                        (shape as IVideo).PlayMode = VideoPlayMode.Auto;
                    }
                }
            }
            presentation.SaveToFile("Video.pptx", FileFormat.Pptx2010);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Namespace Set_Video_Play_Mode
	Class Program
		Private Shared Sub Main(args As String())
			Dim presentation As New Presentation()
	presentation.LoadFromFile("C:\Users\Administrator\Desktop\Sample.pptx")
			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).PlayMode = VideoPlayMode.Auto
					End If
				Next
			Next
			presentation.SaveToFile("Video.pptx", FileFormat.Pptx2010)
		End Sub
	End Class
End Namespace

If you couldn't use Spire.Presentation successfully, please refer the Spire.Presentation Quick Start which will guide you to quickly use the Spire.Presentation.

Audio can be used in PowerPoint document to create a more interesting and dynamic PowerPoint effect. Using the Spire.presention you can now use the Shapes.AppendVideoMedia() method to insert audio in your PowerPoint document at specific position with C#, VB.NET. There is guide will introduce the method.

Here are the steps:

Step 1: Create a new PowerPoint document first.

Presentation presentation = new Presentation();

Step 2: Then set the backgroung image before inserting Audio file.

string ImageFile = "2.jpg";
RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

Step 3: Load the Audio file from disk and set properties of AutoShape

presentation.Slides[0].Shapes.AppendAudioMedia(@"1.mp3",500,100,true);
IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(500, 100, 100, 150));
shape.ShapeStyle.LineColor.Color = Color.White;
shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;

Step 4: Save the PowerPoint file and review.

presentation.SaveToFile("audio.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("audio.pptx");

Effect Screenshot:

Insert audio to PowerPoint document at specified position in C#, VB.NET

Full code:

[C#]
using Spire.Presentation;
using System.Drawing;

namespace InsertAudio
{

    class Program
    {

        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();

            string ImageFile = "2.jpg";
            RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
            presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

            presentation.Slides[0].Shapes.AppendAudioMedia(@"1.mp3", 500, 100, true);
            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(500, 100, 100, 150));
            shape.ShapeStyle.LineColor.Color = Color.White;
            shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;

            presentation.SaveToFile("audio.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("audio.pptx");

        }

    }
}
[VB.NET]
Imports Spire.Presentation
Imports System.Drawing

Namespace InsertAudio

	Class Program

		Private Shared Sub Main(args As String())
			Dim presentation As New Presentation()

			Dim ImageFile As String = "2.jpg"
			Dim rect As New RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height)
			presentation.Slides(0).Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect)
			presentation.Slides(0).Shapes(0).Line.FillFormat.SolidFillColor.Color = Color.FloralWhite

			presentation.Slides(0).Shapes.AppendAudioMedia("1.mp3", 500, 100, True)
			Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(500, 100, 100, 150))
			shape.ShapeStyle.LineColor.Color = Color.White
			shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None

			presentation.SaveToFile("audio.pptx", FileFormat.Pptx2010)
			System.Diagnostics.Process.Start("audio.pptx")

		End Sub

	End Class
End Namespace

Sometimes, audiences may feel bored when they see slides with only text and images. Inserting a video is a great way to add visual interest to your presentation and make it more engaging for your audience. In this article, you will learn how to insert videos as well as replace and extract videos in PowerPoint in C# and VB.NET using Spire.Presentation for .NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

Insert a Video into a PowerPoint Presentation in C# and VB.NET

By inserting a video into your presentation, you can share the video with your audience instantly without having to look for it on your computer while presenting. The following steps demonstrate how to insert a video into a PowerPoint presentation:

  • Create an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get a specific slide by its index through Presentation.Slides[int] property.
  • Create an instance of the RectangleF class.
  • Add a video to the slide using ISlide.Shapes.AppendVideoMedia(string, RectangleF) method.
  • Set a thumbnail image for the video through IVideo.PictureFill.Picture.Url property.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace InsertVideoInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation presentation = new Presentation();
            //Load a PowerPoint document
            presentation.LoadFromFile(@"Input.pptx");

            //Get the first slide
            ISlide slide = presentation.Slides[0];

            //Add description text
            RectangleF rec_title = new RectangleF(50, 280, 160, 50);
            IAutoShape shape_title = slide.Shapes.AppendShape(ShapeType.Rectangle, rec_title);
            shape_title.ShapeStyle.LineColor.Color = Color.Transparent;
            shape_title.Fill.FillType = FillFormatType.None;
            TextParagraph para_title = new TextParagraph();
            para_title.Text = "Video:";
            para_title.Alignment = TextAlignmentType.Center;
            para_title.TextRanges[0].LatinFont = new TextFont("Myriad Pro Light");
            para_title.TextRanges[0].FontHeight = 32;
            para_title.TextRanges[0].IsBold = TriState.True;
            para_title.TextRanges[0].Fill.FillType = FillFormatType.Solid;
            para_title.TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(68, 68, 68);
            shape_title.TextFrame.Paragraphs.Append(para_title);
            
            //Add a video to the first slide
            RectangleF videoRect = new RectangleF(presentation.SlideSize.Size.Width / 2 - 125, 240, 240, 130);
            IVideo video = slide.Shapes.AppendVideoMedia("Video.mp4", videoRect);
            //Set a thumbnail image for the video
            video.PictureFill.Picture.Url = @"Picture.png";   

            //Save the result document
            presentation.SaveToFile("InsertVideo.pptx", FileFormat.Pptx2010);
        }
    }
}

C#/VB.NET: Insert, Replace or Extract Videos in PowerPoint

Replace a Video in a PowerPoint Presentation in C# and VB.NET

If you think an existing video cannot support your statements well, you can replace it with another suitable one. The following steps demonstrate how to replace an existing video with another video in a PowerPoint presentation:

  • Create an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get a specific slide by its index through Presentation.Slides[int] property.
  • Load a video into a byte array using File.ReadAllBytes() method.
  • Add the loaded video to the video collection of the document using Presentation.Videos.Append(byte[]) method.
  • Loop through all shapes on the slide and find the video shape.
  • Replace the original video with the loaded video through IVideo.EmbeddedVideoData property. Then change the thumbnail image of the video through IVideo.PictureFill.Picture.Url property.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;
using System.IO;

namespace ReplaceVideoInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("InsertVideo.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Load a video into a byte array
            byte[] bts = File.ReadAllBytes(@"NewVideo.mp4");
            //Add the loaded video to the video collection of the document
            VideoData videoData = ppt.Videos.Append(bts);

            //Loop through all shapes on the first slide
            foreach (Shape shape in slide.Shapes)
            {
                //Check if the shape is of IVideo type
                if (shape is IVideo)
                {
                    //Typecast the shape as IVideo
                    IVideo video = shape as IVideo;                   
                    //Replace the original video with the loaded video
                    video.EmbeddedVideoData = videoData;
                    //Change the thumbnail image of the video
                    video.PictureFill.Picture.Url = @"Picture1.png";
                }
            }

            //Save the result document
            ppt.SaveToFile("ReplaceVideo.pptx", FileFormat.Pptx2010);
        }
    }
}

C#/VB.NET: Insert, Replace or Extract Videos in PowerPoint

Extract Videos from a PowerPoint Presentation in C# and VB.NET

If you like the videos in a PowerPoint presentation and want to use them in other places, you can extract and save them to your disk. The following steps demonstrate how to extract videos from a PowerPoint presentation:

  • Create an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the document.
  • Loop through all shapes on each slide.
  • Find the video shapes, then save the videos to disk using IVideo.EmbeddedVideoData.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;

namespace ExtractVideosInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation presentation = new Presentation();
            //Load a PowerPoint document
            presentation.LoadFromFile(@"ReplaceVideo.pptx");

            int i = 0;
            //Specify the output file path
            string result = string.Format(@"Videos\Video{0}.mp4", i);
                
            //Loop through all slides in the document
            foreach (ISlide slide in presentation.Slides)
            {
                //Loop through all shapes on each slide
                foreach (IShape shape in slide.Shapes)
                {
                    //Check if the shape is of IVideo type
                    if (shape is IVideo)
                    {
                        //Save the video to the specified path
                        (shape as IVideo).EmbeddedVideoData.SaveToFile(result);
                        i++;
                    }
                }
            }
        }
    }
}

C#/VB.NET: Insert, Replace or Extract Videos in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Generally, we tend to insert graphics, animation effects, audio frames or video frames into our PPT slides to make it more vivid and persuasive. Adding high-quality audio, background music for example, to a slide presentation can greatly increase the impact that it has on your viewers.

Spire.Presentation for .NET is a comprehensive toolkit which allows developers to process PPT slides in massive ways, including multitude of graphics and multimedia features. In this topic, we will make a simple introduction of how to insert audio into PPT using C#.

Step 1: Create a new PPT document first

            Presentation presentation = new Presentation()

Step 2: Basically, we need to set a background image before inserting Audio file

string ImageFile = "bg.png";

            RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
            presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

Step 3: Load the Audio file from disk

presentation.Slides[0].Shapes.AppendAudioMedia(Path.GetFullPath("paipai_sound.wav"), new RectangleF(100, 100, 20, 20));

Step 4: Set properties of AutoShape

IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 150, 600, 250));
            shape.ShapeStyle.LineColor.Color = Color.White;
            shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;

Step 5: Save PPT file

presentation.SaveToFile("Audio.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("Audio.pptx");

Effect Screenshot:

Insert Audio into PPT

With Spire.Presentation, you can insert audio into your PowerPoint documents and do more same thing in your any .NET(C#, VB.NET, ASP.NET) applications without PowerPoint automation and any other third party add-ins.