title: pyav: saving video and audio to separate files from streaming hls
body: <p>I am writing a script to extract the video keyframes (to <code>frame_{0}.jpg</code>) and audio to the a separate <code>.mp3</code> file. Here is what I have so far:</p>
  
  <pre><code>import os
  import av
  
  path_to_video = 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8'
  container = av.open(path_to_video)
  
  stream = container.streams.video[0]
  audio_stream = container.streams.audio[0]
  
  stream.codec_context.skip_frame = 'NONKEY'
  tgt_path = "./frames"
  if not os.path.isdir(tgt_path):
      os.makedirs(tgt_path)
  for frame in container.decode(stream):
     tgt_filename = os.path.join(tgt_path,'frame-{:09d}.jpg'.format(frame.pts))
     frame.to_image().save(tgt_filename,quality=80)
  </code></pre>
  
  <p>How do I save the audio stream to a file (preferably in chunks). Do i need to launch a separate capture routine and run in parallel, or can I capture in above loop? </p>
  
  <p>I have looked over the <a href="https://github.com/mikeboers/PyAV/issues/302" rel="nofollow noreferrer">pyav github posts</a> to no luck, unfortunately. Not sure how I can do this in a single loop.</p>