-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
124 lines (96 loc) · 2.98 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
#include <iostream>
#include <string>
#include <quvi/quvi.h>
#include <cstdlib>
struct MediaInfo
{
bool IsValid;
std::string Url;
std::string Title;
std::string Id;
int ContentLength;
std::string ContentType;
std::string Extension;
int Duration;
MediaInfo() : IsValid(false) {}
};
MediaInfo ExtractMediaFromUrl(const std::string& url)
{
MediaInfo info;
quvi_t q;
quvi_init(&q);
//char *avail_formats;
//quvi_query_formats(q, (char*)url.c_str(), &avail_formats);
//std::cout << avail_formats << std::endl;
// Get the best possible quality
quvi_setopt(q, QUVIOPT_FORMAT, "best"); // Could be any of the avail_formats list
quvi_media_t m;
if(quvi_parse(q, (char*)url.c_str(), &m) != QUVI_OK)
return info;
char* prop;
quvi_getprop(m, QUVIPROP_MEDIAURL, &prop);
info.Url = (prop ? prop : "");
quvi_getprop(m, QUVIPROP_PAGETITLE, &prop);
info.Title = (prop ? prop : "");
quvi_getprop(m, QUVIPROP_MEDIAID, &prop);
info.Id = (prop ? prop : "");
//quvi_getprop(m, QUVIPROP_MEDIACONTENTLENGTH, &prop);
//info.ContentLength = (prop ? prop : "");
quvi_getprop(m, QUVIPROP_MEDIACONTENTTYPE, &prop);
info.ContentType = (prop ? prop : "");
quvi_getprop(m, QUVIPROP_FILESUFFIX, &prop);
info.Extension = (prop ? prop : "");
//quvi_getprop(m, QUVIPROP_MEDIADURATION, &prop);
//info.Duration = (prop ? prop : "");
//std::cout << "video url: " << mediaUrl << std::endl;
quvi_parse_close(&m);
quvi_close(&q);
info.IsValid = true;
return info;
}
void ShowUsage()
{
std::cout << "Usage: vidfetch [-format] <url>" << std::endl;
std::cout << "\treplace 'format' by the destination file format (the file extension)" << std::endl;
std::cout << "\tIf no format is specified, no conversion occurs" << std::endl;
}
int main(int argc, char **argv)
{
if(argc == 2 && std::string(argv[1]) == "--help")
{
ShowUsage();
return 0;
}
if(argc < 2)
{
ShowUsage();
return 1;
}
std::string format;
std::string url;
for(int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if(arg[0] == '-')
format = arg.substr(1);
else
url = arg;
}
MediaInfo info = ExtractMediaFromUrl(url);
if(info.IsValid)
{
std::cout << info.Url << std::endl;
std::cout << info.Title << std::endl;
std::cout << info.Extension << std::endl;
}
std::string origFilename = info.Title + "." + info.Extension;
system(("curl -C - \"" + info.Url + "\" -o \"" + origFilename + "\"").c_str());
// If conversion needed (if a conversion format has been specified):
if(format != "")
{
std::string finalFilename = info.Title + "." + format;
system(("ffmpeg -i \"" + origFilename + "\" -qscale 0 \"" + finalFilename + "\"").c_str());
// Remove original
system(("rm \"" + origFilename + "\"").c_str());
}
}