当前位置:   article > 正文

AI视屏处理(C++)_c++ ai视频生成

c++ ai视频生成

opencv读取视频并保存视频(C++)

main()
{
		int iFrameIndex = 0;
		cv::VideoCapture cap;
		cap.open("/root/zhangsong/yolo-tensorrt-master/3.mp4");
		 cv::Mat mat_image;
		std::string videoname = "/root/zhangsong/yolo-tensorrt-master/build/tracker_result.avi";
		int videoFps = cap.get(CV_CAP_PROP_FPS);
		int myFourCC = cv::VideoWriter::fourcc('m', 'p', '4', 'v');
		cv::VideoWriter g_writer = cv::VideoWriter(videoname, myFourCC, videoFps, cv::Size(cap.get(cv::CAP_PROP_FRAME_WIDTH), cap.get(cv::CAP_PROP_FRAME_HEIGHT)));
		while(true)
		{
			cap>>mat_image;
			cv::Mat mat_temp = mat_image.clone();
			iFrameIndex++;
			//cv::imwrite("000269_.jpg",mat_temp);
			g_writer << mat_temp;
			cv::namedWindow("image",0);
			cv::imshow("image", mat_temp);
			cv::waitKey(1);
		}
		g_writer.release();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

opencv读取视频并保存视频(python)

import os
import cv2

if __name__ == "__main__":
    os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

    cap = cv2.VideoCapture('F:/gongye/data/video/IMG_3451.mp4')
    fps = cap.get(cv2.CAP_PROP_FPS)
    g_ntotalFrameNumber = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    print(size)
    # 定义编码格式mpge-4
    fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
    # 定义视频文件输入对象
    savevideopath = 'F:/gongye/data/video/result01.avi'
    outVideo = cv2.VideoWriter(savevideopath, fourcc, fps, size)
    # 获取视频流打开状态
    if cap.isOpened():
        rval, frame = cap.read()
        print('ture')
    else:
        rval = False
        print('False')
    tot = 1
    #c = 1

    while (rval):
        rval, frame = cap.read()  # 返回两个值,第一个为bool类型,如果读到帧返回True,如果没读到帧返回False,第二个值为帧图像
        #cv2.imshow('test', frame)
        # 每间隔20帧保存一张图像帧
        # if tot % 20 ==0 :
        #   cv2.imwrite('cut/'+'cut_'+str(c)+'.jpg',frame)
        #   c+=1

        tot += 1
        print('tot=', tot)
        outVideo.write(frame)

        #cv2.waitKey(1)
    cap.release()
    outVideo.release()
    #cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

将视频截取成图片

#include <QCoreApplication>
#include <iostream>
#include <time.h>
#include<sys/io.h>
#include<iostream>
#include<vector>
#include <string>
#include <list>
#include <dirent.h>
#include <sys/stat.h>
#include<opencv2/opencv.hpp>

using namespace std;
int g_ntotalFrameNumber,g_ncurrentframe;
double g_currentpercent;

#define FRAME_PER_NUME 20
#define FILENAME_SUFFIX_NUM 4
#define INPUT_VIDEO_PATH "/media/t/disk1/HOME/workplace/qt/videotoimage/video/1"
#define OUTPUT_IMAGE_PATH "/media/t/disk1/HOME/workplace/qt/videotoimage/output_image/"

//vector<string> getFiles(const string &folder,const bool all /* = true */)
//{
//    vector<string> files;
//    list<string> subfolders;
//    subfolders.push_back(folder);

//    while (!subfolders.empty()) {
//        string current_folder(subfolders.back());

//        if (*(current_folder.end() - 1) != '/') {    //确保地址是 “ ..../*” 结尾的
//            current_folder.append("/*");
//        }
//        else {
//            current_folder.append("*");
//        }

//        subfolders.pop_back();

//        struct _finddata_t  file_info;
//        long file_handler = _findfirst(current_folder.c_str(), &file_info);

//        while (file_handler != -1)
//        {
//            if (all &&
//                (!strcmp(file_info.name, ".") || !strcmp(file_info.name, ".."))) {
//                if (_findnext(file_handler, &file_info) != 0) break;
//                continue;
//            }
//#if 0
//            斜杆/ 和 反斜杠\ 的区别基本上就是这些了,下面再讨论一下相对路径和绝对路径。
//                ./SRC/  这样写表示,当前目录中的SRC文件夹;
//                ../SRC/  这样写表示,当前目录的上一层目录中SRC文件夹;
//                /SRC/   这样写表示,项目根目录(可以只磁盘根目录,也可以指项目根目录,具体根据实际情况而定)
//#endif

//            if (file_info.attrib & _A_SUBDIR)
//            {
//                // it's a sub folder
//                if (all)
//                {
//                    // will search sub folder
//                    string folder(current_folder);
//                    folder.pop_back();         //  就是减去最后的*     string 的pop_back Delete last character   Erases the last character of the string, effectively reducing its length by one.
//                    folder.append(file_info.name);

//                    subfolders.push_back(folder.c_str());
//                }
//            }
//            else
//            {
//                // it's a file
//                string file_path;
//                // current_folder.pop_back();
//                file_path.assign(current_folder.c_str()).pop_back();
//                file_path.append(file_info.name);

//                files.push_back(file_path);
//            }

//            if (_findnext(file_handler, &file_info) != 0) break;
//        }  // while
//        _findclose(file_handler);
//    }

//    return files;
//}
#if 0
int main(int argc, char *argv[])
{
    time_t t;
    struct tm * lt;
    bool f_stop = false;
    cv::Mat Input;
    std::string input_video_path = INPUT_VIDEO_PATH;

    cv::VideoCapture capture(input_video_path);

     if (!capture.isOpened())
         std::cout << "fail to open!" << std::endl;
     g_ntotalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
     std::cout << "整个视频共" << g_ntotalFrameNumber << "帧" << std::endl;
     //获得帧的宽高
     int w = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
     int h = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
     cv::Size S(w, h);
     // 视频写入对象
     cv::VideoWriter writer("/home/t/result.avi", CV_FOURCC('m', 'p', '4', ' v'),25, cv::Size(1280, 720));
     while (!f_stop )
     {
         time (&t);
         lt = localtime (&t);
         for (int i = 0;i < 25;i++) {
             capture >> Input;
             if(Input.empty())
             {
                 break;
             }
         }
         if(Input.empty())
         {
             break;
         }
         writer<<Input;
         g_currentpercent = int(g_ncurrentframe * 100 / g_ntotalFrameNumber);
         std::stringstream pathROIss;
         std::string pathROIs;
         pathROIss<<"/media/t/disk1/DukTo/Videos/20180518_HIKVISION_HDR_1/20190926"<< lt->tm_hour << lt->tm_min << "_" <<g_ncurrentframe<<".jpg";
         pathROIss>>pathROIs;
         imwrite(pathROIs, Input);
         g_ncurrentframe += 25;
     }
     //释放对象
     capture.release();

    return 0;
}
#endif
#if 1
//int main(int argc, char *argv[])
//{
//    //目标文件夹路径
//    std::string inPath = "/media/t/disk1/DukTo/Videos/*.mp4";//遍历文件夹下的所有.jpg文件
//    //用于查找的句柄
//    long handle;
//    struct _finddata_t fileinfo;
//    //第一次查找
//    handle = _findfirst(inPath.c_str(), &fileinfo);
//    if (handle == -1)
//        return -1;
//    do
//    {
//        //找到的文件的文件名
//        printf("%s\n", fileinfo.name);

//    } while (!_findnext(handle, &fileinfo));

//    _findclose(handle);
//    system("pause");
//    return 0;
//}
void videotoimage(std::string input_video_path, std::string output_image_path, std::string filename)
{
    time_t t;
    struct tm * lt;
    bool f_stop = false;
    cv::Mat Input;

    cv::VideoCapture capture(input_video_path);

     if (!capture.isOpened())
         std::cout << "fail to open!" << std::endl;
     g_ntotalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
     std::cout << "整个视频共" << g_ntotalFrameNumber << "帧" << std::endl;
     //获得帧的宽高
     int w = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
     int h = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
     cv::Size S(w, h);
     // 视频写入对象
     cv::VideoWriter writer("/home/t/result.avi", CV_FOURCC('m', 'p', '4', ' v'),25, cv::Size(1280, 720));
     while (!f_stop )
     {
         time (&t);
         lt = localtime (&t);
         for (int i = 0;i < FRAME_PER_NUME;i++) {
             capture >> Input;
             if(Input.empty())
             {
                 break;
             }
         }
         if(Input.empty())
         {
             break;
         }
         writer<<Input;
         g_currentpercent = int(g_ncurrentframe * 100 / g_ntotalFrameNumber);
         std::stringstream pathROIss;
         std::string pathROIs;
         pathROIss<<output_image_path<<"/"<<filename<<"_"<< lt->tm_hour << lt->tm_min << "_" <<g_ncurrentframe<<".jpg";
         pathROIss>>pathROIs;
         imwrite(pathROIs, Input);
         g_ncurrentframe += FRAME_PER_NUME;
     }
     //释放对象
     capture.release();
    //return 0;
}
int main()
{
    string src_video_path = INPUT_VIDEO_PATH;
    DIR* dir = opendir(src_video_path.data());//打开指定目录
    dirent* p = NULL;//定义遍历指针
    while((p = readdir(dir)) != NULL)//开始逐个遍历
    {
        //这里需要注意,linux平台下一个目录中有"."和".."隐藏文件,需要过滤掉
        if(p->d_name[0] != '.')//d_name是一个char数组,存放当前遍历到的文件名
        {
            g_ncurrentframe = 0;
            string dirpath = OUTPUT_IMAGE_PATH;
//            std::stringstream pathROIss;
//            std::string pathROIs;
//            pathROIss<<"/media/t/disk1/DukTo/Videos"<< lt->tm_hour << lt->tm_min << "_" <<g_ncurrentframe<<".jpg";
//            pathROIss>>pathROIs;
            string str = string(p->d_name);
            cout<<str<<endl;

            string videopath = src_video_path + "/";
            videopath += str;
            //str.erase(remove(str.begin() , str.end() , '.') , str.end() );
            str.erase( str.size()-FILENAME_SUFFIX_NUM, FILENAME_SUFFIX_NUM);
            dirpath += str;
            mkdir(dirpath.data(),S_IRWXU);
            videotoimage(videopath,dirpath,str);
//            string name = "/home/xxx/test/" + str;
//            cout<<name<<endl;
        }
    }
    closedir(dir);//关闭指定目录
}

#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号