zoukankan      html  css  js  c++  java
  • 批量分割视频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!!!

  • 相关阅读:
    一个奇怪的网页bug 竟然是局域网DNS搞的鬼
    繁体系统下如何快速将简体安装文件乱码恢复正常?
    Ubuntu16.04LTS国内快速源
    bitnami redmine版本由2.3.1升级至3.2.2过程
    Ubuntu1404安装gogs过程
    AJAX
    jQuery 事件解释
    安装phpMyadmi报错
    总结二
    总结
  • 原文地址:https://www.cnblogs.com/happyamyhope/p/7761493.html
Copyright © 2011-2022 走看看