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

Microsoft PowerPoint 使您能够将视频帧添加到 PowerPoint 演示文稿中。视频可用于提高演示文稿的质量,并有助于更好地向观众传达信息。在某些情况下,您可能希望以编程方式将视频添加到 PowerPoint 演示文稿中。为此,本文将教您如何使用 C++ 在 PowerPoint 演示文稿中嵌入视频。

用于在 PowerPoint 演示文稿中嵌入视频的 C++ API

我们将使用 Aspose.Slides for C++ API 在 PowerPoint 演示文稿中嵌入视频。它是一个功能强大且功能丰富的 API,无需安装 Mircosoft PowerPoint 即可支持创建、读取和修改 PowerPoint 文件。您可以通过 NuGet 安装 API,也可以直接从 下载 部分下载。

PM> Install-Package Aspose.Slides.Cpp

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

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

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

// 文件路径
const String videoFilePath = u"SourceDirectory\\Video\\Wildlife.mp4";
const String outputFilePath = u"OutputDirectory\\EmbedVideo_out.pptx";

// 创建一个 Presentation 类的实例
SharedPtr<Presentation> presentation = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 加载视频文件以进行流式传输
System::SharedPtr<System::IO::Stream> stream = System::MakeObject<System::IO::FileStream>(videoFilePath, System::IO::FileMode::Open, System::IO::FileAccess::Read);

// 将视频添加到演示文稿
System::SharedPtr<IVideo> vid = presentation->get_Videos()->AddVideo(stream);

// 添加视频帧
System::SharedPtr<IVideoFrame> videoFrame = slide->get_Shapes()->AddVideoFrame(50, 150, 300, 150, vid);

// 在视频帧内嵌入视频
videoFrame->set_EmbeddedVideo(vid);

// 设置视频的播放模式和音量
videoFrame->set_PlayMode(VideoPlayModePreset::Auto);
videoFrame->set_Volume(AudioVolumeMode::Loud);

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

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

以下是在 PowerPoint 演示文稿中嵌入来自 Web 源的视频的步骤。

以下示例代码展示了如何使用 C++ 在 PowerPoint 演示文稿中嵌入来自 Web 源的视频。

// 文件路径
const String outputFilePath = u"OutputDirectory\\EmbedVideoFromWeb_out.pptx";

// 创建一个 Presentation 类的实例
SharedPtr<Presentation> presentation = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 从网络源添加视频帧
System::SharedPtr<IVideoFrame> videoFrame = slide->get_Shapes()->AddVideoFrame(10, 10, 427, 240, u"https://www.youtube.com/embed/sZJorZmHiIk");

// 设置视频的播放模式和音量
videoFrame->set_PlayMode(VideoPlayModePreset::Auto);

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

使用 C++ 从 PowerPoint 演示文稿中提取视频

以下是使用 C++ 从 PowerPoint 演示文稿中提取视频的步骤。

  • 首先,使用 Presentation 类加载 PowerPoint 文件。
  • 循环播放演示文稿的幻灯片。
  • 循环浏览每张幻灯片中的形状。
  • 对于每个形状,检查形状是否为 VideoFrame。如果是视频帧,请提取嵌入的视频并保存。

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

// 文件路径
const String sourceFilePath = u"OutputDirectory\\EmbedVideo_out.pptx";
const String outputFilePath = u"OutputDirectory\\ExtractVideoFromSlide_out.";

// 加载演示文件
System::SharedPtr<Presentation> presentation = System::MakeObject<Presentation>(sourceFilePath);
{
	// 循环播放幻灯片
	auto slide_enumerator = (presentation->get_Slides())->GetEnumerator();
	decltype(slide_enumerator->get_Current()) slide;
	while (slide_enumerator->MoveNext() && (slide = slide_enumerator->get_Current(), true))
	{
		// 循环遍历形状
		auto 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))
		{
			// 检查形状是否为视频帧
			if (System::ObjectExt::Is<VideoFrame>(shape))
			{
				// 提取视频文件
				System::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);

					// 在“使用”语句下清除资源
					//System::Details::DisposeGuard __dispose_guard_0{ stream, ASPOSE_CURRENT_FUNCTION };
					// ------------------------------------------
					stream->Write(buffer, 0, buffer->get_Length());
				}
			}
		}
	}
}

获得免费许可证

为了在没有评估限制的情况下试用 API,您可以申请 免费的临时许可证

结论

在本文中,您学习了如何使用 C++ 在 PowerPoint 演示文稿中嵌入视频。此外,您还了解了如何使用 Aspose.Slides for C++ API 从 PowerPoint 演示文稿中提取嵌入视频。它是一个强大的 API,为处理 PowerPoint 文件提供了许多附加功能。您可以通过访问 官方文档 来详细探索 API。如有任何问题,请随时通过我们的 免费支持论坛 与我们联系。

也可以看看

提示:您可能需要查看 Aspose FREE PowerPoint 到视频 转换器,因为它允许您将演示文稿转换为具有过渡效果的令人惊叹的视频。