title: Converting videos to flv using ffmpeg
body: <p>In my c# application, i am writing code for converting any video format to flv format. For this FFMPEG is used.  </p>
  
  <p>Some times an exceptions is occuring like:</p>
  
  <blockquote>
    <p>Attempted to read or write protected memory. This is often an indication that other memory is corrupt</p>
  </blockquote>
  
  <p>Below is my code from where the exception throwing,</p>
  
  <pre><code>    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 &lt; 0)
      {
          Trace.WriteLine("couldn't open input file");
  
          FFmpeg.av_free_static();
          return;
      }
  
  
      try
      {
          ret = FFmpeg.av_find_stream_info(pFormatContext);
  
          if (ret &lt; 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 &lt; 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();
  }
  </code></pre>
  
  <p>And from the above code this <code>ret = FFmpeg.av_find_stream_info(pFormatContext);</code> line throws memory corrupt exception.
  Please help me to solve this issue.</p>