title: Writing multithreaded video and audio packets with FFmpeg
body: <p>I couldn't find any information on the way <code>av_interleaved_write_frame</code> deals with video and audio packets.</p>
  
  <p>I have multiple audio and video packets coming from 2 threads. Each thread calls a <code>write_video_frame</code> or <code>write_audio_frame</code>, locks a mutex, initialize an AVPacket and writes data to an .avi file.</p>
  
  <p>Initialization of AVCodecContext and AVFOrmatContext is ok. </p>
  
  <p><strong>-- Edit 1 --</strong></p>
  
  <p>Audio and video are coming from an external source (microphone and camera) and are captured as raw data without any compression (even for video). 
  I use h264 to encode video and no compression for Audio (PCM). </p>
  
  <p>Audio captured is: 16bits, 44100khz, stereo</p>
  
  <p>Video captured is 25FPS</p>
  
  <p><strong>Question:</strong></p>
  
  <p>1) Is it a problem if I write multiple video packets at once (let's say 25 packets/sec) and just one audio packet/sec.</p>
  
  <p><strong><em>Answer: Apparently not, the function <code>av_interleaved_write_frame</code> should be able to manage that kind of data as soon as pts and dts is well managed</em></strong></p>
  
  <p>This means I call <code>av_interleaved_write_frame</code> 25 times for video writing and just 1 for audio writing per second. Could this be a problem ? If it is how can I deal with this scenario ?</p>
  
  <p>2) How can I manage pts and dts in this case ? It seems to be a problem in my application since I cannot correctly render the .avi file. Can I use real time stamps for both video and audio ? </p>
  
  <p><strong><em>Answer: The best thing to do here is to use the timestamp given when capturing audio / video as pts and dts for this kind of application. So these are not exactly real time stamps (from wall clock) but media capture timestamps.</em></strong></p>
  
  <p>Thank you for your precious advices.</p>