Search code examples
c#encodingserial-portmjpegbinarywriter

Save the received MJPEG data in a .mjpeg file with C#


I have a project. It is with Raspberry Pi Camera V2. One PC is used for encoding the captured video in MJPEG format and sending it with the serial port. My PC is used for receiving the data, saving it in a .mjpeg formatted file and playing it with an MJPEG to MP4 converter. I am trying to save the data in these lines:

byte[] data= new byte[100];
serialPort.Read(data,0,100);
BinaryWriter videoFile = new BinaryWriter(File.Open("video.mjpeg",FileMode.Create));

string dataAscii;
dataAscii = System.Text.Encoding.UTF8.GetString(data); //bytearray to string

videoFile.Write(dataAscii); // which is received

It works, it creates a .mjpeg file. However, I couldn't make it play with the converter. Maybe I should save the data frame by frame or try to save in a different way. I have no idea about what I am doing wrong.

Any ideas, many thanks!

Kane


Solution

  • Why are you converting the byte array into a string before writing it? That's your problem. Just write the byte array directly to the file stream.