使用ffmpeg将视频转换为flv

时间:2012-01-06 09:40:54

标签: c# ffmpeg

在我的c#应用程序中,我正在编写将任何视频格式转换为flv格式的代码。为此,使用FFMPEG。

有时出现例外情况:

  

尝试读取或写入受保护的内存。这通常表明其他内存已损坏

以下是我抛出异常的代码,

    IntPtr pFormatContext;
    FFmpeg.av_register_all();

    int ret;
    ret = FFmpeg.av_open_input_file(out pFormatContext, this.Filename, IntPtr.Zero, 0, IntPtr.Zero);

    if (ret < 0)
    {
        Trace.WriteLine("couldn't open input file");

        FFmpeg.av_free_static();
        return;
    }


    try
    {
        ret = FFmpeg.av_find_stream_info(pFormatContext);

        if (ret < 0)
        {
            Trace.WriteLine("couldnt find stream informaion");
            FFmpeg.av_close_input_file(pFormatContext);
            FFmpeg.av_free_static();
            return;
        }


        FFmpeg.AVFormatContext formatContext = (FFmpeg.AVFormatContext)Marshal.PtrToStructure(pFormatContext, typeof(FFmpeg.AVFormatContext));

        Duration = formatContext.duration / FFmpeg.AV_TIME_BASE;

        for (int i = 0; i < formatContext.nb_streams; ++i)
        {
            FFmpeg.AVStream stream = (FFmpeg.AVStream)Marshal.PtrToStructure(formatContext.streams[i], typeof(FFmpeg.AVStream));
            FFmpeg.AVCodecContext codec = (FFmpeg.AVCodecContext)Marshal.PtrToStructure(stream.codec, typeof(FFmpeg.AVCodecContext));

            if (codec.codec_type == FFmpeg.CodecType.CODEC_TYPE_VIDEO)
            {
                Height = codec.height;
                Width = codec.width;

                        Type = FileType.flv;
                        MimeType = "video/x-flv";

            }

        }
    }
    catch (Exception ex)
    {
        Trace.WriteLine("FFMpeg failed to understand the file");
    }

    FFmpeg.av_close_input_file(pFormatContext);
    FFmpeg.av_free_static();
}

从上面的代码中,这个ret = FFmpeg.av_find_stream_info(pFormatContext);行会抛出内存损坏的异常。 请帮我解决这个问题。

0 个答案:

没有答案