【问题标题】:pyav: saving video and audio to separate files from streaming hlspyav:将视频和音频从流式 hls 中保存到单独的文件中
【发布时间】:2019-03-27 09:52:19
【问题描述】:

我正在编写一个脚本来提取视频关键帧(到frame_{0}.jpg)和音频到一个单独的.mp3 文件。这是我目前所拥有的:

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)

如何将音频流保存到文件中(最好是分块)。我需要启动一个单独的捕获例程并并行运行,还是可以在上面的循环中捕获?

不幸的是,我查看了pyav github posts 没有运气。不知道如何在一个循环中做到这一点。

【问题讨论】:

    标签: audio-streaming audio-recording pyav


    【解决方案1】:

    这是来自发布在 pyav github 存储库上的 answer

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      相关资源
      最近更新 更多