OpenCV中VideoWriter输出视频文件为空或6KB解决方法(java)

最近因项目需要,刚刚开始接触OpenCV。使用videoWriter将处理完的视频帧转换为视频存到磁盘时视频大小总是为0KB(mp4)或者6KB(AVI)。查找资料都说是视频编码的问题,但是换了好几种常见编码都无法解决问题。后来在Stack Overflow找到一个关于vw.isOpened() 返回值为false(我的代码返回值为true)的回答,抱着试一试的心态解决了问题。代码如下:

                    Mat frame0 = new Mat();
		VideoWriter vw = null;
		
		VideoCapture camera = new VideoCapture("input.dav");
		int i = 0;
		while (camera.read(frame0)) {
			if(i==0){
				Size frameSize = new Size((int) camera.get(Videoio.CAP_PROP_FRAME_WIDTH),(int) camera.get(Videoio.CAP_PROP_FRAME_HEIGHT));
				vw = new VideoWriter("output.avi", VideoWriter.fourcc('M', 'J', 'P', 'G'), 10, frameSize,true);//frameSize不能为frame0.size()
				System.out.println(vw.isOpened());
			}
			vw.write(frame0);
			i++;
		}


原文链接:https://blog.csdn.net/qq_30711587/article/details/78658624

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCVVideoWriter类只支持本地文件路径作为输出路径,无法直接将TCP作为输出路径。但是,你可以使用网络传输协议(如RTSP、HTTP)来将视频流传输到网络上,然后使用网络流媒体服务器(如FFmpeg、VLC)将视频流保存到本地文件或将其推送到其他客户端。 以下是使用RTSP协议将视频流传输到网络上并将其保存到本地文件的示例代码: ```python import cv2 # RTSP URL rtsp_url = "rtsp://username:password@ip_address:port/video_stream" # Create VideoCapture object cap = cv2.VideoCapture(rtsp_url) # Check if camera opened successfully if not cap.isOpened(): print("Error opening video stream or file") # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 25.0, (640, 480)) # Read until video is completed while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() if ret: # Display the resulting frame cv2.imshow('Frame', frame) # Write the frame to the output file out.write(frame) # Press Q on keyboard to exit if cv2.waitKey(25) & 0xFF == ord('q'): break else: break # Release the video capture and writer objects cap.release() out.release() # Close all windows cv2.destroyAllWindows() ``` 在上述代码,我们使用RTSP协议从IP摄像机获取视频流,并使用VideoWriter类将其保存为本地文件。请注意,我们没有将TCP作为输出路径,而是使用了RTSP协议将视频流传输到网络上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值