解决opencv保存视频打不开问题
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
问题描述
使用opencv来保存录制的视频,但是保存后的视频就是打不开,网上查找了很多资料,后面发现原来是保存的视频尺寸和被录制的视频尺寸不一样造成的,原本的代码如下
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videoWrite = cv2.VideoWriter(r'../videos/test.mp4', fourcc, 30, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
videoWrite.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(33) & 0xFF == ord('q'):
break
videoWrite.release()
cap.release()
cv2.destroyAllWindows()
解决办法
解决方法很简单,统一尺寸即可,使用代码获取被录制视频宽和高。
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
解决后的完整代码如下
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
videoWrite = cv2.VideoWriter(r'../videos/test.mp4', fourcc, 30, (width,height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
videoWrite.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(33) & 0xFF == ord('q'):
break
videoWrite.release()
cap.release()
cv2.destroyAllWindows()
GitHub 加速计划 / opencv31 / opencv
166
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
c623a5af
Android camera refactoring #26646
This patch set does not contain any functional changes. It just cleans up the code structure to improve readability and to prepare for future changes.
* videoio(Android): Use 'unique_ptr' instead of 'shared_ptr'
Using shared pointers for unshared data is considered an antipattern.
* videoio(Android): Make callback functions private static members
Don't leak internal functions into global namespace. Some member
variables are now private as well.
* videoio(Android): Move resolution matching into separate function
Also make internally used member functions private.
* videoio(Android): Move ranges query into separate function
Also remove some unneccessary initialisations from initCapture().
* videoio(Android): Wrap extremly long source code lines
* videoio(Android): Rename members of 'RangeValue'
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
11 小时前
7728dd33
Fix potential READ memory access #26782
This fixes https://oss-fuzz.com/testcase-detail/4923671881252864 and https://oss-fuzz.com/testcase-detail/5048650127966208
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
13 小时前
更多推荐
已为社区贡献14条内容
所有评论(0)