forked from francescolonardo/ScreenRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
121 lines (103 loc) · 3 KB
/
main.cpp
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
#include "ScreenRecorder.h"
#include <regex>
#define DEFAULT_VIDEO_FPS "15"
string area_size;
string area_offsets;
string audio_flag_str;
bool audio_flag;
string out_filename;
void getArgumentsFromMenu()
{
cout << "*****************************************" << endl;
cout << "* ScreenRecorder menu *" << endl;
cout << "*****************************************" << endl;
cout << "- Set the area size to be recorded: its format must be `widthxheight` (e.g. 1920x1200) -> ";
cin >> area_size;
cout << "- Set the area offsets: its format must be `x_offset,y_offset` (e.g. 0,0) -> ";
cin >> area_offsets;
cout << "- Do you want to record the microphone audio? (press `1` if yes, `0` if no) -> ";
cin >> audio_flag_str;
audio_flag = audio_flag_str == "1" ? true : false;
cout << "- Set the output filename, the extension must be `.mp4` (e.g. output_video.mp4) -> ";
cin >> out_filename;
}
bool checkArgumentsNumber(int argc)
{
if (argc == 1)
{
getArgumentsFromMenu();
return true;
}
else
{
if (argc < 5)
{
cerr << "Missing arguments!" << endl;
cout << "Usage: ./main widthxheight x_offset,y_offset audio_flag output_filename.mp4" << endl;
cout << "Example: ./main 1920x1200 0,0 1 output_video.mp4" << endl;
return false;
}
else if (argc > 5)
{
cerr << "Too much arguments!" << endl;
cout << "Usage: ./main widthxheight x_offset,y_offset audio_flag output_filename.mp4" << endl;
cout << "Example: ./main 1920x1200 0,0 1 output_video.mp4" << endl;
return false;
}
else
return true;
}
}
bool checkArgumentsFormat(char const *argv[])
{
string arguments_check_errors = "";
regex area_size_rx("[0-9]+(x[0-9]+)+");
if (regex_match(argv[1], area_size_rx))
area_size = argv[1];
else
arguments_check_errors += "Check the area size's format: it must be `widthxheight` (e.g. 1920x1200)\n";
regex area_offsets_rx("[0-9]+(,[0-9]+)+");
if (regex_match(argv[2], area_offsets_rx))
area_offsets = argv[2];
else
arguments_check_errors += "Check the area offsets' format: it must be `x_offset,y_offset` (e.g. 0,0)\n";
if (atoi(argv[3]) == 0 || atoi(argv[3]) == 1)
audio_flag = atoi(argv[3]) == 1 ? true : false;
else
arguments_check_errors += "Check the audio flag's format: it must be `0` or `1`\n";
regex out_filename_rx(".*\\.mp4$");
if (regex_match(argv[4], out_filename_rx))
out_filename = argv[4];
else
arguments_check_errors += "Check the output filename's extension: it must be `.mp4` (e.g. output_video.mp4)\n";
if (arguments_check_errors != "")
{
cerr << arguments_check_errors << endl;
return false;
}
else
return true;
}
int main(int argc, char const *argv[])
{
try
{
// check arguments' number
if (!checkArgumentsNumber(argc))
exit(1);
if (argc != 1)
{
// check arguments' format
if (!checkArgumentsFormat(argv))
exit(1);
}
ScreenRecorder sr{area_size, area_offsets, DEFAULT_VIDEO_FPS, audio_flag, out_filename};
sr.record();
}
catch (const exception &ex)
{
cerr << endl
<< ex.what() << endl;
}
return 0;
}