-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't Add video or audio to premade presentation #1740
Comments
How to add video or audio to a pre made presentation? |
Hi @yswenli using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;
using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;
using ShapeTree = DocumentFormat.OpenXml.Presentation.ShapeTree;
using ShapeProperties = DocumentFormat.OpenXml.Presentation.ShapeProperties;
using NonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties;
using NonVisualPictureProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties;
using NonVisualPictureDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties;
using Picture = DocumentFormat.OpenXml.Presentation.Picture;
using BlipFill = DocumentFormat.OpenXml.Presentation.BlipFill;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Office2016.Drawing;
string imgEmbedId = "rId4", embedId = "rId3", mediaEmbedId = "rId2";
string filePath = @"C:\source\TestFiles\MyPresentation.pptx";
string videoFilePath = @"C:\source\TestFiles\video.mp4";
string coverPicPath = @"C:\source\TestFiles\sample.png";
string audioFilePath = @"C:\source\TestFiles\audio.mp3";
using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true))
{
if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null)
{
throw new NullReferenceException("Presenation Part is empty or there are no slides");
}
//Get presentation part
PresentationPart presentationPart = presentationDocument.PresentationPart;
//Get slides ids.
OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements;
int lastSlideIndex = presentationPart.Presentation.SlideIdList.Count() - 1;
//gets relationsipIds of the 2 last slides
string? videoSlidePartRelationshipId = ((SlideId)slidesIds[lastSlideIndex - 1]).RelationshipId;
string? audioSlidePartRelationshipId = ((SlideId)slidesIds[lastSlideIndex]).RelationshipId;
if (videoSlidePartRelationshipId == null || audioSlidePartRelationshipId == null)
{
throw new NullReferenceException("Slides ids not found");
}
//Get slide part by relationshipID
SlidePart? videoSlidepart = (SlidePart)presentationPart.GetPartById(videoSlidePartRelationshipId);
SlidePart? audioSlidepart = (SlidePart)presentationPart.GetPartById(audioSlidePartRelationshipId);
//Adds vide
AddVideo(videoSlidepart, presentationDocument);
//Adds audio
AddAudio(audioSlidepart, presentationDocument);
}
void AddVideo(SlidePart slidePart, PresentationDocument presentationDocument)
{
// Create video Media Data Part (content type, extension)
MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("video/mp4", ".mp4");
//Get the video file and feed the stream
using (Stream mediaDataPartStream = File.OpenRead(videoFilePath))
{
mediaDataPart.FeedData(mediaDataPartStream);
}
//Adds a VideoReferenceRelationship to the MainDocumentPart
slidePart.AddVideoReferenceRelationship(mediaDataPart, embedId);
//Adds a MediaReferenceRelationship to the SlideLayoutPart
slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId);
NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "video" };
A.VideoFromFile videoFromFile = new A.VideoFromFile() { Link = embedId };
ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
appNonVisualDrawingProperties.Append(videoFromFile);
//Adds all the elements to the slide part
AddMediaToTheSlide(slidePart, nonVisualDrawingProperties, appNonVisualDrawingProperties);
}
void AddAudio(SlidePart slidePart, PresentationDocument presentationDocument)
{
// Create video Media Data Part (content type, extension)
MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("audio/mp3", ".mp3");
//Feeds the stream with audio file
using (Stream mediaDataPartStream = File.OpenRead(audioFilePath))
{
mediaDataPart.FeedData(mediaDataPartStream);
}
slidePart.AddAudioReferenceRelationship(mediaDataPart, embedId);
slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId);
NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "audio" };
A.AudioFromFile audioFromFile = new A.AudioFromFile() { Link = embedId };
ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
appNonVisualDrawingProperties.Append(audioFromFile);
AddMediaToTheSlide(slidePart, nonVisualDrawingProperties, appNonVisualDrawingProperties);
}
void AddMediaToTheSlide(SlidePart slidePart, NonVisualDrawingProperties nonVisualDrawingProperties, ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties)
{
//adds sample image to the slide with id to be used as reference in blip
ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgEmbedId);
using (Stream data = File.OpenRead(coverPicPath))
{
imagePart.FeedData(data);
}
if (slidePart!.Slide!.CommonSlideData!.ShapeTree == null)
{
throw new NullReferenceException("Presenation shape tree is empty");
}
//Getting existing shape tree object from PowerPoint
ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;
// specifies the existence of a picture within a presentation.
// It can have non-visual properties, a picture fill as well as shape properties attached to it.
Picture picture = new Picture();
NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties();
//When the hyperlink text is clicked the Action is fetched
A.HyperlinkOnClick hyperlinkOnClick = new A.HyperlinkOnClick() { Id = "", Action = "ppaction://media" };
nonVisualDrawingProperties.Append(hyperlinkOnClick);
NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties();
A.PictureLocks pictureLocks = new A.PictureLocks() { NoChangeAspect = true };
nonVisualPictureDrawingProperties.Append(pictureLocks);
ApplicationNonVisualDrawingPropertiesExtensionList appNonVisualDrawingPropertiesExtensionList = new
ApplicationNonVisualDrawingPropertiesExtensionList();
ApplicationNonVisualDrawingPropertiesExtension appNonVisualDrawingPropertiesExtension = new
ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" };
P14.Media media = new() { Embed = mediaEmbedId };
media.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
appNonVisualDrawingPropertiesExtension.Append(media);
appNonVisualDrawingPropertiesExtensionList.Append(appNonVisualDrawingPropertiesExtension);
applicationNonVisualDrawingProperties.Append(appNonVisualDrawingPropertiesExtensionList);
nonVisualPictureProperties.Append(nonVisualDrawingProperties);
nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
nonVisualPictureProperties.Append(applicationNonVisualDrawingProperties);
//Prepare shape properties to display picture
BlipFill blipFill = new BlipFill();
A.Blip blip = new A.Blip() { Embed = imgEmbedId };
A.Stretch stretch = new A.Stretch();
A.FillRectangle fillRectangle = new A.FillRectangle();
A.Transform2D transform2D = new A.Transform2D();
A.Offset offset = new A.Offset() { X = 1524000L, Y = 857250L };
A.Extents extents = new A.Extents() { Cx = 9144000L, Cy = 5143500L };
A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
A.AdjustValueList adjValueList = new A.AdjustValueList();
stretch.Append(fillRectangle);
blipFill.Append(blip);
blipFill.Append(stretch);
transform2D.Append(offset);
transform2D.Append(extents);
presetGeometry.Append(adjValueList);
ShapeProperties shapeProperties = new ShapeProperties();
shapeProperties.Append(transform2D);
shapeProperties.Append(presetGeometry);
//adds all elements to the slide's shape tree
picture.Append(nonVisualPictureProperties);
picture.Append(blipFill);
picture.Append(shapeProperties);
shapeTree.Append(picture);
} |
Is there some chance to automatically start playing the audio when the slide is presented? |
Hi @steamonimo the auto play could be done with use of timing. Added auto play for both audio and video using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;
using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;
using ShapeTree = DocumentFormat.OpenXml.Presentation.ShapeTree;
using ShapeProperties = DocumentFormat.OpenXml.Presentation.ShapeProperties;
using NonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties;
using NonVisualPictureProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties;
using NonVisualPictureDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties;
using Picture = DocumentFormat.OpenXml.Presentation.Picture;
using BlipFill = DocumentFormat.OpenXml.Presentation.BlipFill;
using DocumentFormat.OpenXml.Packaging;
using ApplicationNonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties;
using Command = DocumentFormat.OpenXml.Presentation.Command;
string imgEmbedId = "rId8", embedId = "rId3", mediaEmbedId = "rId2";
UInt32Value shapeId = 5;
string filePath = @"C:\source\TestFiles\MyPresentation.pptx";
string videoFilePath = @"C:\source\TestFiles\video.mp4";
string coverPicPath = @"C:\source\TestFiles\sample.png";
string audioFilePath = @"C:\source\TestFiles\audio.mp3";
using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true))
{
if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null)
{
throw new NullReferenceException("Presenation Part is empty or there are no slides");
}
//Get presentation part
PresentationPart presentationPart = presentationDocument.PresentationPart;
//Get slides ids.
OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements;
int lastSlideIndex = presentationPart.Presentation.SlideIdList.Count() - 1;
//gets relationsipIds of the 2 last slides
string? videoSlidePartRelationshipId = ((SlideId)slidesIds[lastSlideIndex - 1]).RelationshipId;
string? audioSlidePartRelationshipId = ((SlideId)slidesIds[lastSlideIndex]).RelationshipId;
if (videoSlidePartRelationshipId == null || audioSlidePartRelationshipId == null)
{
throw new NullReferenceException("Slides ids not found");
}
//Get slide part by relationshipID
SlidePart? videoSlidepart = (SlidePart)presentationPart.GetPartById(videoSlidePartRelationshipId);
SlidePart? audioSlidepart = (SlidePart)presentationPart.GetPartById(audioSlidePartRelationshipId);
//Adds vide
AddVideo(videoSlidepart, presentationDocument);
//Adds audio
AddAudio(audioSlidepart, presentationDocument);
}
void AddVideo(SlidePart slidePart, PresentationDocument presentationDocument)
{
// Create video Media Data Part (content type, extension)
MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("video/mp4", ".mp4");
//Get the video file and feed the stream
using (Stream mediaDataPartStream = File.OpenRead(videoFilePath))
{
mediaDataPart.FeedData(mediaDataPartStream);
}
//Adds a VideoReferenceRelationship to the MainDocumentPart
slidePart.AddVideoReferenceRelationship(mediaDataPart, embedId);
//Adds a MediaReferenceRelationship to the SlideLayoutPart
slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId);
NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = shapeId, Name = "video" };
A.VideoFromFile videoFromFile = new A.VideoFromFile() { Link = embedId };
ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
appNonVisualDrawingProperties.Append(videoFromFile);
//Adds all the elements to the slide part
AddMediaToTheSlide(slidePart, nonVisualDrawingProperties, appNonVisualDrawingProperties);
}
void AddAudio(SlidePart slidePart, PresentationDocument presentationDocument)
{
// Create video Media Data Part (content type, extension)
MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("audio/mp3", ".mp3");
//Feeds the stream with audio file
using (Stream mediaDataPartStream = File.OpenRead(audioFilePath))
{
mediaDataPart.FeedData(mediaDataPartStream);
}
slidePart.AddAudioReferenceRelationship(mediaDataPart, embedId);
slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId);
NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = shapeId, Name = "audio" };
A.AudioFromFile audioFromFile = new A.AudioFromFile() { Link = embedId };
ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
appNonVisualDrawingProperties.Append(audioFromFile);
AddMediaToTheSlide(slidePart, nonVisualDrawingProperties, appNonVisualDrawingProperties);
}
void AddMediaToTheSlide(SlidePart slidePart, NonVisualDrawingProperties nonVisualDrawingProperties, ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties)
{
//adds sample image to the slide with id to be used as reference in blip
ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgEmbedId);
using (Stream data = File.OpenRead(coverPicPath))
{
imagePart.FeedData(data);
}
if (slidePart!.Slide!.CommonSlideData!.ShapeTree == null)
{
throw new NullReferenceException("Presenation shape tree is empty");
}
//Getting existing shape tree object from PowerPoint
ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;
// specifies the existence of a picture within a presentation.
// It can have non-visual properties, a picture fill as well as shape properties attached to it.
Picture picture = new Picture();
NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties();
A.HyperlinkOnClick hyperlinkOnClick = new A.HyperlinkOnClick() { Id = "", Action = "ppaction://media" };
nonVisualDrawingProperties.Append(hyperlinkOnClick);
NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties();
A.PictureLocks pictureLocks = new A.PictureLocks() { NoChangeAspect = true };
nonVisualPictureDrawingProperties.Append(pictureLocks);
ApplicationNonVisualDrawingPropertiesExtensionList appNonVisualDrawingPropertiesExtensionList = new ApplicationNonVisualDrawingPropertiesExtensionList();
ApplicationNonVisualDrawingPropertiesExtension appNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" };
P14.Media media = new() { Embed = mediaEmbedId };
media.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
appNonVisualDrawingPropertiesExtension.Append(media);
applicationNonVisualDrawingProperties.Append();
appNonVisualDrawingPropertiesExtensionList.Append(appNonVisualDrawingPropertiesExtension);
applicationNonVisualDrawingProperties.Append(appNonVisualDrawingPropertiesExtensionList);
nonVisualPictureProperties.Append(nonVisualDrawingProperties);
nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
nonVisualPictureProperties.Append(applicationNonVisualDrawingProperties);
//Prepare shape properties to display picture
BlipFill blipFill = new BlipFill();
A.Blip blip = new A.Blip() { Embed = imgEmbedId };
A.Stretch stretch = new A.Stretch();
A.FillRectangle fillRectangle = new A.FillRectangle();
A.Transform2D transform2D = new A.Transform2D();
A.Offset offset = new A.Offset() { X = 1524000L, Y = 857250L };
A.Extents extents = new A.Extents() { Cx = 9144000L, Cy = 5143500L };
A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
A.AdjustValueList adjValueList = new A.AdjustValueList();
stretch.Append(fillRectangle);
blipFill.Append(blip);
blipFill.Append(stretch);
transform2D.Append(offset);
transform2D.Append(extents);
presetGeometry.Append(adjValueList);
ShapeProperties shapeProperties = new ShapeProperties();
shapeProperties.Append(transform2D);
shapeProperties.Append(presetGeometry);
//adds all elements to the slide's shape tree
picture.Append(nonVisualPictureProperties);
picture.Append(blipFill);
picture.Append(shapeProperties);
shapeTree.Append(picture);
//Adding timing to the slide to allow auto play of the video and audio
CommonTimeNode cTn = new CommonTimeNode() { Id = 1, NodeType = TimeNodeValues.TmingRoot, Duration = "indefinite" };
ParallelTimeNode parrarelTimeNode2 = new ParallelTimeNode();
StartConditionList stCondLst = new StartConditionList(new Condition() { Event = TriggerEventValues.OnBegin, Delay = "0" });
//Common time node of type AfterEffect to play automaicly with commnd to play from 0.0
CommonTimeNode cTn2 = new CommonTimeNode(stCondLst) { Id = 2, PresetId = 1, PresetClass = TimeNodePresetClassValues.MediaCall, PresetSubtype = 0, NodeType = TimeNodeValues.AfterEffect, Fill = TimeNodeFillValues.Hold };
ChildTimeNodeList chTnLst2 = new ChildTimeNodeList();
TargetElement targetElement = new TargetElement();
ShapeTarget shapeTarget = new ShapeTarget() { ShapeId = shapeId.ToString() };
Command command = new Command() { Type = CommandValues.Call, CommandName = "playFrom(0.0)" };
CommonBehavior cBhvr = new CommonBehavior(new CommonTimeNode() { Id = 2 });
targetElement.Append(shapeTarget);
cBhvr.Append(targetElement);
command.Append(cBhvr);
chTnLst2.Append(command);
cTn2.Append(chTnLst2);
SequenceTimeNode sequenceTimeNode = new SequenceTimeNode() { Concurrent = true, NextAction = NextActionValues.Seek };
sequenceTimeNode.Append(cTn2);
ChildTimeNodeList chTnLst = new ChildTimeNodeList();
chTnLst.Append(sequenceTimeNode);
cTn.Append(chTnLst);
ParallelTimeNode parrarelTimeNode = new ParallelTimeNode();
parrarelTimeNode.Append(cTn);
TimeNodeList tnLst = new TimeNodeList();
tnLst.Append(parrarelTimeNode);
Timing timing = new Timing();
timing.Append(tnLst);
Slide slide = slidePart.Slide;
slide.Append(timing);
} Thanks |
Thank you very much! But there is one catch with your code. It saves the media at root\media in the resulting ZIP. However, if you save the resulting file in PowerPoint it takes the mp3 from root\media and copies them to root\ppt\media and then there is still an old copy remaining in root\media. In the copy process PowerPoint renames all relation IDs that pointed to the previous location. If you then reopen this file again with your code it will again save to root\media. So far this does not cause issues but after operating multiple times on the PowerPoint file it will contain multiple copies of the same MP3 file. |
Hi @steamonimo I see that PPT removes old mp3/mp4 files whenever saving directly from PPT. |
We consider this as answered. Closing this issue |
Three problems still remain:
|
Hi @steamonimo thank you for your input on this. |
@mkaszewiak: I contacted you via mail... |
Hi @steamonimo I didn't receive any email from you. |
Thanks for letting me know. I tried again without attachments... |
@steamonimo I have it now. Will update you once will have an update on it. Thanks |
Hi @steamonimo I see the working file you have provided in the email narrate_working_EN.pptx file has errors related to the slide transition and this is causing the issue later when you save in PPT. |
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.
Observed behavior
No Video or audio embedded in the presentation
Expected behavior
Play audio after slide transition
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: