Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locate video relationships in OpenXML

Tags:

video

openxml

So I'm writing a pptx parser and using OpenXML to get the data loaded in. It's all going pretty well (that's a lie - I'm actually ready to throw the computer across the room and jump out of the window), but I've run into an issue with loading videos that I simply can't figure out. The problem is that OpenXML doesn't seem to be able to locate the relationship tag that specifies video URIs.

What I've done is written code to cycle through the parts in the slide and log out their Ids, like so:

SlidePart slidePart = ...;
foreach(var curPart in slidePart.Parts)
  Console.WriteLine("Part ID: " + curPart.RelationshipId);

So that works great - it logs out all of the relationships specified in the slide.xml.rels file - except for the video relationships for the relevant file. I can see the video relationship in the rels file, and it matches the link ID of the videoFile tag in the slide, but I can't figure out how to get at it through code. I've got image loading working fine (OpenXML can find the image relationships). Are video relationships treated differently than other relationships? How can I get at the video URIs?

like image 311
Lincoln Green Avatar asked Oct 07 '22 14:10

Lincoln Green


1 Answers

Video releationships are stored in the ExternalReleationships collection of a SlidePart.

Powerpoint embeds a video (external file) into a presentation in the following way (simplified):

  1. It creates a p:video (class Video) tag within a p:timing (class Timing) tag for the slide containing the video.
  2. The p:video tag contains a child called p:cMediaNode (class CommonMediaNode).

  3. The p:cMediaNode contains a child called p:tgtEl (class TargetElement).

  4. Again, the p:cMediaNode contains a child called p:spTgt (class ShapeTarget) which points to the ID of the picture shape releated to the video. The ID of the picture shape is stored in the NonVisualDrawingProperties Id member. So, the video is connected to the picture shape via these Ids.

  5. Furthermore, the picture shape contains a child called a:videoFile (class VideoFromFile). The class VideoFromFile has a member called Link which points to the Id of the external releationship.

I highy recommend you to download the OpenXML SDK 2.0 productivity tool. This tool allows you to inspect the generated XML of your presentation file.

The following code enumerates all videos for all slides in a given presentation. For each video the Uri to the external file is printed. This is done by finding the releated external releationship for the given video.

using (var doc = PresentationDocument.Open("c:\\temp\\presentation.pptx", false))
{
  var presentation = doc.PresentationPart.Presentation;

  foreach (SlideId slideId in presentation.SlideIdList)
  {
    SlidePart slidePart = doc.PresentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
    if (slidePart == null || slidePart.Slide == null)
    {
      continue;
    }

    Slide slide = slidePart.Slide;

    var videos = slide.Descendants<Video>();

    Console.Out.WriteLine("Found videos for slide ID: {0}", slideId.Id);
    foreach (Video video in videos)
    {
      ShapeTarget shapeTarget = video.Descendants<ShapeTarget>().FirstOrDefault();

      Console.Out.WriteLine("ShapeTargetId = {0}", shapeTarget.ShapeId);

      var videoFromFile = slide.CommonSlideData.ShapeTree.Descendants<Picture>().
                Where<Picture>(p => p.NonVisualPictureProperties.Descendants<NonVisualDrawingProperties>().FirstOrDefault().Id == shapeTarget.ShapeId).
                FirstOrDefault().Descendants<VideoFromFile>().FirstOrDefault();                

      Console.Out.WriteLine("Releationship ID: {0}", videoFromFile.Link);

      var externalReleationship = 
                slidePart.ExternalRelationships.Where(er => er.Id == videoFromFile.Link).FirstOrDefault();

      if(externalReleationship == null) // Then it must be embedded
      {
         ReferenceRelationship rr = slidePart.GetReferenceRelationship(videoFromFile.Link);
         if (rr != null)
         {
           Console.Out.WriteLine(rr.Uri.OriginalString);
         }
      }
      else
      {
        Console.Out.WriteLine("Path to video file: {0}", externalReleationship.Uri.AbsolutePath);
      }
    }
  }
}

Of course, you could also directly enumerate the a:videoFile (class VideoFromFile) tags. See the code below.

foreach (SlideId slideId in presentation.SlideIdList)
{
  SlidePart slidePart = doc.PresentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
  if (slidePart == null || slidePart.Slide == null)
  {
    continue;
  }

  Slide slide = slidePart.Slide;

  var videos = slide.CommonSlideData.ShapeTree.Descendants<VideoFromFile>();

  foreach (VideoFromFile video in videos)
  {                                
    Console.Out.WriteLine("Releationship ID: {0}", video.Link);

    var externalReleationship =
           slidePart.ExternalRelationships.Where(er => er.Id == video.Link).FirstOrDefault();

    if(externalReleationship == null) 
    {
        ReferenceRelationship rr = slidePart.GetReferenceRelationship(videoFromFile.Link);
        if (rr != null)
        {
          Console.Out.WriteLine(rr.Uri.OriginalString);
        }
    }
    else
    {
      Console.Out.WriteLine("Path to video file: {0}", externalReleationship.Uri.AbsolutePath);
    }
  }
}
like image 151
Hans Avatar answered Oct 11 '22 02:10

Hans