static void appendFrames(FileStream fs, MjpegIterator mjpeg)
        {
            mjpeg.MoveTo(0);

            ulong nbr_of_jpgs = (ulong)mjpeg.Count;

            while (!mjpeg.Eof())
            {
                var len = mjpeg.CurrentMem.Length;

                fs.WriteAsByteArray("00db");
                fs.fwrite_DWORD((uint)len);

                var bb = mjpeg.CurrentMem.ToArray();
                fs.Write(bb, 0, bb.Length);
                if (bb.Length % 2 != 0)
                {
                    fs.WriteByte(0);
                }
                if (!mjpeg.Next())
                {
                    break;
                }
            }

            ulong AVI_KEYFRAME = 16;
            ulong offset_count = 4;
            ulong index_length = 4 * 4 * nbr_of_jpgs;

            fs.WriteAsByteArray("idx1");
            fs.fwrite_DWORD((uint)index_length);

            mjpeg.MoveTo(0);

            while (!mjpeg.Eof())
            {
                var len = mjpeg.CurrentMem.Length;
                if (len % 2 != 0)
                {
                    len++;
                }

                fs.WriteAsByteArray("00db");

                fs.fwrite_DWORD((uint)AVI_KEYFRAME);
                fs.fwrite_DWORD((uint)offset_count);
                fs.fwrite_DWORD((uint)len);
                offset_count += (uint)(len + 8);
                if (!mjpeg.Next())
                {
                    break;
                }
            }
        }
        static ulong get_all_sizes(MjpegIterator mjpeg)
        {
            ulong sizes = 0;

            mjpeg.MoveTo(0);
            while (!mjpeg.Eof())
            {
                var len = mjpeg.CurrentMem.Length;
                if (len % 2 != 0)
                {
                    len += 1;
                }
                sizes += (ulong)len;
                if (!mjpeg.Next())
                {
                    break;
                }
            }
            return(sizes);
        }