共计 1392 个字符,预计需要花费 4 分钟才能阅读完成。
import av
input_file = 'DaoMengKongJian-720P.mp4'
input_file = 'DaoMengKongJian-480P.mp4'
# 初始化 FFmpeg 上下文
# av.avdevice_register_all()
# av.logging.set_level(av.logging.INFO)
count = 0
with av.open(input_file, metadata_encoding='utf-8', metadata_errors='ignore') as container:
video_stream = container.streams.video[0]
video_stream.thread_type = 'AUTO'
average_fps: int = round(video_stream.average_rate)
interval = 1
for index, frame in enumerate(container.decode(video_stream)):
if index % (average_fps) == 0:
frame.to_ndarray(format='rgb24')
count += 1
print('count is', count)
pyav 拆帧速度测试
平台 | 视频分辨率 | 耗时(秒) |
---|---|---|
E5-2690 | 1280×720 | 115 |
E5-2690 | 640×360 | 63 |
此时,利用了 8 个 cpu core(自动挡,pyav 主动设置线程数目)
接下来看看,手动设置不同线程数,拆帧的效率
参考文章:pyav 指定线程数目
import av
import time
input_file = 'DaoMengKongJian-360P.mp4'
for thread_count in [1, 2, 3, 4, 5]:
count = 0
s = time.time()
with av.open(input_file, metadata_encoding='utf-8', metadata_errors='ignore') as container:
video_stream = container.streams.video[0]
# video_stream.thread_type = 'AUTO'
video_stream.thread_count = thread_count
average_fps: int = round(video_stream.average_rate)
interval = 1
for index, frame in enumerate(container.decode(video_stream)):
if index % (average_fps) == 0:
frame.to_ndarray(format='rgb24')
count += 1
e = time.time()
print(f'thread count is {thread_count}, pay time is {e-s}')
测试后果
pyav 拆帧速度测试
平台 | 视频分辨率 | 线程数 | 耗时(秒) |
---|---|---|---|
E5-2690 | 640×360 | 1 | 147 |
E5-2690 | 640×360 | 2 | 115 |
E5-2690 | 640×360 | 3 | 93 |
E5-2690 | 640×360 | 4 | 104 |
E5-2690 | 640×360 | 5 | 77 |
E5-2690 | 640×360 | 6 | 83 |
E5-2690 | 640×360 | 7 | 87 |
E5-2690 | 640×360 | 8 | 77 |
论断,能够看到,多线程,能够起到减速作用,但不是线性减速
正文完