使用 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 免費 PowerPoint 到視頻 轉換器,因為它允許您將演示文稿轉換為具有過渡效果的精美視頻。