简体   繁体   English

将使用 PyAV 收到的 h264 包保存在磁盘上

[英]Save on disk h264 packages received using PyAV

I'm using PyAV to read packets from H.264 stream during 5 minutes.我正在使用 PyAV 在 5 分钟内从 H.264 流中读取数据包。 I need to save the packets encoded on disk and load and decode them later.我需要保存在磁盘上编码的数据包,稍后加载和解码它们。 I tried to use pickle to save the packets, but it cannot serialize them.我尝试使用pickle来保存数据包,但它无法序列化它们。 What can I do?我能做什么? Here is part of my code (an exception is thrown when I try to save the packets using pickle, so the code doesn't work):这是我的代码的一部分(当我尝试使用 pickle 保存数据包时抛出异常,因此代码不起作用):

import av
import time
import pickle

# open container
container = av.open(url) # url from a h264 stream
# read packages from stream during 5 minutes and add them to a list
packets = []
initialTime = time.time()
for packet in container.demux(video = 0):
    packets.append(packet) 
    if time.time() - initialTime > 300:
        break
# save packets
pickle.dump(packets, open("packets.obj", "wb"))
# load packets, get the frames they contain and saves them as files
load_packets = pickle.load(open("packets.obj", "rb"))
for idx, packet in enumerate(load_packets):
    frame = packet.decode_one()
    if frame is not None:
        frame.to_image().save('frame-%04d-%04d.jpg' % (frame.index, idx))

Instead of saving the packets using pickle, I created an output mp4 container and muxed to it the packets read from the H.264 stream.我没有使用 pickle 保存数据包,而是创建了一个输出 mp4 容器,并将从 H.264 流中读取的数据包混入其中。 When I need to load them later, I just process the video generated by the container.当我稍后需要加载它们时,我只处理容器生成的视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM