แปลงงานนำเสนอ PowerPoint เป็นวิดีโอใน C++

บทนำ

โดยการแปลงงานนำเสนอ PowerPoint ของคุณเป็นวิดีโอ คุณจะได้

  • เพิ่มความเข้าถึงได้: ทุกอุปกรณ์ (ไม่ว่าจะเป็นแพลตฟอร์มใด) มีโปรแกรมเล่นวิดีโอเป็นค่าเริ่มต้นเมื่อเทียบกับแอปพลิเคชันเปิดงานนำเสนอ ทำให้ผู้ใช้เปิดหรือเล่นวิดีโอได้ง่ายขึ้น
  • เข้าถึงได้มากขึ้น: ผ่านวิดีโอ คุณสามารถเข้าถึงผู้ชมจำนวนมากและนำเสนอข้อมูลที่อาจดูน่าเบื่อในงานนำเสนอ ตามการสำรวจและสถิติส่วนใหญ่ ผู้คนมักดูและบริโภควิดีโอมากกว่ารูปแบบเนื้อหาอื่น ๆ และโดยทั่วไปพวกเขาชอบเนื้อหานั้น

ใน Aspose.Slides 22.11, เราเพิ่มการสนับสนุนการแปลงงานนำเสนอเป็นวิดีโอ

  • ใช้ Aspose.Slides เพื่อสร้างชุดเฟรม (จากสไลด์งานนำเสนอ) ที่สอดคล้องกับ FPS (เฟรมต่อวินาที) ที่ระบุ
  • ใช้เครื่องมือของบุคคลที่สามเช่น ffmpeg เพื่อสร้างวิดีโอตามเฟรมเหล่านั้น

แปลงงานนำเสนอ PowerPoint เป็นวิดีโอ

  1. ดาวน์โหลด ffmpeg here.
  2. เพิ่มพาธไปยัง ffmpeg.exe ในตัวแปรสภาพแวดล้อม PATH.
  3. รันโค้ดแปลง PowerPoint เป็นวิดีโอ

โค้ด C++ นี้แสดงวิธีแปลงงานนำเสนอ (ที่มีรูปภาพและสองเอฟเฟกต์แอนิเมชัน) เป็นวิดีโอ:

void OnFrameTick(System::SharedPtr<PresentationPlayer> sender, System::SharedPtr<FrameTickEventArgs> args)
{
    System::String fileName = System::String::Format(u"frame_{0}.png", sender->get_FrameIndex());
    args->GetFrame()->Save(fileName);
}

void Run()
{
    auto presentation = System::MakeObject<Presentation>();
    auto slide = presentation->get_Slide(0);

    // เพิ่มรูปแบบรอยยิ้มแล้วทำแอนิเมชันให้
    System::SharedPtr<IAutoShape> smile = slide->get_Shapes()->AddAutoShape(ShapeType::SmileyFace, 110.0f, 20.0f, 500.0f, 500.0f);
    auto sequence = slide->get_Timeline()->get_MainSequence();
    System::SharedPtr<IEffect> effectIn = sequence->AddEffect(smile, EffectType::Fly, EffectSubtype::TopLeft, EffectTriggerType::AfterPrevious);
    System::SharedPtr<IEffect> effectOut = sequence->AddEffect(smile, EffectType::Fly, EffectSubtype::BottomRight, EffectTriggerType::AfterPrevious);
    effectIn->get_Timing()->set_Duration(2.0f);
    effectOut->set_PresetClassType(EffectPresetClassType::Exit);

    const int32_t fps = 33;

    auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
    auto player = System::MakeObject<PresentationPlayer>(animationsGenerator, fps);
    player->FrameTick += OnFrameTick;
    animationsGenerator->Run(presentation->get_Slides());

    const System::String ffmpegParameters = System::String::Format(
        u"-loglevel {0} -framerate {1} -i {2} -y -c:v {3} -pix_fmt {4} {5}",
        u"warning", m_fps, "frame_%d.png", u"libx264", u"yuv420p", "video.mp4");
    auto ffmpegProcess = System::Diagnostics::Process::Start(u"ffmpeg", ffmpegParameters);
    ffmpegProcess->WaitForExit();
}

เอฟเฟกต์วิดีโอ

คุณสามารถใส่แอนิเมชันให้กับวัตถุบนสไลด์และใช้การเปลี่ยนผ่านระหว่างสไลด์ได้

แอนิเมชันและการเปลี่ยนผ่านทำให้การแสดงสไลด์มีชีวิตชีวาและน่าสนใจ—และทำเช่นเดียวกันกับวิดีโอ มาเพิ่มสไลด์และการเปลี่ยนผ่านอีกหนึ่งสไลด์ลงในโค้ดของงานนำเสนอก่อนหน้า:

// เพิ่มรูปแบบรอยยิ้มและทำแอนิเมชันให้

// ...

// เพิ่มสไลด์ใหม่และการเปลี่ยนผ่านแบบแอนิเมชัน

System::SharedPtr<ISlide> newSlide = presentation->get_Slides()->AddEmptySlide(presentation->get_Slide(0)->get_LayoutSlide());

System::SharedPtr<IBackground> slideBackground = newSlide->get_Background();

slideBackground->set_Type(BackgroundType::OwnBackground);

auto fillFormat = slideBackground->get_FillFormat();

fillFormat->set_FillType(FillType::Solid);

fillFormat->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Indigo());

newSlide->get_SlideShowTransition()->set_Type(TransitionType::Push);

Aspose.Slides ยังสนับสนุนแอนิเมชันสำหรับข้อความด้วย เราจึงแอนิเมทย่อหน้าบนวัตถุให้ปรากฏต่อเนื่องกัน (โดยตั้งค่าหน่วงเวลาเป็นหนึ่งวินาที):

void OnFrameTick(System::SharedPtr<PresentationPlayer> sender, System::SharedPtr<FrameTickEventArgs> args)
{
    System::String fileName = System::String::Format(u"frame_{0}.png", sender->get_FrameIndex());
    args->GetFrame()->Save(fileName);
}

void Run()
{
    auto presentation = System::MakeObject<Presentation>();
    auto slide = presentation->get_Slide(0);

    // เพิ่มข้อความและแอนิเมชัน
    System::SharedPtr<IAutoShape> autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 210.0f, 120.0f, 300.0f, 300.0f);
    System::SharedPtr<Paragraph> para1 = System::MakeObject<Paragraph>();
    para1->get_Portions()->Add(System::MakeObject<Portion>(u"Aspose Slides for C++"));
    System::SharedPtr<Paragraph> para2 = System::MakeObject<Paragraph>();
    para2->get_Portions()->Add(System::MakeObject<Portion>(u"convert PowerPoint Presentation with text to video"));

    System::SharedPtr<Paragraph> para3 = System::MakeObject<Paragraph>();
    para3->get_Portions()->Add(System::MakeObject<Portion>(u"paragraph by paragraph"));
    auto paragraphs = autoShape->get_TextFrame()->get_Paragraphs();
    paragraphs->Add(para1);
    paragraphs->Add(para2);
    paragraphs->Add(para3);
    paragraphs->Add(System::MakeObject<Paragraph>());

    auto sequence = slide->get_Timeline()->get_MainSequence();
    System::SharedPtr<IEffect> effect = sequence->AddEffect(para1, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    System::SharedPtr<IEffect> effect2 = sequence->AddEffect(para2, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    System::SharedPtr<IEffect> effect3 = sequence->AddEffect(para3, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    System::SharedPtr<IEffect> effect4 = sequence->AddEffect(para3, EffectType::Appear, EffectSubtype::None, EffectTriggerType::AfterPrevious);

    effect->get_Timing()->set_TriggerDelayTime(1.0f);
    effect2->get_Timing()->set_TriggerDelayTime(1.0f);
    effect3->get_Timing()->set_TriggerDelayTime(1.0f);
    effect4->get_Timing()->set_TriggerDelayTime(1.0f);

    // แปลงเฟรมเป็นวิดีโอ
    const int32_t fps = 33;

    auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
    auto player = System::MakeObject<PresentationPlayer>(animationsGenerator, fps);
    
    player->FrameTick += OnFrameTick;
    animationsGenerator->Run(presentation->get_Slides());

    const System::String ffmpegParameters = System::String::Format(
        u"-loglevel {0} -framerate {1} -i {2} -y -c:v {3} -pix_fmt {4} {5}",
        u"warning", m_fps, "frame_%d.png", u"libx264", u"yuv420p", "video.mp4");
    auto ffmpegProcess = System::Diagnostics::Process::Start(u"ffmpeg", ffmpegParameters);
    ffmpegProcess->WaitForExit();
}

คลาสสำหรับการแปลงวิดีโอ

เพื่อให้คุณทำงานแปลง PowerPoint เป็นวิดีโอได้, Aspose.Slides มีคลาส PresentationAnimationsGenerator และ PresentationPlayer ให้ใช้

PresentationAnimationsGenerator ให้คุณตั้งขนาดเฟรมของวิดีโอ (ที่จะสร้างต่อไป) ผ่านคอนสตรัคเตอร์ของมัน หากคุณส่งอินสแตนซ์ของงานนำเสนอ, Presentation.SlideSize จะถูกใช้และมันจะสร้างแอนิเมชันที่ PresentationPlayer ใช้

เมื่อแอนิเมชันถูกสร้าง, เหตุการณ์ NewAnimation จะถูกสร้างสำหรับแต่ละแอนิเมชันต่อเนื่อง, ซึ่งมีพารามิเตอร์ประเภท IPresentationAnimationPlayer ตัวหลังเป็นคลาสที่แทนผู้เล่นสำหรับแอนิเมชันแยกต่างหาก

เพื่อทำงานกับ IPresentationAnimationPlayer, ใช้คุณสมบัติ get_Duration (ระยะเวลาทั้งหมดของแอนิเมชัน) และเมธอด SetTimePosition แต่ละตำแหน่งแอนิเมชันจะถูกตั้งค่าในช่วง 0 ถึง duration จากนั้นเมธอด GetFrame จะคืนค่า Bitmap ที่สอดคล้องกับสถานะแอนิเมชันในขณะนั้น

void OnNewAnimation(System::SharedPtr<IPresentationAnimationPlayer> animationPlayer)
{
    System::Console::WriteLine(u"Total animation duration: {0}", animationPlayer->get_Duration());

    animationPlayer->SetTimePosition(0);
    // สถานะเริ่มต้นของแอนิเมชัน
    System::SharedPtr<System::Drawing::Bitmap> bitmap = animationPlayer->GetFrame();
    // บิทแมปสถานะเริ่มต้นของแอนิเมชัน

    animationPlayer->SetTimePosition(animationPlayer->get_Duration());
    // สถานะสุดท้ายของแอนิเมชัน
    System::SharedPtr<System::Drawing::Bitmap> lastBitmap = animationPlayer->GetFrame();
    // เฟรมสุดท้ายของแอนิเมชัน
    lastBitmap->Save(u"last.png");
}

void Run()
{
    auto presentation = System::MakeObject<Presentation>();
    auto slide = presentation->get_Slide(0);

    // เพิ่มรูปแบบรอยยิ้มและทำแอนิเมชันให้
    System::SharedPtr<IAutoShape> smile = slide->get_Shapes()->AddAutoShape(ShapeType::SmileyFace, 110.0f, 20.0f, 500.0f, 500.0f);
    auto sequence = slide->get_Timeline()->get_MainSequence();
    System::SharedPtr<IEffect> effectIn = sequence->AddEffect(smile, EffectType::Fly, EffectSubtype::TopLeft, EffectTriggerType::AfterPrevious);
    System::SharedPtr<IEffect> effectOut = sequence->AddEffect(smile, EffectType::Fly, EffectSubtype::BottomRight, EffectTriggerType::AfterPrevious);
    effectIn->get_Timing()->set_Duration(2.0f);
    effectOut->set_PresetClassType(EffectPresetClassType::Exit);

    auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
    animationsGenerator->NewAnimation += OnNewAnimation;
}

เพื่อให้แอนิเมชันทั้งหมดในงานนำเสนอเล่นพร้อมกัน, ใช้คลาส PresentationPlayer คลาสนี้รับอินสแตนซ์ของ PresentationAnimationsGenerator และ FPS สำหรับเอฟเฟกต์ในคอนสตรัคเตอร์ แล้วเรียกเหตุการณ์ FrameTick สำหรับแอนิเมชันทั้งหมดเพื่อให้เล่น:

void OnFrameTick(System::SharedPtr<PresentationPlayer> sender, System::SharedPtr<FrameTickEventArgs> args)
{
    System::String fileName = System::String::Format(u"frame_{0}.png", sender->get_FrameIndex());
    args->GetFrame()->Save(fileName);
}

void Run()
{
    auto presentation = System::MakeObject<Presentation>(u"animated.pptx");
    auto animationsGenerator = System::MakeObject<PresentationAnimationsGenerator>(presentation);
    auto player = System::MakeObject<PresentationPlayer>(animationsGenerator, 33);

    player->FrameTick += OnFrameTick;
    animationsGenerator->Run(presentation->get_Slides());
}

จากนั้นเฟรมที่สร้างจะถูกคอมไพล์เพื่อผลิตวิดีโอ ดูส่วน แปลง PowerPoint เป็นวิดีโอ เพื่อรายละเอียดเพิ่มเติม

แอนิเมชันและเอฟเฟกต์ที่สนับสนุน

Entrance:

Animation Type Aspose.Slides PowerPoint
Appear not supported supported
Fade supported supported
Fly In supported supported
Float In supported supported
Split supported supported
Wipe supported supported
Shape supported supported
Wheel supported supported
Random Bars supported supported
Grow & Turn not supported supported
Zoom supported supported
Swivel supported supported
Bounce supported supported

Emphasis:

Animation Type Aspose.Slides PowerPoint
Pulse not supported supported
Color Pulse not supported supported
Teeter supported supported
Spin supported supported
Grow/Shrink not supported supported
Desaturate not supported supported
Darken not supported supported
Lighten not supported supported
Transparency not supported supported
Object Color not supported supported
Complementary Color not supported supported
Line Color not supported supported
Fill Color not supported supported

Exit:

Animation Type Aspose.Slides PowerPoint
Disappear not supported supported
Fade supported supported
Fly Out supported supported
Float Out supported supported
Split supported supported
Wipe supported supported
Shape supported supported
Random Bars supported supported
Shrink & Turn not supported supported
Zoom supported supported
Swivel supported supported
Bounce supported supported

Motion Paths:

Animation Type Aspose.Slides PowerPoint
Lines supported supported
Arcs supported supported
Turns supported supported
Shapes supported supported
Loops supported supported
Custom Path supported supported

FAQ

สามารถแปลงงานนำเสนอที่มีการตั้งรหัสผ่านได้หรือไม่?

ใช่, Aspose.Slides รองรับการทำงานกับ password‑protected presentations. เมื่อประมวลผลไฟล์ดังกล่าว คุณต้องใส่รหัสผ่านที่ถูกต้องเพื่อให้ไลบรารีเข้าถึงเนื้อหาของงานนำเสนอได้

Aspose.Slides รองรับการใช้งานในโซลูชันคลาวด์หรือไม่?

ใช่, Aspose.Slides สามารถผสานรวมกับแอปพลิเคชันและบริการบนคลาวด์ได้ ไลบรารีออกแบบมาสำหรับสภาพแวดล้อมเซิร์ฟเวอร์ เพื่อให้ได้ประสิทธิภาพสูงและขยายตัวได้ดีสำหรับการประมวลผลไฟล์เป็นชุด

มีข้อจำกัดขนาดของงานนำเสนอระหว่างการแปลงหรือไม่?

Aspose.Slides สามารถจัดการงานนำเสนอขนาดใด ๆ ก็ได้ อย่างไรก็ตาม เมื่อทำงานกับไฟล์ที่มีขนาดใหญ่มาก อาจต้องการทรัพยากรระบบเพิ่มเติม และบางครั้งแนะนำให้ทำการปรับขนาดงานนำเสนอเพื่อเพิ่มประสิทธิภาพ