Pregunta

Libado externo usado: http://sourceforge.net/projects/directshownet/

con archivos AVI, WMV, MP4, FLV, WEBM Este código funciona bien, pero con MKV devuelve solo la imagen negra (probada con flujo de video X264 y video de video xvid).

Entonces ... Mi pregunta es: ¿Tiene alguna idea de cómo obtener el marco desde el archivo MKV?(Actualmente estoy usando Hali Media Splitter)

public string FileName
{
        get
        {
            return fileName;
        }

        set
        {
            mediaDet = null;
            fileName = value;

            if (File.Exists(fileName))
            {
                AMMediaType mediaType = null;

                try
                {
                    mediaDet = (IMediaDet)new MediaDet();
                    DsError.ThrowExceptionForHR(mediaDet.put_Filename(fileName));

                    // find the video stream in the file
                    int index = 0;
                    Guid type = Guid.Empty;
                    while (type != MediaType.Video)
                    {
                        mediaDet.put_CurrentStream(index++);
                        mediaDet.get_StreamType(out type);
                    }

                    // retrieve some measurements from the video
                    mediaDet.get_FrameRate(out frameRate);

                    mediaType = new AMMediaType();
                    mediaDet.get_StreamMediaType(mediaType);
                    videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));
                    DsUtils.FreeAMMediaType(mediaType);
                    mediaType = null;
                    width = videoInfo.BmiHeader.Width;
                    height = videoInfo.BmiHeader.Height;

                    mediaDet.get_StreamLength(out mediaLength);
                    frameCount = (int)(frameRate * mediaLength);
                }
                catch (Exception e)
                {
                    if (mediaType != null)
                    {
                        DsUtils.FreeAMMediaType(mediaType);
                    }

                    fileName = "";

                    throw new ArgumentException(String.Format("unable to open the file \"{0}\", DirectShow reported the following error: {1}", value, e.Message));
                }
            }
        }
    }

public Bitmap GetImageAtTime(double seconds)
    {
        if (seconds <= mediaLength)
        {
            if (mediaDet != null)
            {
                IntPtr bufferPtr = IntPtr.Zero;
                Bitmap returnValue = null;

                try
                {
                    // create a buffer to hold the image data from the MediaDet
                    int bufferSize;
                    mediaDet.GetBitmapBits(seconds, out bufferSize, IntPtr.Zero, width, height);
                    bufferPtr = Marshal.AllocHGlobal(bufferSize);
                    mediaDet.GetBitmapBits(seconds, out bufferSize, bufferPtr, width, height);

                    // compose a bitmap from the data in the managed buffer 
                    unsafe
                    {
                        returnValue = new Bitmap(width, height, this.PixelFormat);
                        BitmapData imageData = returnValue.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, this.PixelFormat);
                        int* imagePtr = (int*)imageData.Scan0;

                        int bitmapHeaderSize = Marshal.SizeOf(videoInfo.BmiHeader);
                        int* sourcePtr = (int*)((byte*)bufferPtr.ToPointer() + bitmapHeaderSize);

                        for (int i = 0; i < (bufferSize - bitmapHeaderSize) / 4; i++)
                        {
                            *imagePtr = *sourcePtr;
                            imagePtr++;
                            sourcePtr++;
                        }

                        returnValue.UnlockBits(imageData);
                        returnValue.RotateFlip(RotateFlipType.Rotate180FlipX); // DirectShow stores pixels in a different order than Bitmaps do
                    }

                    Marshal.FreeHGlobal(bufferPtr);

                    return returnValue;
                }
                catch
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(bufferPtr);
                    }

                    if (returnValue != null)
                    {
                        returnValue.Dispose();
                    }

                    throw;
                }
            }
            else
            {
                throw new InvalidOperationException("cannot retrieve the frame because the FileName property has not been set yet");
            }
        }
        else
        {
            throw new ArgumentException(String.Format("seconds must be between 0 and {0} inclusive, value was \"{1}\"", mediaLength, seconds));
        }
    }

¿Fue útil?

Solución

Debe crear gráficos manualmente, también mejor usar su propio filtro de grabador de muestra, según el filtro de transinplace.Razón Problema - VideoInfoHeader2 Uso que no es compatible con el filtro de grabador de muestra estándar (utilizado en Imediadet).Debe hacer su propio filtro con el soporte de VideoInfoHeader / VideoInfoHeader2.Es fácil.Use CEZRGB24 o filtro de escaler de grises como muestras.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top