Python – 使用scikit-video库获取视频的旋转角度并使用opencv_python根据旋转角度对视频进行旋转复原
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – 使用scikit-video库获取视频的旋转角度并使用opencv_python根据旋转角度对视频进行旋转复原
原文链接:https://www.stubbornhuang.com/1855/
发布于:2021年12月07日 9:59:49
修改于:2021年12月07日 10:29:39

1 视频中的旋转信息
Android或者ios等手机上录制视频时,由于重力感应或者录制视频的摆放方式的问题会导致录制的视频拥有旋转信息。如果是横屏录制(手机逆时针旋转90度),则录制的视频时不带角度的。如果是竖屏录制(正常的拿手机的姿势),此时的录制的视频的旋转角度是90度。如果再旋转90度,此时一般音量键和关屏键朝下,此时的视频的旋转角度是180。以此类推。所以在手机上的视频一般会有4种角度的视频,播放时,要对视频资源进行旋转后再进行播放,不然视频就会出现各种反转、倾倒。
在本站文章https://www.stubbornhuang.com/1856/也分享了如何在C++中使用ffmpge sdk读取视频旋转角度以及使用OpenCV库根据旋转角度对视频进行旋转复原的代码,有兴趣可以看看。
2 为什么不直接使用opencv读取视频的旋转信息
使用opencv库单独读取视频时并不会读取到视频的旋转信息,这是因为视频中视频流的旋转信息通常存储在视频流的metadata元数据中,这个时候就需要使用scikit-video库,这个库封装了ffmpeg中ffporbe的方法,可以以字典的方式返回视频中的各种元信息。
3 使用scikit-video库获取视频的旋转信息并使用opencv对视频进行旋转复原
3.1 安装scikit-video
pip install scikit-video
3.2 使用scikit-video获取旋转信息
if __name__ == "__main__":
file_name = "test.MOV"
# get video info(rotate)
video_metadata = skvideo.io.ffprobe(file_name)
#video_tag_info = video_metadata['video']['tag']
rotate_degree_info = -1.0
for tag_info in video_metadata['video']['tag']:
for key, val in tag_info.items():
if val == "rotate":
rotate_degree_info = float(tag_info["@value"])
print("Info: video rotate degree info:{}".format(rotate_degree_info))
break
3.3 使用Opencv的自带的旋转矩阵旋转视频帧
# 得到旋转角度之后,对视频帧旋转对应的负角度便可以得到正向的图像
def rotate_img_data(img_data, degree):
h, w = img_data.shape[:2]
(cx, cy) = (w / 2, h / 2)
# 设置旋转矩阵
M = cv2.getRotationMatrix2D((cx, cy), -degree, scale=1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# 计算图像旋转后的新边界
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# 调整旋转矩阵的移动距离(t_{x}, t_{y})
M[0, 2] += (nW / 2) - cx
M[1, 2] += (nH / 2) - cy
img_rotated = cv2.warpAffine(img_data, M, (nW, nH))
return img_rotated
3.4 完整代码
import cv2
import skvideo.io
# 得到旋转角度之后,对视频帧旋转对应的负角度便可以得到正向的图像
def rotate_img_data(img_data, degree):
h, w = img_data.shape[:2]
(cx, cy) = (w / 2, h / 2)
# 设置旋转矩阵
M = cv2.getRotationMatrix2D((cx, cy), -degree, scale=1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# 计算图像旋转后的新边界
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# 调整旋转矩阵的移动距离(t_{x}, t_{y})
M[0, 2] += (nW / 2) - cx
M[1, 2] += (nH / 2) - cy
img_rotated = cv2.warpAffine(img_data, M, (nW, nH))
return img_rotated
if __name__ == "__main__":
file_name = "test.MOV"
# get video info(rotate)
video_metadata = skvideo.io.ffprobe(file_name)
#video_tag_info = video_metadata['video']['tag']
rotate_degree_info = -1.0
for tag_info in video_metadata['video']['tag']:
for key, val in tag_info.items():
if val == "rotate":
rotate_degree_info = float(tag_info["@value"])
print("Info: video rotate degree info:{}".format(rotate_degree_info))
break
# read video sequence
video_io = cv2.VideoCapture(file_name)
all_frame_nums = video_io.get(cv2.CAP_PROP_FRAME_COUNT)
read_status, video_frame = video_io.read()
while read_status:
# rotate the image if needs
if abs(-1.0 - rotate_degree_info) > 1.0:
video_frame = rotate_img_data(video_frame.copy(), rotate_degree_info)
read_status, video_frame = video_io.read()
参考链接
当前分类随机文章推荐
- Python - 读取csv文件和在csv文件写入内容 阅读245次,点赞0次
- Python - 不依赖第三方库对类对象进行json序列化与反序列化 阅读1229次,点赞0次
- Python3爬虫 - requests的请求响应状态码(requests.status_code) 阅读8495次,点赞4次
- Python3爬虫 - requests库 阅读3527次,点赞3次
- Python - yaml配置用法详解以及使用Pyyaml库操作yaml文件 阅读550次,点赞0次
- Python - 运行YOLOv5出现AttributeError: module 'torchvision' has no attribute 'ops' 阅读1515次,点赞1次
- Python - opencv-python保存视频时出现Failed to load OpenH264 library: openh264-1.8.0-win64.dll错误 阅读628次,点赞0次
- Python - 运算符/、//和%的含义与区别 阅读260次,点赞0次
- Python - 深度学习训练过程使用matplotlib.pyplot实时动态显示loss和acc曲线 阅读2002次,点赞0次
- Python - 配置Yolov5出现ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'错误 阅读986次,点赞0次
全站随机文章推荐
- Duilib - duilib应用程序实现文件可拖拽的功能 阅读2207次,点赞1次
- 计算机图形学 - Flat Shading、Gouraud Shading、Phong Shading的区别 阅读667次,点赞0次
- js判断当前元素是否含有子元素 阅读507次,点赞0次
- C++ - 使用ffmpeg读取视频旋转角度并使用OpenCV根据旋转角度对视频进行旋转复原 阅读1485次,点赞0次
- 资源分享 - GPU Pro 4 - Advanced Rendering Techniques 英文高清PDF下载 阅读2322次,点赞1次
- WordPress - 在erphpdown插件中增加在隐藏文章指定位置之后文章内容的付费查看功能 阅读741次,点赞0次
- 资源分享 - Ray Tracing Gems - High-Quality and Real-Time Rendering with DXR and Other APIs 英文高清PDF下载 阅读2399次,点赞0次
- 资源分享 - Speech and Language Processing - An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition , Third Edition draft 英文高清PDF下载 阅读393次,点赞0次
- C++ - queue存储动态指针时正确释放内存 阅读4992次,点赞2次
- 资源分享 - 3D Game Engine Architecture - Engineering Real-Time Applications with Wild Magic 英文高清PDF下载 阅读1296次,点赞0次
评论
164