forked from cmrd-senya/ffmpegthumbnailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
326 lines (282 loc) · 9.38 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (C) 2010 Dirk Vanden Boer <dirk.vdb@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "config.h"
#include <iostream>
#include <unistd.h>
#include <stdexcept>
#include <stdlib.h>
#include <clocale>
#ifdef ENABLE_GIO
#include <dlfcn.h>
#endif
#include "libffmpegthumbnailer/videothumbnailer.h"
#include "libffmpegthumbnailer/stringoperations.h"
#include "libffmpegthumbnailer/filmstripfilter.h"
using namespace std;
using namespace ffmpegthumbnailer;
void printVersion();
void printUsage();
void tryUriConvert(std::string& filename);
ThumbnailerImageType determineImageTypeFromString(const std::string& filename);
ThumbnailerImageType determineImageTypeFromFilename(const std::string& filename);
int main(int argc, char** argv)
{
int option;
int seekPercentage = 10;
string seekTime;
int thumbnailSize = 128;
int imageQuality = 8;
bool filmStripOverlay = false;
bool workaroundIssues = false;
bool maintainAspectRatio = true;
bool smartFrameSelection = false;
bool preferEmbeddedMetadata = false;
string inputFile;
string outputFile;
string imageFormat;
if (!std::setlocale(LC_CTYPE, ""))
{
std::cerr << "Failed to set locale" << std::endl;
}
while ((option = getopt (argc, argv, "i:o:s:t:q:c:afwhvpm")) != -1)
{
switch (option)
{
case 'a':
maintainAspectRatio = false;
break;
case 'i':
inputFile = optarg;
break;
case 'o':
outputFile = optarg;
break;
case 's':
thumbnailSize = atoi(optarg);
break;
case 'f':
filmStripOverlay = true;
break;
case 'p':
smartFrameSelection = true;
break;
case 'm':
preferEmbeddedMetadata = true;
break;
case 't':
if (string(optarg).find(':') != string::npos)
{
seekTime = optarg;
}
else
{
seekPercentage = atoi(optarg);
}
break;
case 'w':
workaroundIssues = true;
break;
case 'q':
imageQuality = atoi(optarg);
break;
case 'c':
imageFormat = optarg;
break;
case 'h':
printUsage();
return 0;
case 'v':
printVersion();
return 0;
case '?':
default:
cerr << "invalid arguments" << endl;
printUsage();
return -1;
}
}
if (inputFile.empty() || outputFile.empty())
{
cerr << "invalid arguments" << endl;
printUsage();
return 0;
}
if (outputFile == "-" && imageFormat.empty())
{
cerr << "When writing to stdout the image format needs to be specified (e.g.: -c png)" << endl;
return 0;
}
try
{
#ifdef ENABLE_GIO
tryUriConvert(inputFile);
#endif
ThumbnailerImageType imageType = imageFormat.empty() ? determineImageTypeFromFilename(outputFile)
: determineImageTypeFromString(imageFormat);
VideoThumbnailer videoThumbnailer(thumbnailSize, workaroundIssues, maintainAspectRatio, imageQuality, smartFrameSelection);
videoThumbnailer.setLogCallback([] (ThumbnailerLogLevel lvl, const std::string& msg) {
if (lvl == ThumbnailerLogLevelInfo)
std::cout << msg << std::endl;
else
std::cerr << msg << std::endl;
});
FilmStripFilter* filmStripFilter = nullptr;
if (filmStripOverlay)
{
filmStripFilter = new FilmStripFilter();
videoThumbnailer.addFilter(filmStripFilter);
}
videoThumbnailer.setPreferEmbeddedMetadata(preferEmbeddedMetadata);
if (!seekTime.empty())
{
videoThumbnailer.setSeekTime(seekTime);
}
else
{
videoThumbnailer.setSeekPercentage(seekPercentage);
}
videoThumbnailer.generateThumbnail(inputFile, imageType, outputFile);
delete filmStripFilter;
}
catch (exception& e)
{
cerr << "Error: " << e.what() << endl;
return -1;
}
catch (...)
{
cerr << "Unexpected rror" << endl;
return -1;
}
return 0;
}
void printVersion()
{
cout << PACKAGE " version: " PACKAGE_VERSION << endl;
}
void printUsage()
{
cout << "Usage: " PACKAGE " [options]" << endl << endl
<< "Options:" << endl
<< " -i<s> : input file" << endl
<< " -o<s> : output file" << endl
<< " -s<n> : thumbnail size (use 0 for original size) (default: 128)" << endl
<< " -t<n|s> : time to seek to (percentage or absolute time hh:mm:ss) (default: 10%)" << endl
<< " -q<n> : image quality (0 = bad, 10 = best) (default: 8)" << endl
<< " -c : override image format (jpeg or png) (default: determined by filename)" << endl
<< " -a : ignore aspect ratio and generate square thumbnail" << endl
<< " -f : create a movie strip overlay" << endl
//<< " -p : use smarter frame selection (slower)" << endl
<< " -m : prefer embedded image metadata over video content" << endl
<< " -w : workaround issues in old versions of ffmpeg" << endl
<< " -v : print version number" << endl
<< " -h : display this help" << endl;
}
ThumbnailerImageType determineImageTypeFromString(const std::string& type)
{
string lowercaseType = type;
StringOperations::lowercase(lowercaseType);
if (lowercaseType == "png")
{
return Png;
}
if (lowercaseType == "jpeg" || lowercaseType == "jpg")
{
return Jpeg;
}
throw logic_error("Invalid image type specified");
}
#ifdef ENABLE_GIO
typedef void* (*FileCreateFunc)(const char*);
typedef char* (*FileGetFunc)(void* file);
typedef int (*IsNativeFunc)(void* file);
typedef void (*InitFunc)(void);
typedef void (*FreeFunc)(void*);
typedef void (*UnrefFunc)(void*);
class LibHandle
{
public:
LibHandle(const std::string& libName)
: m_pLib(dlopen(libName.c_str(), RTLD_LAZY))
{
if (!m_pLib) cerr << dlerror() << endl;
}
~LibHandle() { if (m_pLib) dlclose(m_pLib); }
operator void*() const { return m_pLib; }
operator bool() const { return m_pLib != nullptr; }
private:
void* m_pLib;
};
void tryUriConvert(std::string& filename)
{
if (filename.find(":") == string::npos)
{
return;
}
LibHandle gLib("libglib-2.0.so.0");
LibHandle gobjectLib("libgobject-2.0.so.0");
LibHandle gioLib("libgio-2.0.so.0");
if (gioLib && gLib && gobjectLib)
{
FileCreateFunc createUriFunc = (FileCreateFunc) dlsym(gioLib, "g_file_new_for_uri");
IsNativeFunc nativeFunc = (IsNativeFunc) dlsym(gioLib, "g_file_is_native");
FileGetFunc getFunc = (FileGetFunc) dlsym(gioLib, "g_file_get_path");
InitFunc initFunc = (InitFunc) dlsym(gobjectLib, "g_type_init");
UnrefFunc unrefFunc = (UnrefFunc) dlsym(gobjectLib, "g_object_unref");
FreeFunc freeFunc = (FreeFunc) dlsym(gLib, "g_free");
if (!(createUriFunc && nativeFunc && getFunc && freeFunc && initFunc && unrefFunc))
{
cerr << "Failed to obtain functions from gio libraries" << endl;
return;
}
initFunc();
void* pFile = createUriFunc(filename.c_str());
if (!pFile)
{
cerr << "Failed to create gio file: " << filename << endl;
return;
}
if (!nativeFunc(pFile))
{
unrefFunc(pFile);
cout << "Not a native file, thumbnailing will likely fail: " << filename << endl;
return;
}
char* pPath = getFunc(pFile);
if (pPath)
{
filename = pPath;
freeFunc(pPath);
}
else
{
cerr << "Failed to get path: " << filename << endl;
}
unrefFunc(pFile);
}
}
#endif
ThumbnailerImageType determineImageTypeFromFilename(const std::string& filename)
{
string lowercaseFilename = filename;
StringOperations::lowercase(lowercaseFilename);
size_t size = lowercaseFilename.size();
if ((lowercaseFilename.substr(size - 5, size) == ".jpeg") || (lowercaseFilename.substr(size - 4, size) == ".jpg"))
{
return Jpeg;
}
return Png;
}