I am writing a script to extract the video keyframes (to frame_{0}.jpg
) and audio to the a separate .mp3
file. Here is what I have so far:
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)
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?
I have looked over the pyav github posts to no luck, unfortunately. Not sure how I can do this in a single loop.
This is from an answer posted on pyav github repo:
import av
input_container = av.open(
'http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8')
input_stream = input_container.streams.get(audio=0)[0]
output_container = av.open('live_stream.mp3', 'w')
output_stream = output_container.add_stream('mp3')
for frame in input_container.decode(input_stream):
frame.pts = None
for packet in output_stream.encode(frame):
output_container.mux(packet)
for packet in output_stream.encode(None):
output_container.mux(packet)
output_container.close()