>>你可以点击这里下载Aspose.Slides 最新版测试体验。
用于在 PowerPoint 演示文稿中嵌入视频的 C++ API
我们将使用Aspose.Slides for C++ API 在 PowerPoint 演示文稿中嵌入视频。它是一个强大且功能丰富的 API,支持创建、阅读和修改 PowerPoint 文件,而无需安装 Mircosoft PowerPoint。
使用 C++ 在 PowerPoint 演示文稿中嵌入视频
以下是在 PowerPoint 演示文稿中嵌入视频的步骤。
- 首先,创建一个Presentation 类的实例 来表示一个新的 PowerPoint 文件。
- 检索要嵌入视频的幻灯片。
- 使用Presentation->get_Videos()->AddVideo(System::SharedPtr<System::IO::Stream> 流)方法将视频添加到演示文稿中。
- 使用ISlide->get_Shapes()->AddVideoFrame(float x, float y, float width, float height, System::SharedPtr< IVideo > video)方法添加视频帧。
- 使用IVideoFrame->set_EmbeddedVideo(System::SharedPtr<IVideo> value)方法将视频嵌入到视频帧中。
- 设置视频的播放模式和音量。
- 最后,使用 Presentation->Save(System::String fname, Export::SaveFormat format) 方法保存演示文稿。
以下示例代码展示了如何使用 C++ 在 PowerPoint 演示文稿中嵌入视频。
// File pathsconst String videoFilePath = u"SourceDirectory\Video\Wildlife.mp4";const String outputFilePath = u"OutputDirectory\EmbedVideo_out.pptx";// Create an instance of the Presentation classSharedPtr<Presentation> presentation = MakeObject<Presentation>();// Access the first slideSharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);// Load the video file to streamSystem::SharedPtr<System::IO::Stream> stream = System::MakeObject<System::IO::FileStream>(videoFilePath, System::IO::FileMode::Open, System::IO::FileAccess::Read);// Add the video to the presentationSystem::SharedPtr<IVideo> vid = presentation->get_Videos()->AddVideo(stream);// Add video frameSystem::SharedPtr<IVideoFrame> videoFrame = slide->get_Shapes()->AddVideoFrame(50, 150, 300, 150, vid);// Embed video inside the video framevideoFrame->set_EmbeddedVideo(vid);// Set the play mode and volume of the videovideoFrame->set_PlayMode(VideoPlayModePreset::Auto);videoFrame->set_Volume(AudioVolumeMode::Loud);// Save the presentationpresentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
在 PowerPoint 演示文稿中嵌入来自 Web 源的视频
以下是在 PowerPoint 演示文稿中嵌入来自 Web 源的视频的步骤。
- 首先,创建一个Presentation 类的实例 来表示一个新的 PowerPoint 文件。
- 检索要嵌入视频的幻灯片。
- 使用ISlide->get_Shapes()->AddVideoFrame(float x, float y, float width, float height, System::String fname)方法添加视频帧。
- 设置视频的播放模式。
- 最后,使用 Presentation->Save(System::String fname, Export::SaveFormat format) 方法保存演示文稿。
以下示例代码展示了如何使用 C++ 在 PowerPoint 演示文稿中嵌入来自 Web 源的视频。
// File pathconst String outputFilePath = u"OutputDirectory\EmbedVideoFromWeb_out.pptx";// Create an instance of the Presentation classSharedPtr<Presentation> presentation = MakeObject<Presentation>();// Access the first slideSharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);// Add the video frame from web sourceSystem::SharedPtr<IVideoFrame> videoFrame = slide->get_Shapes()->AddVideoFrame(10, 10, 427, 240, u"https://www.youtube.com/embed/sZJorZmHiIk");// Set the play mode and volume of the videovideoFrame->set_PlayMode(VideoPlayModePreset::Auto);// Save the presentationpresentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
使用 C++ 从 PowerPoint 演示文稿中提取视频
以下是使用 C++ 从 PowerPoint 演示文稿中提取视频的步骤。
- 首先,使用Presentation 类加载 PowerPoint 文件 。
- 循环播放演示文稿的幻灯片。
- 循环浏览每张幻灯片中的形状。
- 对于每个形状,检查形状是否为VideoFrame。如果是视频帧,则提取嵌入的视频并保存。
以下示例代码显示了如何使用 C++ 从 PowerPoint 演示文稿中提取视频。
// File pathsconst String sourceFilePath = u"OutputDirectory\EmbedVideo_out.pptx";const String outputFilePath = u"OutputDirectory\ExtractVideoFromSlide_out.";// Load the presentation fileSystem::SharedPtr<Presentation> presentation = System::MakeObject<Presentation>(sourceFilePath);{// Loop through the slidesauto slide_enumerator = (presentation->get_Slides())->GetEnumerator();decltype(slide_enumerator->get_Current()) slide;while (slide_enumerator->MoveNext() && (slide = slide_enumerator->get_Current(), true)){// Loop through the shapesauto shape_enumerator = (presentation->get_Slides()->idx_get(0)->get_Shapes())->GetEnumerator();decltype(shape_enumerator->get_Current()) shape;while (shape_enumerator->MoveNext() && (shape = shape_enumerator->get_Current(), true)){// Check if the shape is a video frameif (System::ObjectExt::Is<VideoFrame>(shape)){// Extract the video fileSystem::SharedPtr<VideoFrame> vf = System::DynamicCast_noexcept<Aspose::Slides::VideoFrame>(shape);System::String type = vf->get_EmbeddedVideo()->get_ContentType();int32_t ss = type.LastIndexOf(L'/');type = type.Remove(0, type.LastIndexOf(L'/') + 1);System::ArrayPtr<uint8_t> buffer = vf->get_EmbeddedVideo()->get_BinaryData();{System::SharedPtr<System::IO::FileStream> stream = System::MakeObject<System::IO::FileStream>(outputFilePath + type, System::IO::FileMode::Create, System::IO::FileAccess::Write, System::IO::FileShare::Read);// Clearing resources under 'using' statement//System::Details::DisposeGuard __dispose_guard_0{ stream, ASPOSE_CURRENT_FUNCTION };// ------------------------------------------stream->Write(buffer, 0, buffer->get_Length());}}}}}
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!