Example #1
0
        static void f(IsoBaseMediaFileFormatReader reader)
        {
            const int indent = 4;

            long totalSize         = 0;
            int  totalBoxes        = 0;
            long mediaData         = 0;
            long totalFreeBoxSpace = 0;

            while (reader.Read())
            {
                Console.WriteLine("{0}Box {1} @ {2} of size: {3}, ends @ {4} {5}",
                                  string.Empty.PadRight(reader.Depth * indent),
                                  reader.GetTypeAsString(),
                                  reader.BoxPosition,
                                  reader.CalculatedSize + (reader.Size == 0 ? " (0*)" : string.Empty),
                                  reader.BoxPosition + reader.CalculatedSize,
                                  (reader.IsRecognizedType && reader.IsRecognizedVersion.GetValueOrDefault(true)) ? string.Empty : "~");
                if (reader.Depth == 0)
                {
                    totalSize += reader.CalculatedSize;
                }
                if (reader.TypeString == "mdat")
                {
                    mediaData = reader.CalculatedSize;
                }
                if (reader.TypeString == "free" || reader.TypeString == "skip")
                {
                    totalFreeBoxSpace += reader.CalculatedSize;
                }
                totalBoxes++;
            }
            Console.WriteLine("                        (*)denotes length of atom goes to End-of-File");
            Console.WriteLine();
            Console.WriteLine(" ~ denotes an unknown box");
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Total size: {0} bytes; {1} atoms total.", totalSize, totalBoxes);
            Console.WriteLine("Media data: {0} bytes; {1} bytes all other boxes ({2} box overhead).", mediaData, totalSize - mediaData, ((totalSize - mediaData) / (double)totalSize).ToString("0.000%"));
            Console.WriteLine("Total free box space: {0} bytes; {1} waste. Padding avaliable: {2} bytes.", totalFreeBoxSpace, (totalFreeBoxSpace / (double)totalSize).ToString("0.000%"), "?");
            Console.WriteLine("------------------------------------------------------");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"D:\Users\Jonathan\Documents\Visual Studio 2010\Projects\IsoBaseMediaFormatParser\trailer.m4v", FileMode.Open, FileAccess.Read))
            {
                using (FileStream output = new FileStream(@"test.m4v", FileMode.Create, FileAccess.Write))
                {
                    Stack <KeyValuePair <uint, bool> > boxes = new Stack <KeyValuePair <uint, bool> >();

                    IsoBaseMediaFileFormatReader reader = new IsoBaseMediaFileFormatReader(fs);
                    IsoBaseMediaFormatWriter     writer = new IsoBaseMediaFormatWriter(output);
                    int writerDepth = 0;

                    while (reader.Read())
                    {
                        /* while (boxes.Count > reader.Depth)
                         * {
                         *  if (boxes.Peek().Value)
                         *      writer.WriteBoxEnd();
                         *  boxes.Pop();
                         * }
                         *
                         * if (reader.Depth == boxes.Count)
                         *  boxes.Push(new KeyValuePair<uint, bool>(reader.Type, false));
                         *
                         * //var boxTypes = boxes.Reverse().Select(b => Utility.GetTypeAsString(b.Key)).ToArray();
                         *
                         * //Console.WriteLine(string.Join("->", boxTypes)); */

                        while (writerDepth > reader.Depth)
                        {
                            writer.WriteEndBox();
                            writerDepth--;
                        }

                        if (reader.IsFullBox)
                        {
                            writer.WriteStartFullBox(reader.Type, reader.Version.Value, reader.Flags);
                        }
                        else
                        {
                            writer.WriteStartBox(reader.Type, reader.UserType);
                        }
                        writerDepth++;

                        if (!reader.IsContainer)
                        {
                            int    bytesRead;
                            byte[] buffer = new byte[4096];
                            while ((bytesRead = reader.ReadContent(buffer, 0, buffer.Length)) > 0)
                            {
                                writer.WriteContent(buffer, 0, bytesRead);
                            }
                        }

                        //boxes.Push(new KeyValuePair<uint, bool>(boxes.Pop().Key, true));
                    }
                }
            }

            Console.ReadLine();
        }