opencv:rtsp看视频并保存

1059 篇文章 277 订阅

环境安装

参考

保存图片

#include <string>
#include <iostream>
#include <time.h>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

void Video_to_Image(Mat& frame);

int main()
{
    //string filename = "Wildlife.wmv";
    string filename = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
    Mat frame;
    VideoCapture cap;
    cap.open(filename);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }

    //--- GRAB AND WRITE LOOP
    cout << "Start grabbing" << endl
         << "Press any key to terminate" << endl;
    time_t start_time = time(NULL);
    for (;;)
    {
        // wait for a new frame from camera and store it into 'frame'
        cap.read(frame);
        // check if we succeeded
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        // show live and wait for a key with timeout long enough to show images
        imshow("Live", frame);

        // 每隔2s保存图片
        time_t end_time = time(NULL);
        if ((end_time - start_time) >=2)
        {
            cout << "2s capture" << endl;
            Video_to_Image(frame);
            start_time = time(NULL);
        }

        if (waitKey(5) >= 0)
            break;
    }
    cap.release();

    return 0;
}

void Video_to_Image(Mat& frame)
{

    char image_name[PATH_MAX];
    sprintf(image_name, "%s%s", "test_image", ".jpg");
    imwrite(image_name, frame);

}

获取实时视频

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <unistd.h>
using namespace cv;
int main(int argc, char** argv )
{
    cv::VideoCapture cap("rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov");
    if (!cap.isOpened()) {
        std::cout << "" << std::endl;
        return -1;
    }

    cv::Mat frame;
    int waitNum = 5;
    do {
        cap >> frame;
        if (!frame.empty()) break;
        std::cout << "NOT FRAME: wait " << waitNum << std::endl;
        sleep(10); // linux Sleep
    } while (--waitNum > 0);

    cv::namedWindow("MYLAF RTSP");
    while (true) {
        cap >> frame;
        if (frame.empty()) break;

        cv::imshow("MYLAF RTSP", frame);
        cv::waitKey(30);
    }

    cap.release();
    waitKey(0);
    return 0;
}

保存视频

·Opencv·保存摄像头视频为mp4和flv两种格式
由于我采用的是opencv4.0,在保存视频的地方遇到了一些坑。
首先,原来的VideoWriter_fourcc没有了,需要使用VideoWriter::fourcc()来定义编码,
具体的编码类型可以在http://www.fourcc.org/codecs.php中查看
例如

  • MP4的fourcc编码为int myFourCC = VideoWriter::fourcc('m', 'p', '4', 'v');
  • FLV的fourcc编码为int myFourCC = VideoWriter::fourcc('X', 'V', 'I', 'D');

写入视频无非是一帧帧写入图像,通过VideoWriter writer("OutFileSavePath", myFourCC, rate, size, true);然后通过writer的writer.write(frame)或者writer << frame;写入视频图像。
VideoWriter实例化中最后一个参数是指定是否彩色写入,true即为彩色。

当实例化VideoWriter出错时,可以根据报错信息改变fourcc的值,或者查看FFmpeg是否安装完善。
可以通过ffmpeg.exe -formats来查看ffmpeg支持的格式
实例程序

#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
    VideoCapture videoCapture("rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov");
    Size size = Size(videoCapture.get(CAP_PROP_FRAME_WIDTH), videoCapture.get(CAP_PROP_FRAME_HEIGHT));
    if (size.width <= 0 || size.height <= 0)
    {
        cout << "ERROR while get the width or the height" << endl;
    }
    else {
        cout << "width: " << size.width << "\nheight: " << size.height << endl;
    }
    int myFourCC = VideoWriter::fourcc('X', 'V', 'I', 'D');//avi
    //int myFourCC = VideoWriter::fourcc('m', 'p', '4', 'v');//mp4
    //'m', 'p', '4', 'v'//'F','L','V','1'//'m', 'p', '4','2' //'m','p','4','v'//'d','i','v','x'
    double rate = videoCapture.get(CAP_PROP_FPS);
    VideoWriter writer("/home/oceanstar/CLionProjects/hello.avi", myFourCC, rate, size, true);
    if (!videoCapture.isOpened()) {
        cout << "Video not open!" << endl;
        return 1;
    }

    Mat frame;
    namedWindow("frame", WINDOW_AUTOSIZE);
    // 通过总帧数来控制拍摄时间,100 = 4s, 200 = 8s
    int count= 100;
    while (count > 0){
            bool ret = videoCapture.read(frame);
            if (ret){
                imshow("frame", frame);
                //writer.write(frame);
                writer << frame;
                if (waitKey(20) > 0)break;
            }else {
                break;
            }
            count--;
    }
    cout << "FINISH ALL WORK----------" << endl;
    videoCapture.release(); //when everything done, release the capture
    writer.release();
    destroyAllWindows();
    system("pause");
    return 0;
}

https://zhuanlan.zhihu.com/p/101383213

https://blog.csdn.net/weixin_40316053/article/details/85256429

在opencv中关于视频的读操作是通过VideoCapture类来完成的;关于视频的写操作是通过VideoWriter类来实现的。

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用OpenCV在Java中获取RTSP视频流可以通过使用OpenCVVideoCapture类实现。首先,您需要创建一个VideoCapture对象,并使用它的open()方法打开RTSP URL:VideoCapture capture = new VideoCapture(); capture.open("rtsp://<username>:<password>@<ip_address>:<port>/<path>"); 然后,您可以使用read()方法从视频流中读取帧:Mat frame = new Mat(); capture.read(frame); ### 回答2: 在Java中使用OpenCV获取RTSP视频流的步骤如下: 1. 首先,确保你已经安装了OpenCV库,并且已经在你的Java项目中引入了相关的库文件。 2. 创建一个新的Java类,并在类中导入OpenCV库: ``` import org.opencv.core.*; import org.opencv.videoio.*; ``` 3. 在代码中创建一个VideoCapture对象,并将RTSP视频流的URL传递给它: ``` String rtspUrl = "rtsp://your_rtsp_video_stream_url"; VideoCapture capture = new VideoCapture(rtspUrl); ``` 4. 检查VideoCapture对象是否已经成功打开了RTSP视频流: ``` if (!capture.isOpened()) { System.out.println("无法打开RTSP视频流!"); return; } ``` 5. 进入一个循环,读取视频帧并进行处理: ``` Mat frame = new Mat(); while (true) { if (capture.read(frame)) { // 在这里对视频帧进行处理,比如显示、保存等 // ... } else { System.out.println("无法读取视频帧!"); break; } } ``` 6. 最后,记得在循环结束后释放VideoCapture对象: ``` capture.release(); ``` 这样,你就可以在Java中使用OpenCV获取RTSP视频流了。注意,根据你所使用的OpenCV版本和操作系统不同,可能需要进行一些额外的配置和处理。 ### 回答3: 在Java中使用OpenCV获取RTSP视频流可以通过以下步骤来实现: 首先,需要确保在项目的构建路径中已经添加了OpenCV库文件。 接下来,可以使用Java中的VideoCapture类来创建一个用于从RTSP源中读取视频流的对象。可以通过传递RTSP流的URL作为参数来完成这一步骤,例如: ```java VideoCapture capture = new VideoCapture("rtsp://username:password@ip_address:port/video_stream"); ``` 在上述代码中,"username"和"password"是RTSP流的访问凭证,"ip_address"是RTSP服务器的IP地址,"port"是RTSP服务器的端口号,"video_stream"是要获取的视频流名称。 接下来,可以使用while循环来持续读取视频帧,并进行处理,直到没有更多的帧可供读取为止。可以使用Mat类来存储每一帧的像素数据,如下所示: ```java Mat frame = new Mat(); while (capture.read(frame)) { // 进行帧处理的代码 } ``` 在循环中,通过调用capture.read(frame)方法来读取每一帧的像素数据,并将其存储在名为"frame"的Mat对象中。可以在循环中加入自己的帧处理代码,例如进行图像处理、人脸检测等。 最后,在不再需要时,记得释放VideoCapture对象的资源,释放内存: ```java capture.release(); ``` 以上就是使用OpenCV在Java中获取RTSP视频流的基本步骤。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值