批量分割視頻opencv


前言

視頻處理過程中,會用到對等長的視頻進行處理,此時要對大視頻進行分割。

實現步驟

1.批量讀取視頻集;

2.視頻分割;

測試代碼

1.批量讀取視頻集;

/************************************************************************
* Copyright(c) 2017  AMY
* All rights reserved.
*
* File:
* Brief:
* Version: 1.0
* Author:
* Email: 
* Date:    2017/10/30
* History:
* 20171030: ;

************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include "opencv2/opencv.hpp"
#include "opencv2/contrib/contrib.hpp"

using namespace std;
using namespace cv;

void videoSegmentation(string videoName, string videoSegFolder, int segLen, int k);

int main()
{
    string videoFolder = ".\\dataset\\p1";
    string videoSegFolder = ".\\segVideos\\";
    cv::Directory dir;
    string videoType = "*.avi";
    bool addPath = true;
    int segLen = 30;
    vector<string> videosName = dir.GetListFiles(videoFolder, videoType, addPath);
    string videoFile;
    cout << videosName.size() << endl;
    for (int i = 0; i < videosName.size(); i++)
    {
        videoSegmentation(videosName[i], videoSegFolder, segLen, i+1);
    }
    return 0;
}

2.視頻分割;

void videoSegmentation(string videoName, string videoSegFolder, int segLen, int k)
{
    VideoCapture capture(videoName);
    if (!capture.isOpened())
    {
        cout << "Loading error!" << endl;
        abort();
    }
    int nFrame = (int)capture.get(CV_CAP_PROP_POS_FRAMES);
    int fps = (int)capture.get(CV_CAP_PROP_FPS);
    int width = (int)capture.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT);
    Size videoSize(width, height);
    VideoWriter writer;
    string videoSeg = videoSegFolder + to_string(k) + "_1.avi";
    writer.open(videoSeg, CV_FOURCC('D', 'I', 'V', '3'), fps, videoSize);
    Mat frame;
    int videoNum = 0;
    while (1)
    {
        if (capture.read(frame))
        {
            videoNum++;
            writer << frame;
            if (!(videoNum % (fps*segLen)))
            {
                videoSeg = videoSegFolder + to_string(k) + "_" + to_string(videoNum/(fps*segLen)) + ".avi";
                writer.open(videoSeg, CV_FOURCC('D', 'I', 'V', '3'), fps, videoSize);
            }
            else
            {

            }
        }
        else
        {
            break;
        }
    }
    capture.release();
    return;

}
View Code

注意,OpenCV只支持avi的格式,而且生成的視頻文件不能大於2GB,而且不能添加音頻。

補充:

如果生成視頻的顏色不正確,很有可能是中間處理過程中圖片的R/G/B通道發生了改變,也有可能是編解碼格式問題。

牢記:opencv的顏色通道順序默認都是BGR!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM