PPT处理控件Aspose.Slides功能演示:使用 C# 在 PowerPoint 演示文稿中嵌入视频

在本文中,将学习如何以编程方式处理演示文稿中的视频。特别是,本文将介绍如何使用 C# 在 PowerPoint 演示文稿中嵌入或提取视频。

PowerPoint 演示文稿中使用视频帧来演示某些内容或吸引观众。通常,视频用于节省时间并使演示更有效。在本文中,将学习如何以编程方式处理演示文稿中的视频。特别是,本文将介绍如何使用 C# 在 PowerPoint 演示文稿中嵌入或提取视频。

  • 使用 C# 在 PowerPoint 演示文稿中嵌入视频
  • 嵌入来自 Web 源的视频
  • 从 PowerPoint 演示文稿中提取视频

为了在 PowerPoint 演示文稿中嵌入或提取视频,我们将使用Aspose.Slides for .NET,API 旨在创建和操作 PowerPoint 和 OpenOffice 文档。

>>你可以点击这里下载Aspose.Slides 最新版测试体验。

使用 C# 在 PowerPoint 演示文稿中嵌入视频

以下是使用 C# 在 PowerPoint 演示文稿中嵌入视频的步骤。

  • 首先,加载 PowerPoint 文件或使用Presentation类创建一个新文件。
  • 然后,在ISlide对象中获取所需幻灯片的引用。
  • 使用Presentation.Videos.AddVideo()方法将新视频添加到演示文稿的视频集合中,并将其引用添加到IVideo对象中。
  • 使用ISlide.Shapes.AddVideoFrame(single, single, single, single, IVideo)方法在幻灯片中添加新的视频帧。
  • 将视频帧引用到IVideoFrame对象中。
  • 设置视频的播放模式和音量。
  • 最后,使用Presentation.Save(String, SaveFormat)方法保存演示文稿。

以下代码示例展示了如何使用 C# 在 PowerPoint 演示文稿中嵌入视频。

// Instantiate Presentation class that represents the PPTXusing (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);}

在来自 Web 源的演示文稿中嵌入视频

还可以在 PowerPoint 演示文稿中嵌入来自 Web 源的视频。以下是实现此目的的步骤。

  • 首先,加载 PowerPoint 文件或使用Presentation类创建一个新文件。
  • 然后,在ISlide对象中获取所需幻灯片的引用。
  • 通过在ISlide.Shapes.AddVideoFrame(single, single, single, single, String)方法中指定视频的 URL,在幻灯片中添加新的视频帧。
  • 将视频帧引用到IVideoFrame对象中。
  • 设置视频的播放模式和音量。
  • 使用WebClient设置视频的缩略图。
  • 使用Presentation.Save(String, SaveFormat)方法保存演示文稿。

以下代码示例展示了如何将来自 Web 源的视频嵌入到演示文稿中。

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);}

从 C# 中的 PowerPoint 演示文稿中提取视频

Aspose.Slides for .NET 还允许您从演示文稿中提取视频。以下是实现此目的的简单步骤。

  • 首先,使用Presentation类加载 PowerPoint 文件。
  • 然后,通过每个回路I幻灯片式的Presentation.Slides集合。
  • 对于每个I幻灯片式对象,遍历集合IShape的在里面。
  • 如果IShape是VideoFrame,则提取并保存嵌入的视频。

以下代码示例展示了如何使用 C# 从 PowerPoint 演示文稿中提取视频。

// Load a presentation filePresentation presentation = new Presentation("Video.pptx");// Loop through slides in the presentationforeach (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);            }        }    }}

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年8月19日
下一篇 2021年8月19日

相关推荐

发表回复

登录后才能评论