Aspose.Slides使用教程:使用 C++ 在 PowerPoint 演示文稿中嵌入视频

目录

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

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

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

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

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

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

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

我们将使用Aspose.Slides for C++ API 在 PowerPoint 演示文稿中嵌入视频。它是一个强大且功能丰富的 API,支持创建、阅读和修改 PowerPoint 文件,而无需安装 Mircosoft PowerPoint。

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

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

  1. 首先,创建一个Presentation 类的实例 来表示一个新的 PowerPoint 文件。
  2. 检索要嵌入视频的幻灯片。
  3. 使用Presentation->get_Videos()->AddVideo(System::SharedPtr<System::IO::Stream> 流)方法将视频添加到演示文稿中。
  4. 使用ISlide->get_Shapes()->AddVideoFrame(float x, float y, float width, float height, System::SharedPtr< IVideo > video)方法添加视频帧。
  5. 使用IVideoFrame->set_EmbeddedVideo(System::SharedPtr<IVideo> value)方法将视频嵌入到视频帧中。
  6. 设置视频的播放模式和音量。
  7. 最后,使用 Presentation->Save(System::String fname, Export::SaveFormat format) 方法保存演示文稿。

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

// File paths
const String videoFilePath = u"SourceDirectory\\Video\\Wildlife.mp4";
const String outputFilePath = u"OutputDirectory\\EmbedVideo_out.pptx";

// Create an instance of the Presentation class
SharedPtr<Presentation> presentation = MakeObject<Presentation>();

// Access the first slide
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// Load the video file to stream
System::SharedPtr<System::IO::Stream> stream = System::MakeObject<System::IO::FileStream>(videoFilePath, System::IO::FileMode::Open, System::IO::FileAccess::Read);

// Add the video to the presentation
System::SharedPtr<IVideo> vid = presentation->get_Videos()->AddVideo(stream);

// Add video frame
System::SharedPtr<IVideoFrame> videoFrame = slide->get_Shapes()->AddVideoFrame(50, 150, 300, 150, vid);

// Embed video inside the video frame
videoFrame->set_EmbeddedVideo(vid);

// Set the play mode and volume of the video
videoFrame->set_PlayMode(VideoPlayModePreset::Auto);
videoFrame->set_Volume(AudioVolumeMode::Loud);

// Save the presentation
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

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

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

  1. 首先,创建一个Presentation 类的实例 来表示一个新的 PowerPoint 文件。
  2. 检索要嵌入视频的幻灯片。
  3. 使用ISlide->get_Shapes()->AddVideoFrame(float x, float y, float width, float height, System::String fname)方法添加视频帧。
  4. 设置视频的播放模式。
  5. 最后,使用 Presentation->Save(System::String fname, Export::SaveFormat format) 方法保存演示文稿。

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

// File path
const String outputFilePath = u"OutputDirectory\\EmbedVideoFromWeb_out.pptx";

// Create an instance of the Presentation class
SharedPtr<Presentation> presentation = MakeObject<Presentation>();

// Access the first slide
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// Add the video frame from web source
System::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 video
videoFrame->set_PlayMode(VideoPlayModePreset::Auto);

// Save the presentation
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

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

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

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

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

// File paths
const String sourceFilePath = u"OutputDirectory\\EmbedVideo_out.pptx";
const String outputFilePath = u"OutputDirectory\\ExtractVideoFromSlide_out.";

// Load the presentation file
System::SharedPtr<Presentation> presentation = System::MakeObject<Presentation>(sourceFilePath);
{
// Loop through the slides
auto 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 shapes
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))
{
// Check if the shape is a video frame
if (System::ObjectExt::Is<VideoFrame>(shape))
{
// Extract the video file
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);

// Clearing resources under 'using' statement
//System::Details::DisposeGuard __dispose_guard_0{ stream, ASPOSE_CURRENT_FUNCTION };
// ------------------------------------------
stream->Write(buffer, 0, buffer->get_Length());
}
}
}
}
}

猜你喜欢

转载自blog.csdn.net/Augenstern__zyx/article/details/121015146