Python 读取 YUV(NV12) 视频文件实例
本文转载(部分),侵删!原文请点击
代码部分主要是将YUV格式文件保存成BGR(NV12)格式文件
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
def yuv2bgr(filename, height, width, startfrm):
"""
:param filename: 待处理 YUV 视频的名字
:param height: YUV 视频中图像的高
:param width: YUV 视频中图像的宽
:param startfrm: 起始帧
:return: None
"""
fp = open(filename, 'rb')
framesize = height * width * 3 // 2 # 一帧图像所含的像素个数
h_h = height // 2
h_w = width // 2
fp.seek(0, 2) # 设置文件指针到文件流的尾部
ps = fp.tell() # 当前文件指针位置
numfrm = ps // framesize # 计算输出帧数
fp.seek(framesize * startfrm, 0)
for i in range(numfrm - startfrm):
Yt = np.zeros(shape=(height, width), dtype='uint8', order='C')
Ut = np.zeros(shape=(h_h, h_w), dtype='uint8', order='C')
Vt = np.zeros(shape=(h_h, h_w), dtype='uint8', order='C')
for m in range(height):
for n in range(width):
Yt[m, n] = ord(fp.read(1))
for m in range(h_h):
for n in range(h_w):
Ut[m, n] = ord(fp.read(1))
for m in range(h_h):
for n in range(h_w):
Vt[m, n] = ord(fp.read(1))
img = np.concatenate((Yt.reshape(-1), Ut.reshape(-1), Vt.reshape(-1)))
img = img.reshape((height * 3 // 2, width)).astype('uint8') # YUV 的存储格式为:NV12(YYYY UV)
# 由于 opencv 不能直接读取 YUV 格式的文件, 所以要转换一下格式
bgr_img = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12) # 注意 YUV 的存储格式
cv2.imwrite('yuv2bgr/%d.jpg' % (i + 1), bgr_img)
print("Extract frame %d " % (i + 1))
fp.close()
print("job done!")
return None
if __name__ == '__main__':
_ = yuv2bgr(filename='xxx.yuv', height=1080, width=1920, startfrm=0)
最后
以上就是凶狠爆米花最近收集整理的关于分享 Python 读取 YUV(NV12) 视频文件实例的全部内容,更多相关分享内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复