Find video relationships in OpenXML

So, I write a pptx parser and use OpenXML to load data. Everything is going well (it’s a lie - I’m actually ready to throw the computer around the room and jump out of the window), but I had a problem downloading the video, which I just can’t understand. The problem is that OpenXML doesn't seem to be able to find the relationship tag that indicates the video URI.

What I did was written code to cycle through parts of a slide and exit from their identifiers, for example:

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

Thus, this works great - it registers all the relationships specified in the slide.xml.rels file, except for the video relationships for the corresponding file. I see the video relationship in the rels file and it matches the link identifier of the videoFile tag on the slide, but I cannot figure out how to get it through the code. I have downloaded image loading (OpenXML can find image relationships). Are there different ways to relate to a video than other relationships? How can I get a video on a URI?

+4
source share
1 answer

Video separators are stored in the ExternalReleationships collection at SlidePart.

Powerpoint embeds the video (external file) in the presentation as follows (simplified):

  • It creates the p: video (class Video ) tag in the p: timing (class Timing ) tag for the slide containing the video.
  • The p: video tag contains a child of p: cMediaNode (the CommonMediaNode class).

  • p: cMediaNode contains a child of p: tgtEl (class TargetElement ).

  • Again, p: cMediaNode contains a child p: spTgt (class ShapeTarget ) that points to the identifier of the shape of the image released on the video. The image identifier form is stored in the NonVisualDrawingProperties Id element. Thus, the video is associated with the image form through these identifiers.

  • In addition, the image form contains a child element called: videoFile (class VideoFromFile ). The VideoFromFile class has a member named Link that points to an external free identifier.

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

The following code lists all the videos for all the slides in this presentation. For each video, Uri is printed to an external file. This is done by finding the issued external exemption for a 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 can also directly list a: videoFile tags (class VideoFromFile ). See 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); } } } 
+3
source

Source: https://habr.com/ru/post/1435163/


All Articles