如何在C ++中使用OpenCV录制早期视频

时间:2018-03-12 02:43:34

标签: c++ opencv video

我播放视频A,我现在可以逐帧写出另一个视频B. 现在我想尝试将早期帧写入我的视频!
例如:
当视频A播放到第10秒时,我按一个键,它从视频A的第5秒开始录制到视频B,
然后当视频A播放到第15秒时,它会停止录制 因此,视频B的详细信息是视频A的第5至第15秒。

你能帮助我吗? 我使用VS C ++ 2015,OpenCV3.2

这是我目前的代码

while (1)
{
    if (startNewRecording == true)
    {
        startNewRecording = false;
        recording = true;
    }

    char c = (char)waitKey(fps);
    if (stop_run != 0)
    {
        if (!video.read(mat_frame))
            return -1;

        current_pos = (int)video.get(CV_CAP_PROP_POS_FRAMES); //get the frame pos now           

        if (c == 114) //r:start record
        {
            recording = !recording;
            if (recording == true)
                printf("Begin Recording\n");
            else
                printf("Recording Paused\n");
            //insert code here
            if (firstRun)
            {
                writer = VideoWriter(filename, fcc, fps, frameSize);
                if (!writer.isOpened())
                {
                    printf("ERROR OPENING FILE FOR WRITE\n");
                    getchar();
                    return -1;
                }
            }
        }
        else if (c == 115) //s:stop record
        {
            recording = false;
            printf("Stop Recording\n");
        }
        // Record From Earlier




        else
        {
            //setTrackbarPos("Second", "Video", current_pos / fps);
        }

        if (recording == true)
        {
            SYSTEMTIME sys;
            GetLocalTime(&sys);
            sprintf(systime, "%4d/%02d/%02d-%02d:%02d:%02d", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond);

            //writer.write(mat_frame); //只顯示
            putText(mat_frame, systime, Point(5, 40), 6, 1, Scalar(0, 0, 255));
            writer.write(mat_frame); //顯示也寫入
        }
        int h, h1, m, m1, s, s1;
        h = (current_pos / (int)fps) / 3600;
        m = (current_pos / (int)fps) % 3600 / 60;
        s = (current_pos / (int)fps) % 3600 % 60;
        h1 = video_totalsecond / 3600;
        m1 = video_totalsecond % 3600 / 60;
        s1 = video_totalsecond % 3600 % 60;

        sprintf(videotime, "%02d:%02d:%02d-%02d:%02d:%02d", h, m, s, h1, m1, s1);           
        putText(mat_frame, videotime, Point(5, 40), 2, 1, Scalar(0, 0, 255));
        //imwrite("Img000.jpg", mat_frame);
        imshow("Video", mat_frame);
        stop_run += 1;
    }

    if (c == 32) //空白鍵暫停
    {
        if (stop_run>0)
        {
            stop_run = -1;
            cout << "Pause" << endl;
        }
        else
        {
            stop_run = 1;
            cout << "Play" << endl;
        }
    }
    if (c == 27)
        break;
}

0 个答案:

没有答案