python-3.x 如何使用av.open打开BytesIO对象?

66bbxpm5  于 2023-06-25  发布在  Python
关注(0)|答案(1)|浏览(163)

我想使用av.open()(PyAV包)读取Python中的BytesIO对象,但是失败了。我使用一个普通的x.mp4文件和一个BytesIO对象进行比较。对于普通的x.mp4,av.open函数返回正确的容器,并且容器可以被解码成帧列表。

container = av.open('x.mp4')
print(container.streams.video)  # <av.VideoStream #0 h264, yuv420p 1280x720 at 0x7eff2ac89168>
for frame in container.decode(video=0):
    frame.to_image().save('frame-{}.jpg'.format(frame.index))  # which can output the jpg list

对于BytesIO对象,

some_bytesio_object = io.BytesIO(some_byte_file)  # the x.mp4 has the same content as the some_byte_file
container = av.open(some_bytesio_object)
print(container.streams.video)  # <av.VideoStream #? h264, yuv420p 1280x720 at 0x7eff2ac89168>, ? means the result can be random, like 0, 1054153136, -649221856.
for frame in container.decode(video=0):
    frame.to_image().save('frame-{}.jpg'.format(frame.index))  # which can not output the jpg list
kknvjkwl

kknvjkwl1#

some_bytesio_object.seek(0) # may help sometimes
av.open(some_bytesio_object, format='mp4')

相关问题