dotnet / Open-XML-SDK

Open XML SDK by Microsoft
https://www.nuget.org/packages/DocumentFormat.OpenXml/
MIT License
3.99k stars 545 forks source link

Can't Add video or audio to premade presentation #1740

Open patolax opened 2 months ago

patolax commented 2 months ago

Describe the bug Can't Add video or audio to premade presentations

To Reproduce This is my code, I load a PowerPoint presentation from disk and make text changes to the slide after I call these methods to add audio/video.

public void AddVideoToPresentation(PresentationDocument presentationDocument, string audioFilePath)
{
    int PicID = 915;
    PresentationPart presentationPart = presentationDocument.PresentationPart;
    var slideCount = presentationPart.Presentation.SlideIdList.Count();
    var videoEmbedId = string.Format("audioId{0}{1}", slideCount, PicID++);
    var mediaEmbedId = string.Format("medId{0}{1}", slideCount, PicID++);

    SlidePart slidepart = presentationDocument.PresentationPart.SlideParts.LastOrDefault();

    MediaDataPart mediaDataPart1 = presentationDocument.CreateMediaDataPart("audio/mp3", ".mp3");
    System.IO.Stream mediaDataPart1Stream = File.OpenRead(audioFilePath);
    mediaDataPart1.FeedData(mediaDataPart1Stream);
    mediaDataPart1Stream.Close();

    slidepart.AddAudioReferenceRelationship(mediaDataPart1, videoEmbedId);
    slidepart.AddMediaReferenceRelationship(mediaDataPart1, mediaEmbedId);

    slidepart.Slide.Save();

    AddVid(presentationDocument);
}

public void AddVid(PresentationDocument presentationDocument)
{
    PresentationPart presentationPart = presentationDocument.PresentationPart;
    SlidePart slidepart = presentationDocument.PresentationPart.AddNewPart<SlidePart>("rxId3");
    MediaDataPart mediaDataPart1 = presentationDocument.CreateMediaDataPart("video/mp4", ".mp4");
    System.IO.Stream mediaDataPart1Stream = System.IO.File.Open(@"D:\samplevideo.mp4", System.IO.FileMode.Open);
    mediaDataPart1.FeedData(mediaDataPart1Stream);
    mediaDataPart1Stream.Close();

    slidepart.AddMediaReferenceRelationship(mediaDataPart1, "rxId2");
    slidepart.AddVideoReferenceRelationship(mediaDataPart1, "rxId1");
}

Observed behavior No Video or audio embedded in the presentation

Expected behavior Play audio after slide transition

Desktop (please complete the following information):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
    <PackageReference Include="Microsoft.Office.Interop.PowerPoint" Version="15.0.4420.1018" />
    <PackageReference Include="Mscc.GenerativeAI" Version="1.5.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="OpenXmlPowerTools.NetCore" Version="1.1.0" />
    <PackageReference Include="System.ServiceModel.Syndication" Version="8.0.0" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="office">
      <HintPath>..\..\..\..\..\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL</HintPath>
    </Reference>
  </ItemGroup>

</Project>
yswenli commented 1 month ago

How to add video or audio to a pre made presentation?