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

时间:2018-05-26 17:46:14

标签: python ffmpeg pickle h.264 avconv

我正在使用PyAv在5分钟内从H.264流中读取数据包。我需要保存在磁盘上编码的数据包,然后加载并解码它们。我试图使用pickle保存数据包,但它无法序列化它们。我能做什么?这是我的代码的一部分(当我尝试使用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))

1 个答案:

答案 0 :(得分:0)

我没有使用pickle保存数据包,而是创建了一个输出mp4容器,并将从H.264流中读取的数据包复用到该容器。当我以后需要加载它们时,我只处理容器生成的视频。