question

johnzyd-8697 avatar image
0 Votes"
johnzyd-8697 asked Castorix31 commented

How to use media compositions and editing in WinFrom App project?

Hello:

I found Microsoft has some package for Media Editing, from this URL:
https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/media-compositions-and-editing

However, I need to run a WinForm project to merge some video files. I am using Windows 10 (Version 21H1), my IDE is Visual Studio 2019 (Version 16.11.5)
For example, I have two MP4 files: C:\Videos\1.mp4 and C:\Videos\2.mp4, each of them has the same resolution with its own audio.
I want to merge them into one file: C:\Videos\3.mp4, 1.mp4 at the beginning of the merged video with its own audio, and 2.mp4 being the ending of the merged video with its own audio.
I can use FFMPEG, but it has issue with merged video, the merged video didn’t keep both video and audio, I lost either the first audio or the second video.
Since I have to use SQL Server database, so I need some C# code only in WinForm App, not the UWP project.
Please share some C# code, so I can merge 1.mp4 and 2.mp4 to generate 3.mp4, in which both video and audio inside are kept the same.
Thanks,

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered Castorix31 commented

In Winforms, you must add references to :

C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

then you can access MediaComposition :

140353-mediacomposition.jpg




mediacomposition.jpg (197.0 KiB)
· 14
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello:
I followed your advice, but I got the error:
140494-mediaeditnok.png
It seems to add the reference in VS 2019 as indicated by you not working in my environment. By the way, I have tried to use this in .NET 5.0, but it is not supported. It only support .NET framework 4.5 and not beyond. So I have to use .NET framework 4.5, but for WinForms App, it is still not working.
But to think in a few weeks, we will have .NET 6.0, why you can NOT have some media APIs for .NET 5 to work in WinForms locally? I know there are something may work in Azure. But who wants to work with large video files, but also keep connections with Azure? To work locally is much better, right?


0 Votes 0 ·
mediaeditnok.png (92.3 KiB)

I cannot reproduce your problem

I tested with a blank .NET Framework 4.7.2 project, with similar code in a Button click :

 private MediaComposition composition;
    
 private async void button1_Click(object sender, EventArgs e)
 {
     composition = new MediaComposition();
     await PickFileAndAddClip();
 }
    
 private async Task PickFileAndAddClip()
 {
     string sPath1 = @"E:\Western World - Trailer.mp4";
     var file1 = await Windows.Storage.StorageFile.GetFileFromPathAsync(sPath1);
     var clip1 = await MediaClip.CreateFromFileAsync(file1);
     composition.Clips.Add(clip1);
     string sPath2 = @"E:\small.mp4";
     var file2 = await Windows.Storage.StorageFile.GetFileFromPathAsync(sPath2);
     var clip2 = await MediaClip.CreateFromFileAsync(file2);
     composition.Clips.Add(clip2);
     int nNbClips = composition.Clips.Count;
 }

0 Votes 0 ·

Hello:
It seems your code is working now. But I need some code sample for exporting the merged clips (1.mp4 and 2.mp4) to one MP4 file (i.e: 3.mp4).
By the way, can I get similar code works for .NET 5.0? Or if not possible now, but can we get the stuff working for .NET 6.0 in the near future?
Thanks,

0 Votes 0 ·
Show more comments

I tested with your code and the problem comes from ApplicationData that you must not use here

0 Votes 0 ·

Hello:
Thanks, now, I want to export the merged clips to a new MP4 file (3.mp4), but my code didn’t work.

     private async Task PickFileAndAddClip()
     {
         var file1 = await StorageFile.GetFileFromPathAsync(@"C:\1.mp4");
         var clip1 = await MediaClip.CreateFromFileAsync(file1);
         composition.Clips.Add(clip1);
         var file2 = await StorageFile.GetFileFromPathAsync(@"C:\2.mp4");
         var clip2 = await MediaClip.CreateFromFileAsync(file2);
         composition.Clips.Add(clip2);
         int nNbClips = composition.Clips.Count;
         StorageFile compositionFile = await StorageFile.GetFileFromPathAsync(@"C:\3.mp4");
         var action = composition.SaveAsync(compositionFile);
         await action.AsTask();
     }

I got run time error:
System.IO.FileNotFoundException: 'The system cannot find the file specified.
I know as 3.mp4 is the file going to be generated by the composition?
How I can fix this? Just create an empty file using System.IO function?

0 Votes 0 ·
Show more comments