-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
184 lines (153 loc) · 4.25 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
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
#include <iostream>
#include <sstream>
#include <string>
#include <array>
#include <direct.h>
using namespace std;
void print_help()
{
cout << "Video Splitter v1.0" << endl << endl;
cout << "Usage:" << endl;
cout << "video-splitter [options] video-file.mp4" << endl << endl;
cout << "Options:" << endl;
cout << "-c count video count to generate (ignores -s parameter)" << endl;
cout << "-s seconds time for every video (default 30)" << endl;
}
string cmd_exec(const char* cmd)
{
array<char, 128> buffer;
string result;
unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose);
if (!pipe)
{
cerr << "_popen() failed!" << endl;
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
result += buffer.data();
}
return result;
}
string get_file_extension(const char* file_name)
{
string s = string(file_name);
size_t pos = s.rfind('.');
if (pos != string::npos)
{
return s.substr(pos + 1, s.length() - 1);
}
return "";
}
string get_file_name_without_extension(const char* file_name)
{
string s = string(file_name);
size_t path_pos = s.rfind('\\');
size_t path_pos_2 = s.rfind('/');
if (path_pos_2 != string::npos && path_pos_2 > path_pos)
{
path_pos = path_pos_2;
}
if (path_pos != string::npos)
{
s = s.substr(path_pos + 1, s.length() - 1);
}
size_t dot_pos = s.rfind('.');
if (dot_pos != string::npos)
{
return s.substr(0, dot_pos);
}
return file_name;
}
string get_parent_path(const char* file_name)
{
string s = string(file_name);
size_t path_pos = s.rfind('\\');
size_t path_pos_2 = s.rfind('/');
if (path_pos_2 != string::npos && path_pos_2 > path_pos)
{
path_pos = path_pos_2;
}
if (path_pos != string::npos)
{
return s.substr(0, path_pos + 1); // include path seperator at end
}
return "";
}
int main(int argc, char** argv) {
if (argc == 1)
{
print_help();
return 0;
}
int count = -1;
int part_seconds = 30;
char* file_name = nullptr;
if (argc == 2)
{
file_name = argv[1];
cout << "File name is " << file_name << endl;
}
else if (argc == 4)
{
file_name = argv[3];
cout << "File name is " << file_name << endl;
char* option = argv[1];
if (strcmp("-c", option) == 0)
{
count = atoi(argv[2]);
cout << "-c parameter specified as " << count << endl;
}
else if (strcmp("-s", option) == 0)
{
part_seconds = atoi(argv[2]);
cout << "-s parameter specified as " << part_seconds << endl;
}
else
{
cerr << "Invalid options specified!" << endl;
return -1;
}
}
else
{
cerr << "Invalid parameters!" << endl;
for (int i = 0; i < argc; i++) {
cout << "#" << i << ": " << argv[i] << endl;
}
return -1;
}
string cmd = "ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -i ";
cmd += file_name;
string video_duration = cmd_exec(cmd.c_str());
int video_seconds = atoi(video_duration.c_str());
cout << "Video duration is " << video_seconds << " seconds" << endl;
if (count != -1)
{
part_seconds = video_seconds / count;
cout << "Count parameter using (" << count << "), every part will be " << part_seconds << " seconds" << endl;
}
int part_count = video_seconds / part_seconds + (video_seconds % part_seconds == 0 ? 0 : 1);
cout << part_count << " parts will be generated - durations limited to " << part_seconds << "s" << endl << endl;
string base_name = get_file_name_without_extension(file_name);
string ext = get_file_extension(file_name);
string parent_path = get_parent_path(file_name);
cout << "Detected file base name: " << base_name << endl;
cout << "Detected file extension: " << ext << endl << endl;
for (int i = 0; i < part_count; i++)
{
int ss = (i * part_seconds);
int t = part_seconds;
cout << "# " << (i + 1) << "/" << part_count << " (" << ss << " - ";
cout << ss + (i != part_count -1 ? t : video_seconds - i * part_seconds) << ")" << endl;
ostringstream out_file;
out_file << parent_path << base_name << "_part_" << (i < 9 ? "0" : "") << (i + 1) << "." << ext;
string out_file_name = out_file.str();
cout << out_file_name << endl << endl;
ostringstream cmd;
cmd << "ffmpeg -v error -ss " << ss << " -t " << t << " -i " << file_name << " ";
cmd << out_file_name;
// comment below to bypass execution of ffmpeg!!
cmd_exec(cmd.str().c_str());
}
return 0;
}