SharpAvi.Output.AviWriter to stream

Dec 13, 2016 at 5:44 PM
Hi,

I tried to create a video with video frames and audio frames.
I used this constructor SharpAvi.Output.AviWriter writer = new SharpAvi.Output.AviWriter(@"test.avi")
Everything works well!!! Even If I use a FileStream.

But now i don´t want to store the data to disc. I tried to use a memorystream
 var videoOutputStream = new MemoryStream();
 SharpAvi.Output.AviWriter writer = new SharpAvi.Output.AviWriter(videoOutputStream)
Adding the frames is not a problem.
But when I save my stream to disc, the file will not be opened by the media player.

I have no idea...
Can anybody help me?

Thanks in advance!!
Martin
Dec 14, 2016 at 12:16 PM
Further Investigation results:
I used this constructor SharpAvi.Output.AviWriter writer = new SharpAvi.Output.AviWriter(@"test.avi")
This code creates a file with with a size of 29791448 Bytes.

 var videoOutputStream = new MemoryStream();
 SharpAvi.Output.AviWriter writer = new SharpAvi.Output.AviWriter(videoOutputStream)
....
 FileStream file = new FileStream("test_fromMemomryStream.avi", FileMode.Create, FileAccess.Write);
 videoOutputStream.Flush();
 videoOutputStream.WriteTo(file);
This code creates a file with with a size of 29751158 Bytes.


So there is a difference in the filesize. And because of that the second file could not be opened.

I have no clue how to modify this.
I need to use the stream because i want to create a avi file in memory and transfer the bytes only.

thanks in advance
Dec 14, 2016 at 1:01 PM
OK SOLVED!

For everyone facing the same problem...
writer.close()
is doing a dispose on the streams + writing some final header bytes.
So you can use your stream AFTER calling the close function.
My thought was to do it before because of the disposing.

I was even wondering why there is no dispose() method for the writer :)

Here is the solution
//close your stream to write the final Header bytes
writer.Close();
//now get the buffer content of your stream and do further work
byte[] buffer = videoOutputStream.GetBuffer();
MemoryStream msTemp = new MemoryStream(buffer);
FileStream file = new FileStream("videoOutputStream.avi", FileMode.Create, FileAccess.Write);
msTemp.WriteTo(file);
msTemp.Close();
file.Close();
Marked as answer by Prybe on 12/14/2016 at 5:02 AM
Coordinator
Jan 19 at 8:52 PM
Hi
Sorry for not responding in time. CodePlex has stopped sending me notifications, again :(
Yes, you should call the Close method to finalize contents of the AVI file. Just as with Stream/Writer classes from System.IO. This is mentioned in docs
Also, AviWriter does implement IDisposable, but this implementation is explicit, so Dispose method is not public.