#include <iostream>
#include <map>
inline map<string, int> fourccByCodec() {
map<string, int> res;
res["h264"] = VideoWriter::fourcc('H', '2', '6', '4');
res["h265"] = VideoWriter::fourcc('H', 'E', 'V', 'C');
res["mpeg2"] = VideoWriter::fourcc('M', 'P', 'E', 'G');
res["mpeg4"] = VideoWriter::fourcc('M', 'P', '4', '2');
res["mjpeg"] = VideoWriter::fourcc('M', 'J', 'P', 'G');
res["vp8"] = VideoWriter::fourcc('V', 'P', '8', '0');
return res;
}
static Size parseResolution(
const string& resolution) {
stringstream ss(resolution);
int width, height;
char delimiter;
ss >> width >> delimiter >> height;
if (ss.fail() || delimiter != 'x') {
throw runtime_error("Invalid resolution format. Please provide in WxH format.");
}
return Size(width, height);
}
int main(
int argc,
char** argv) {
"{help h usage ? | | Print help message }"
"{fps |30 | fix frame per second for encoding (supported: fps > 0) }"
"{codec |mjpeg | Supported codecs depend on OpenCV Configuration. Try one of 'h264', 'h265', 'mpeg2', 'mpeg4', 'mjpeg', 'vp8' }"
"{resolution |1280x720 | Video resolution in WxH format, (e.g., '1920x1080') }"
"{output_filepath | output.avi | Path to the output video file}";
parser.about("Video Capture and Write with codec and resolution options");
if (parser.has("help")) {
parser.printMessage();
return 0;
}
double fps = parser.get<double>("fps");
string codecStr = parser.get<string>("codec");
string resolutionStr = parser.get<string>("resolution");
string outputFilepath = parser.get<string>("output_filepath");
auto codecMap = fourccByCodec();
if (codecMap.find(codecStr) == codecMap.end()) {
cerr << "Invalid codec" << endl;
return -1;
}
int codec = codecMap[codecStr];
Size resolution = parseResolution(resolutionStr);
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
cout << "Selected FPS: " << fps << endl;
cout << "Selected Codec: " << codecStr << endl;
cout <<
"Selected Resolution: " << resolutionStr <<
" (" << resolution.
width <<
"x" << resolution.
height <<
")" << endl;
VideoWriter writer(outputFilepath, codec, fps, resolution,
true);
if (!writer.isOpened()) {
cerr << "Could not open the output video file for write\n";
return -1;
}
cout << "Writing video file: " << outputFilepath << endl
<< "Press any key to terminate" << endl;
for (;;) {
if (!cap.read(frame) || frame.
empty()) {
break;
}
resize(frame, resizedFrame, resolution);
writer.write(resizedFrame);
}
return 0;
}
Designed for command line parsing.
Definition utility.hpp:890
n-dimensional dense array class
Definition mat.hpp:951
bool empty() const
Returns true if the array has no elements.
Template class for specifying the size of an image or rectangle.
Definition types.hpp:338
_Tp height
the height
Definition types.hpp:366
_Tp width
the width
Definition types.hpp:365
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:727
Video writer class.
Definition videoio.hpp:994
std::string String
Definition cvstd.hpp:151
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3