Exemplo n.º 1
0
        /// <summary>
        /// Opens the media resource given by the specified <paramref name="stream"/>
        /// </summary>
        /// <param name="stream">Stream containing the contents of the file where the metadata should be
        /// extracted.</param>
        /// <returns><c>true</c>, if the open operation was successful. In this case, <see cref="IsValid"/> will
        /// be <c>true</c> also. <c>false</c>, if the open operation was not successful. This means,
        /// <see cref="IsValid"/> will also be <c>false</c>.</returns>
        public bool Open(Stream stream)
        {
            _isValid = (_mediaInfo.Open_Buffer_Init(stream.Length, 0) == MEDIAINFO_FILE_OPENED);
            if (!_isValid)
            {
                return(false);
            }

            // Increased buffer size for some .mp4 that contain first audio, then video stream. If buffer is smaller (i.e. 64 kb),
            // MediaInfo only detects the audio stream. It works correctly in file mode.
            const int bufferSize = 512 * 1024;

            byte[]   buffer    = new byte[bufferSize]; // init the buffer to communicate with MediaInfo
            GCHandle gcHandle  = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr   bufferPtr = gcHandle.AddrOfPinnedObject();

            try
            {
                // Now we need to run the parsing loop, as long as MediaInfo requests information from the stream
                int bytesRead;
                do
                {
                    bytesRead = stream.Read(buffer, 0, bufferSize);

                    if ((_mediaInfo.Open_Buffer_Continue(bufferPtr, (IntPtr)bytesRead) & BufferResult.Finalized) == BufferResult.Finalized)
                    {
                        // MediaInfo doesn't need more information from us
                        break;
                    }

                    long newPos = _mediaInfo.Open_Buffer_Continue_GoTo_Get();
                    // Now we need to test, if MediaInfo wants data from a different place of the stream
                    if (newPos == -1)
                    {
                        break;
                    }
                    Int64 pos = stream.Seek(newPos, SeekOrigin.Begin); // Position the stream
                    _mediaInfo.Open_Buffer_Init(stream.Length, pos);   // Inform MediaInfo that we are at the new position
                } while (bytesRead > 0);
            }
            finally
            {
                gcHandle.Free();

                // Finalising MediaInfo procesing
                _mediaInfo.Open_Buffer_Finalize();
            }
            return(_isValid);
        }
Exemplo n.º 2
0
        public static String ExampleWithStream()
        {
            //Initilaizing MediaInfo
            MediaInfo MI = new MediaInfo();

            //From: preparing an example file for reading
            FileStream From = new FileStream(FilePath, FileMode.Open, FileAccess.Read);

            //From: preparing a memory buffer for reading
            byte[] From_Buffer = new byte[64 * 1024];
            int From_Buffer_Size; //The size of the read file buffer

            //Preparing to fill MediaInfo with a buffer
            MI.Open_Buffer_Init(From.Length, 0);

            //The parsing loop
            do
            {
                //Reading data somewhere, do what you want for this.
                From_Buffer_Size = From.Read(From_Buffer, 0, 64 * 1024);

                //Sending the buffer to MediaInfo
                System.Runtime.InteropServices.GCHandle GC = System.Runtime.InteropServices.GCHandle.Alloc(From_Buffer, System.Runtime.InteropServices.GCHandleType.Pinned);
                IntPtr From_Buffer_IntPtr = GC.AddrOfPinnedObject();
                Status Result = (Status)MI.Open_Buffer_Continue(From_Buffer_IntPtr, (IntPtr)From_Buffer_Size);
                GC.Free();
                if ((Result & Status.Finalized) == Status.Finalized)
                    break;

                //Testing if MediaInfo request to go elsewhere
                if (MI.Open_Buffer_Continue_GoTo_Get() != -1)
                {
                    Int64 Position = From.Seek(MI.Open_Buffer_Continue_GoTo_Get(), SeekOrigin.Begin); //Position the file
                    MI.Open_Buffer_Init(From.Length, Position); //Informing MediaInfo we have seek
                }
            }
            while (From_Buffer_Size > 0);

            //Finalizing
            MI.Open_Buffer_Finalize(); //This is the end of the stream, MediaInfo must finnish some work

            //Get() example
            return "Container format is " + MI.Get(StreamKind.General, 0, "Format");
        }