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
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <string>
#include <iostream>
using namespace std;
using namespace cv;
string getGstDemuxPlugin(string container);
string getGstAvDecodePlugin(string codec);
int main(int argc, char *argv[])
{
const string keys =
"{h help usage ? | | print help messages }"
"{p pipeline |gst-default| pipeline name (supported: 'gst-default', 'gst-vaapi', 'gst-libav', 'ffmpeg') }"
"{ct container |mp4 | container name (supported: 'mp4', 'mov', 'avi', 'mkv') }"
"{cd codec |h264 | codec name (supported: 'h264', 'h265', 'mpeg2', 'mpeg4', 'mjpeg', 'vp8') }"
"{f file path | | path to file }"
"{fm fast | | fast measure fps }";
CommandLineParser parser(argc, argv, keys);
parser.about("This program shows how to read a video file with GStreamer pipeline with OpenCV.");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
bool arg_fast_measure = parser.has("fast"); // fast measure fps
string arg_pipeline = parser.get<string>("pipeline"), // GStreamer pipeline type
arg_container = parser.get<string>("container"), // container type
arg_codec = parser.get<string>("codec"), // codec type
arg_file_name = parser.get<string>("file"); // path to videofile
VideoCapture cap;
if (!parser.check())
{
parser.printErrors();
return 0;
}
// Choose the constructed GStreamer pipeline
if (arg_pipeline.find("gst") == 0)
{
ostringstream pipeline;
pipeline << "filesrc location=\"" << arg_file_name << "\"";
pipeline << " ! " << getGstDemuxPlugin(arg_container);
if (arg_pipeline.find("default") == 4) {
pipeline << " ! decodebin";
}
else if (arg_pipeline.find("vaapi1710") == 4)
{
pipeline << " ! vaapidecodebin";
if (arg_container == "mkv")
{
pipeline << " ! autovideoconvert";
}
else
{
pipeline << " ! video/x-raw, format=YV12";
}
}
else if (arg_pipeline.find("libav") == 4)
{
pipeline << " ! " << getGstAvDecodePlugin(arg_codec);
}
else
{
parser.printMessage();
cout << "Unsupported pipeline: " << arg_pipeline << endl;
return -4;
}
pipeline << " ! videoconvert";
pipeline << " n-threads=" << getNumThreads();
pipeline << " ! appsink sync=false";
cap = VideoCapture(pipeline.str(), CAP_GSTREAMER);
}
else if (arg_pipeline == "ffmpeg")
{
cap = VideoCapture(arg_file_name, CAP_FFMPEG);
}
else
{
parser.printMessage();
cout << "Unsupported pipeline: " << arg_pipeline << endl;
return -4;
}
// Choose a show video or only measure fps
cout << "_____________________________________" << '\n';
cout << "Start measure frame per seconds (fps)" << '\n';
cout << "Loading ..." << '\n';
Mat frame;
vector<double> tick_counts;
cout << "Start decoding: " << arg_file_name;
cout << " (" << arg_pipeline << ")" << endl;
while(true)
{
int64 temp_count_tick = getTickCount();
cap >> frame;
temp_count_tick = getTickCount() - temp_count_tick;
if (frame.empty()) { break; }
tick_counts.push_back(static_cast<double>(temp_count_tick));
if (arg_fast_measure && (tick_counts.size() > 1000)) { break; }
}
double time_fps = sum(tick_counts)[0] / getTickFrequency();
if (tick_counts.size() != 0)
{
cout << "Finished: " << tick_counts.size() << " in " << time_fps <<" sec ~ " ;
cout << tick_counts.size() / time_fps <<" fps " << endl;
}
else
{
cout << "Failed decoding: " << arg_file_name;
cout << " (" << arg_pipeline << ")" << endl;
return -5;
}
return 0;
}
// Choose a video container
string getGstDemuxPlugin(string container) {
if (container == "avi") { return "avidemux"; }
else if (container == "mp4") { return "qtdemux"; }
else if (container == "mov") { return "qtdemux"; }
else if (container == "mkv") { return "matroskademux"; }
return string();
}
// Choose a codec
string getGstAvDecodePlugin(string codec) {
if (codec == "h264") { return "h264parse ! avdec_h264"; }
else if (codec == "h265") { return "h265parse ! avdec_h265"; }
else if (codec == "mpeg2") { return "avdec_mpeg2video"; }
else if (codec == "mpeg4") { return "avdec_mpeg4"; }
else if (codec == "mjpeg") { return "avdec_mjpeg"; }
else if (codec == "vp8") { return "avdec_vp8"; }
return string();
}