xuqq

用OpenCV创建视频

目标

你可能已经不满足于读取视频,还想要将你产生的一系列结果保存到一个新建的视频文件中。使用OpenCV中的 VideoWriter 类就可以简单的完成创建视频的工作。在接下来的教程中,我们将告诉你:

  • 如何用OpenCV创建一个视频文件
  • 用OpenCV能创建什么样的视频文件
  • 如何释放视频文件当中的某个颜色通道

为了使例子简单,我就仅仅释放原始视频RGB通道中的一个,并把它放入新视频文件中。你可以使用命令行参数来控制程序的一些行为:

  • 第一个参数指向你需要操作的视频文件。
  • 第二个参数可以是如下的几个字母之一:R G B。用来指定你需要释放哪一个通道。
  • 最后一个参数是Y(Yes)或者N(No). 如果你选择N, 就直接使用视频输入格式来创建输出文件,否则就会弹出一个对话框来让你选择编码器。

举例来说,可以使用下面这样子的命令行:

video-write.exe video/Megamind.avi R Y

源代码

你可以在 samples/cpp/tutorial_code/highgui/video-write/ 文件夹中找到源代码和视频文件。或者从 download it from here这里下载源代码。

 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
#include <iostream> // for standard I/O
#include <string>   // for strings

#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp>  // Video write

using namespace std;
using namespace cv;
int main(int argc, char *argv[], char *window_name)
{
    if (argc != 4)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    const string source      = argv[1];            // the source file name
    const bool askOutputType = argv[3][0] ==\'Y\';  // If false it will use the inputs codec type
   
    VideoCapture inputVideo(source);        // Open input
    if ( !inputVideo.isOpened())
    {
        cout  << "Could not open the input video." << source << endl;
        return -1;
    }

    string::size_type pAt = source.find_last_of(\'.\');   // Find extension point
    const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // Form the new name with container
    int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form

    // Transform from int to char via Bitwise operators
    char EXT[] = {ex & 0XFF , (ex & 0XFF00) >> 8,(ex & 0XFF0000) >> 16,(ex & 0XFF000000) >> 24, 0};

    Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    //Acquire input size
                  (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));    

    VideoWriter outputVideo;                                        // Open the output
    if (askOutputType)
            outputVideo.open(NAME  , ex=-1, inputVideo.get(CV_CAP_PROP_FPS),S, true);  
    else
        outputVideo.open

分类:

技术点:

相关文章:

  • 2022-02-20
  • 2021-12-07
  • 2021-12-04
  • 2021-07-31
  • 2022-12-23
  • 2021-11-08
  • 2021-07-06
  • 2021-12-16
猜你喜欢
  • 2021-04-07
  • 2022-12-23
  • 2021-12-15
  • 2021-05-11
  • 2021-08-30
  • 2021-12-15
  • 2021-10-23
相关资源
相似解决方案