Skip to main content
 首页 » 编程设计

python之pyglet/avbin 错过了视频的开头和结尾

2025年05月04日9zengkefu

我有一个问题,当使用 pyglet 播放视频时,流的开始和结束显然没有得到正确处理。播放器忽略第一个关键帧并因此显示故障图像,直到它找到第二个关键帧。同样,on_eos 事件没有触发,而是 python 崩溃,因为它显然试图加载更多帧。

import pyglet 
 
vidPath = "test.mp4" 
window = pyglet.window.Window() 
player = pyglet.media.Player() 
source = pyglet.media.load(vidPath) 
 
player.queue(source) 
player.play() 
 
@player.event 
def on_eos(): 
    print('video end') 
 
@window.event 
def on_draw(): 
    window.clear() 
    if player.source and player.source.video_format: 
        player.get_texture().blit(0, 0) 
 
pyglet.app.run() 

我在 OSX 10.11.3 上使用 Python 3.4.3、pyglet 1.2.4 和 AVbin 10。

在 Ubuntu 上尝试相同的代码和视频,我遇到了同样的问题,并在启动时出现以下错误:

Non-reference picture received and no reference available 
[h264 @ 0x3386700] decode_slice_header error 
Non-reference picture received and no reference available 
[h264 @ 0x33880e0] decode_slice_header error 
Non-reference picture received and no reference available 
[h264 @ 0x3388520] decode_slice_header error 
[h264 @ 0x3388a80] illegal short term buffer state detected 
Non-reference picture received and no reference available 
[h264 @ 0x3388a80] decode_slice_header error 
[h264 @ 0x338c2e0] reference picture missing during reorder 
[h264 @ 0x338c2e0] reference picture missing during reorder 
[h264 @ 0x338c2e0] reference picture missing during reorder 

视频播放结束后:

[h264 @ 0x23bde00] no frame! 
[h264 @ 0x23bf7e0] AVC: nal size 0 
[h264 @ 0x23bf7e0] AVC: nal size 0 
[h264 @ 0x23bf7e0] no frame! 
[h264 @ 0x23bfc20] AVC: nal size 0 
[h264 @ 0x23bfc20] AVC: nal size 0 

任何想法这里的问题是什么?我有一些视频可以正确开始,但无法识别结尾。

请您参考如下方法:

我还遇到过通过 pyglet 播放文件开头时出现故障的问题。我通过重新编码并在文件开头强制添加一些额外的关键帧来“修复”它:

avconv -i glitchy-file.mp4 -force_key_frames 0.01,0.02,0.03 \ 
-c:v h264 -c:a copy not-glitchy-file.mp4 

这种方法也适用于合适的最新版本的 ffmpeg(只需将 avconv 替换为 ffmpeg)。我只在 linux 上用 h264 编码的视频流测试过它,使用 avbin10 和从 their git master branch 本地构建的 libav .

这是一个 hack,而不是解决方案,因为它不会告诉您为什么 pyglet 在流的早期忽略关键帧,也不会导致 pyglet 正确读取它们。但它使问题消失了。