当生成H.264帧并使用pyAV对其进行解码时,只有在两次调用parse
种方法时,才会从帧中解析数据包.
Consider the following test H.264 input, created using:
ffmpeg -f lavfi -i testsrc=duration=10:size=1280x720:rate=30 -f image2 -vcodec libx264 -bsf h264_mp4toannexb -force_key_frames source -x264-params keyint=1:scenecut=0 "frame-%4d.h264"
现在,使用pyAV解析第一帧:
import av
codec = av.CodecContext.create('h264', 'r')
with open('/path/to/frame-0001.h264', 'rb') as file_handler:
chunk = file_handler.read()
packets = codec.parse(chunk) # This line needs to be invoked twice to parse packets
除非再次调用最后一行,否则数据包将保持为空(packets = codec.parse(chunk)
)
此外,对于我无法描述的不同现实生活示例,似乎从数据包解码帧也需要几个解码调用:
packet = packets[0]
frames = codec.decode(packet) # This line needs to be invoked 2-3 times to actually receive frames.
有人知道pyAV这种不一致的行为吗?
(Using Python 3.8.12 on macOS Monterey 12.3.1, ffmpeg 4.4.1, pyAV 9.0.2)