-1

I am trying to merge two videos with one another, below is a sample code i am trying to make it work

    FileStream fs = new FileStream(@"C:\Users\test\Downloads\m.mp4", FileMode.Append);
    
    //m1,m2,m3 are video mp4 files on my disk
    var bytes = System.IO.File.ReadAllBytes(@"C:\Users\test\Downloads\m1.mp4");
    fs.Write(bytes,0 ,bytes.Length);
                    
    fs.Close();
    
    FileStream fs1 = new FileStream(@"C:\Users\test\Downloads\m.mp4", FileMode.Append);
    
    //m1,m2 are video mp4 files on my disk
    var bytes1 = System.IO.File.ReadAllBytes(@"C:\Users\test\Downloads\m2.mp4");
    fs1.Write(bytes1,0 ,bytes1.Length);
            
    fs1.Close();

Now what happens is that the new video has the size of the two videos combined but only shows the first video and has the duration of only the first video. How can i fix that?

rambo
  • 21
  • 1
  • 7
  • Does this answer your question? [How can I mix two streams of Video in C#?](https://stackoverflow.com/questions/1332586/how-can-i-mix-two-streams-of-video-in-c) – ndogac Sep 27 '20 at 10:10
  • No it doesnt answer my question ... – rambo Sep 27 '20 at 10:17

2 Answers2

1

The MP4 format should be seen as something like a zip-file: one file as a bag holding multiple other files (like the audio stream and the video stream).

When the file is opened, the header or index is read in order to know which content is to expect. If you concat a second MP4 after the first MP4 content then that content will never be found since it isnt in the first header. This is fundamentaly different then lets say a text-file as then there is only just content.

So what you want is a library that opens up the MP4 and reads it's index and then be able to add content to that package and update the index.

A quick look on google brought me to VisioForge but i'm sure that there are some free libraries to be found as well.

Thomas
  • 258
  • 2
  • 5
1

A few possible solutions:

  1. Check this answer which uses FFmpeg - https://stackoverflow.com/a/41387530/4000335 This can be called from C# Process.Start e.g. this answer https://stackoverflow.com/a/1733516/4000335
  2. NReco VideoConverter FFmpeg wrapper NuGet library (has a free license option), check this answer for concatenate example - https://stackoverflow.com/a/23054427/4000335
kshkarin
  • 574
  • 4
  • 9
  • Thank you very much for the solutions, they work using .net framework, but they dont work with .net standard and xamarin. Is there a solution about using FFmpeg with xamarin forms/ios – rambo Sep 27 '20 at 21:35
  • xamarin/ios requirement wasn't clear from the question, nor from the tags, it's probably possible to use this tutorial - https://www.raywenderlich.com/10857372-how-to-play-record-and-merge-videos-in-ios-and-swift specifically the "Merging Videos" section. Swift code will need to be translated to C# for that to work. Perhaps this SO as well - https://stackoverflow.com/questions/51671353/how-to-merge-mp4-and-mov-type-videos-using-avmutablecomposition-ios-swift – kshkarin Sep 28 '20 at 09:20