Skip to content

Commit d92f211

Browse files
authored
Create main.cpp
1 parent 717b2dd commit d92f211

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

main.cpp

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <string>
4+
#include <array>
5+
#include <direct.h>
6+
7+
using namespace std;
8+
9+
void print_help()
10+
{
11+
cout << "Video Splitter v1.0" << endl << endl;
12+
cout << "Usage:" << endl;
13+
cout << "video-splitter [options] video-file.mp4" << endl << endl;
14+
cout << "Options:" << endl;
15+
cout << "-c count video count to generate (ignores -s parameter)" << endl;
16+
cout << "-s seconds time for every video (default 30)" << endl;
17+
}
18+
19+
string cmd_exec(const char* cmd)
20+
{
21+
array<char, 128> buffer;
22+
string result;
23+
unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose);
24+
if (!pipe)
25+
{
26+
cerr << "_popen() failed!" << endl;
27+
}
28+
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
29+
{
30+
result += buffer.data();
31+
}
32+
return result;
33+
}
34+
35+
string get_file_extension(const char* file_name)
36+
{
37+
string s = string(file_name);
38+
size_t pos = s.rfind('.');
39+
if (pos != string::npos)
40+
{
41+
return s.substr(pos + 1, s.length() - 1);
42+
}
43+
return "";
44+
}
45+
46+
string get_file_name_without_extension(const char* file_name)
47+
{
48+
string s = string(file_name);
49+
size_t path_pos = s.rfind('\\');
50+
size_t path_pos_2 = s.rfind('/');
51+
if (path_pos_2 != string::npos && path_pos_2 > path_pos)
52+
{
53+
path_pos = path_pos_2;
54+
}
55+
if (path_pos != string::npos)
56+
{
57+
s = s.substr(path_pos + 1, s.length() - 1);
58+
}
59+
size_t dot_pos = s.rfind('.');
60+
if (dot_pos != string::npos)
61+
{
62+
return s.substr(0, dot_pos);
63+
}
64+
return file_name;
65+
}
66+
67+
string get_parent_path(const char* file_name)
68+
{
69+
string s = string(file_name);
70+
size_t path_pos = s.rfind('\\');
71+
size_t path_pos_2 = s.rfind('/');
72+
if (path_pos_2 != string::npos && path_pos_2 > path_pos)
73+
{
74+
path_pos = path_pos_2;
75+
}
76+
if (path_pos != string::npos)
77+
{
78+
return s.substr(0, path_pos + 1); // include path seperator at end
79+
}
80+
return "";
81+
}
82+
83+
int main(int argc, char** argv) {
84+
85+
if (argc == 1)
86+
{
87+
print_help();
88+
return 0;
89+
}
90+
91+
int count = -1;
92+
int part_seconds = 30;
93+
char* file_name = nullptr;
94+
95+
if (argc == 2)
96+
{
97+
file_name = argv[1];
98+
cout << "File name is " << file_name << endl;
99+
}
100+
else if (argc == 4)
101+
{
102+
file_name = argv[3];
103+
cout << "File name is " << file_name << endl;
104+
105+
char* option = argv[1];
106+
107+
if (strcmp("-c", option) == 0)
108+
{
109+
count = atoi(argv[2]);
110+
cout << "-c parameter specified as " << count << endl;
111+
}
112+
else if (strcmp("-s", option) == 0)
113+
{
114+
part_seconds = atoi(argv[2]);
115+
cout << "-s parameter specified as " << part_seconds << endl;
116+
}
117+
else
118+
{
119+
cerr << "Invalid options specified!" << endl;
120+
return -1;
121+
}
122+
}
123+
else
124+
{
125+
cerr << "Invalid parameters!" << endl;
126+
127+
for (int i = 0; i < argc; i++) {
128+
cout << "#" << i << ": " << argv[i] << endl;
129+
}
130+
131+
return -1;
132+
}
133+
134+
string cmd = "ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -i ";
135+
cmd += file_name;
136+
137+
string video_duration = cmd_exec(cmd.c_str());
138+
139+
int video_seconds = atoi(video_duration.c_str());
140+
141+
cout << "Video duration is " << video_seconds << " seconds" << endl;
142+
143+
if (count != -1)
144+
{
145+
part_seconds = video_seconds / count;
146+
cout << "Count parameter using (" << count << "), every part will be " << part_seconds << " seconds" << endl;
147+
}
148+
149+
int part_count = video_seconds / part_seconds + (video_seconds % part_seconds == 0 ? 0 : 1);
150+
151+
cout << part_count << " parts will be generated - durations limited to " << part_seconds << "s" << endl << endl;
152+
153+
string base_name = get_file_name_without_extension(file_name);
154+
string ext = get_file_extension(file_name);
155+
string parent_path = get_parent_path(file_name);
156+
157+
cout << "Detected file base name: " << base_name << endl;
158+
cout << "Detected file extension: " << ext << endl << endl;
159+
160+
for (int i = 0; i < part_count; i++)
161+
{
162+
int ss = (i * part_seconds);
163+
int t = part_seconds;
164+
165+
cout << "# " << (i + 1) << "/" << part_count << " (" << ss << " - ";
166+
cout << ss + (i != part_count -1 ? t : video_seconds - i * part_seconds) << ")" << endl;
167+
168+
ostringstream out_file;
169+
out_file << parent_path << base_name << "_part_" << (i < 9 ? "0" : "") << (i + 1) << "." << ext;
170+
171+
string out_file_name = out_file.str();
172+
173+
cout << out_file_name << endl << endl;
174+
175+
ostringstream cmd;
176+
cmd << "ffmpeg -v error -ss " << ss << " -t " << t << " -i " << file_name << " ";
177+
cmd << out_file_name;
178+
179+
// comment below to bypass execution of ffmpeg!!
180+
cmd_exec(cmd.str().c_str());
181+
}
182+
183+
return 0;
184+
}

0 commit comments

Comments
 (0)