I am using Axis media parser SDK to get h264 stream, how can I write that stream directly in MPEG-4 AVC (mp4) container in C# without re encoding and loosing the video quality
Currently these 2 events are fired to write audio and video into a file
private void OnVideoSample(int cookieID, int sampleType,
int sampleFlags, ulong startTime, ulong stopTime, object SampleArray)
{
// Cast the buffer object to a byte array
byte[] bufferBytes = (byte[])SampleArray;
Console.WriteLine("OnVideoSample - {0} bytes", bufferBytes.Length);
// Write the data to out file
lock (fileLock)
{
outFile.Write(sampleType);
outFile.Write(sampleFlags);
outFile.Write(startTime);
outFile.Write(stopTime);
outFile.Write(bufferBytes.Length);
outFile.Write(bufferBytes, 0, bufferBytes.Length);
}
}
// Event handler callback for audio samples buffers
private void OnAudioSample(int cookieID, int sampleType,
int sampleFlags, ulong startTime, ulong stopTime, object SampleArray)
{
// Cast the buffer object to a byte array
byte[] bufferBytes = (byte[])SampleArray;
Console.WriteLine("OnAudioSample - {0} bytes", bufferBytes.Length);
// Write the data to out file
lock (fileLock)
{
outFile.Write(sampleType);
outFile.Write(sampleFlags);
outFile.Write(startTime);
outFile.Write(stopTime);
outFile.Write(bufferBytes.Length);
outFile.Write(bufferBytes, 0, bufferBytes.Length);
}
}
You can build DirectShow pipeline with your custom source filters (esp. based on/derived from `PushSource’ SDK sample) which inject data provided from events quoted above. These two will be connected to a so called MPEG-4 Multiplexer, such as free one from GDCL or a compatible one. Then the former will be connected to file writer. This pipeline will convert the data from events into MP4 file.
Writing filters assumes C++ development though. I am not sure, but DirectShowLib might be providing a sort of bridge into managed envrionment so that you could end up avoiding touching C++.
You can also use Intel IPP Library (Intel Media SDK?) to produce MP4 files, though I think it also requires C++ rather than C#.