title: FFMpeg library: how to precisely seek in an audio file
body: <p>Using the FFMpeg library in my Android app, I try to understand how I can seek in an audio file, at a very precise position.</p>
  
  <p>For example, I want to set the current position in my file to the frame #1234567 (in a file encoded at 44100 Hz), which is equivalent to seek at 27994.717 milliseconds.</p>
  
  <p>To achieve that, here is what I tried:</p>
  
  <pre><code>// this:
  av_seek_frame(formatContext, -1, 27994717, 0);
  
  // or this:
  av_seek_frame(formatContext, -1, 27994717, AVSEEK_FLAG_ANY);
  
  // or even this:
  avformat_seek_file(formatContext, -1, 27994617, 27994717, 27994817, 0);
  </code></pre>
  
  <p>Using a position in microseconds gives me the best result so far.</p>
  
  <p>But for some reason, the positioning is not totally accurate: when I extract the samples from the audio file, it doesn't start exactly at the expected position. There is a slight delay of about 30-40 milliseconds (even if I seek to the position 0, surprisingly...).</p>
  
  <p>Do I use the function the right way, or even the right function?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is how I can get the position:</p>
  
  <pre><code>AVPacket packet;
  AVStream *stream = NULL;
  AVFormatContext *formatContext = NULL;
  AVCodec *dec = NULL;
  
  // initialization:
  avformat_open_input(&amp;formatContext, filename, NULL, NULL);
  avformat_find_stream_info(formatContext, NULL);
  int audio_stream_index = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &amp;dec, 0);
  stream = formatContext-&gt;streams[audio_stream_index];
  
  ...
  
  // later, when I extract samples, here is how I get my position, in microseconds:
  av_read_frame(formatContext, &amp;packet);
  long position = (long) (1000000 * (packet.pts * ((float) stream-&gt;time_base.num / stream-&gt;time_base.den)));
  </code></pre>
  
  <p>Thanks to that piece of code, I can get the position of the beginning of the current frame (frame = bloc of samples, the size depends on the audio format - 1152 samples for mp3, 128 to 1152 for ogg, ...) </p>
  
  <p><strong>The problem is:</strong> the value I get in <code>position</code> is not accurate: it's actually 30 ms late, approximately. For example, when it says 1000000, the actual position is approximately 1030000...</p>
  
  <p>What did I do wrong? Is it a bug in FFMpeg?</p>
  
  <p>Thanks for your help.</p>