翻譯|使用教程|編輯:李顯亮|2021-09-22 09:40:56.430|閱讀 418 次
概述:在本文中,將學習如何以編程方式處理演示文稿中的視頻。特別是,本文將介紹如何使用 C# 在 PowerPoint 演示文稿中嵌入或提取視頻。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
PowerPoint 演示文稿中使用視頻幀來演示某些內容或吸引觀眾。通常,視頻用于節省時間并使演示更有效。在本文中,將學習如何以編程方式處理演示文稿中的視頻。特別是,本文將介紹如何使用 C# 在 PowerPoint 演示文稿中嵌入或提取視頻。
為了在 PowerPoint 演示文稿中嵌入或提取視頻,我們將使用Aspose.Slides for .NET,API 旨在創建和操作 PowerPoint 和 OpenOffice 文檔。
>>你可以點擊這里下載Aspose.Slides 最新版測試體驗。
以下是使用 C# 在 PowerPoint 演示文稿中嵌入視頻的步驟。
以下代碼示例展示了如何使用 C# 在 PowerPoint 演示文稿中嵌入視頻。
// 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); }
還可以在 PowerPoint 演示文稿中嵌入來自 Web 源的視頻。以下是實現此目的的步驟。
以下代碼示例展示了如何將來自 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, "http://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); }
Aspose.Slides for .NET 還允許您從演示文稿中提取視頻。以下是實現此目的的簡單步驟。
以下代碼示例展示了如何使用 C# 從 PowerPoint 演示文稿中提取視頻。
// 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); } } } }
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn