-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.cpp
127 lines (97 loc) · 3.36 KB
/
helpers.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
//
// Created by Trevor Starick on 2016-02-01.
//
#include "helpers.h"
#include "md5.h"
#include <unistd.h>
#include <sys/stat.h>
Helpers::Helpers() {};
Helpers::~Helpers() {};
void Helpers::init() {
struct stat st = {0};
std::string path = "";
char cCurrentPath[FILENAME_MAX];
if (!getcwd(cCurrentPath, sizeof(cCurrentPath))) {
return;
}
cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
pwd = cCurrentPath;
pwd += "/";
path = pwd + "data";
if (stat(path.c_str(), &st) == -1) mkdir(path.c_str(), 0700);
pwd = path + "/";
path = pwd + "video";
if (stat(path.c_str(), &st) == -1) mkdir(path.c_str(), 0700);
path = pwd + "audio";
if (stat(path.c_str(), &st) == -1) mkdir(path.c_str(), 0700);
path = pwd + "tmp";
if (stat(path.c_str(), &st) == -1) mkdir(path.c_str(), 0700);
path = pwd + "media";
if (stat(path.c_str(), &st) == -1) mkdir(path.c_str(), 0700);
path = pwd + "thumbnails";
if (stat(path.c_str(), &st) == -1) mkdir(path.c_str(), 0700);
}
std::string Helpers::exec(const char* cmd) {
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return result;
}
std::string Helpers::replace(const std::string& input, const std::string& regex, const std::string& replace) {
std::regex re(regex);
return std::regex_replace(input, re, replace);
}
std::vector<std::string> Helpers::split(const std::string& input, const std::string& regex) {
// passing -1 as the submatch index parameter performs splitting
std::regex re(regex);
std::sregex_token_iterator
first{input.begin(), input.end(), re, -1},
last;
return {first, last};
}
float Helpers::getMediaLength(std::string filename) {
std::string cmd = "ffprobe -i " + filename + " -show_entries format=duration -v quiet -of csv=\"p=0\"";
auto i = exec(cmd.c_str());
return std::stof(i);
}
std::string Helpers::generateThumbnail(std::string filename) {
auto cmd = "ffmpeg -i " + pwd + "media/" + filename + ".mp4 -y -v error -an -ss 0 -vframes 1 " + pwd + "thumbnails/" + filename + ".png";
exec(cmd.c_str());
return "";
}
std::string Helpers::mergeAV(std::string start, std::string audio, std::string video) {
std::string audioPath = pwd + "tmp/" + audio;
std::string videoPath = pwd + "tmp/" + video;
std::string ext = split(videoPath, "\\.").back();
videoPath = videoPath.substr(0, videoPath.length() - ext.length() - 1);
videoPath += ".mp4";
ext = split(audioPath, "\\.").back();
audioPath = audioPath.substr(0, audioPath.length() - ext.length() - 1);
audioPath += ".m4a";
std::string filename = md5(audio + video);;
std::vector<std::string> args = {
"-y",
"-v", "error",
"-i", audioPath,
"-i", videoPath,
"-map", "0",
"-map", "1",
"-codec", "copy",
"-shortest",
pwd + "media/" + filename + ".mp4"
};
std::string argsString = "";
for(auto x:args) {
argsString += " ";
argsString += x;
}
std::string cmd = "ffmpeg";
cmd += argsString;
exec(cmd.c_str());
return filename;
}