DCL 3.7.4
Loading...
Searching...
No Matches
MediaInfo.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#undef _UNICODE
4#undef UNICODE
5
6#define _UNICODE
7#define UNICODE
8
9#ifdef __GNUC__
10#pragma GCC diagnostic push
11#pragma GCC diagnostic ignored "-Wunused-function"
12#pragma GCC diagnostic ignored "-Wunused-variable"
13#endif
14
15#include "MediaInfoDLL.h"
16
17#ifdef __GNUC__
18#pragma GCC diagnostic pop
19#endif
20
21#ifdef __WINNT__
22 #include <windows.h>
23#else
24 #include <dcl/_dlfcn.h>
25#endif
26
27#include <dcl/Dll.h>
28#include <dcl/Numeric.h>
29#include "MediaInfo.h"
30
31#define __TRACE_THIS 0
32#if __TRACE_THIS
33#define __DCL_TRACE1_N __DCL_TRACE1
34#define __DCL_TRACE2_N __DCL_TRACE2
35#define __DCL_TRACE3_N __DCL_TRACE3
36#define __DCL_TRACE4_N __DCL_TRACE4
37#else
38#define __DCL_TRACE1_N(fmt, arg)
39#define __DCL_TRACE2_N(fmt, arg1, arg2)
40#define __DCL_TRACE3_N(fmt, arg1, arg2, arg3)
41#define __DCL_TRACE4_N(fmt, arg1, arg2, arg3, arg4)
42#endif
43
44__DCL_BEGIN_NAMESPACE
45
46#if __DCL_HAVE_THIS_FILE__
47#undef __THIS_FILE__
48static const char_t __THIS_FILE__[] = __T("media/MediaInfo.cpp");
49#endif
50
53{
54 if (!MediaInfo_Module) {
55 MediaInfoDLL_Load();
56 }
57 if (!MediaInfo_Module) {
58#ifdef __WINNT__
59 throw new DllException(MEDIAINFODLL_NAME, WINAPI_ERROR(GetLastError()));
60#else
61 String msg;
62 __DCL_VERIFY(__dlerror(msg) == 0);
63 throw new DllException(L"" MEDIAINFODLL_NAME, msg);
64#endif
65 };
66}
68{
69 if (MediaInfo_Module) {
70 MediaInfoDLL_UnLoad();
71 }
72}
73
74MediaInfo::MediaInfo(const String& _filename)
76{
77 if (!MediaInfo_Module) {
79 }
80
81 __handle = MediaInfo_New();
82 size_t r = MediaInfo_Open(__handle, _filename);
83 if (r == 0) {
84 throw new IOException(_filename, L"MediaInfo Open Failed");
85 }
86}
87
89{
90 MediaInfo_Close(__handle);
91 MediaInfo_Delete(__handle);
92}
93
94String MediaInfo::inform() const
95{
96 String r = MediaInfo_Inform(__handle, 0);
97 return r;
98}
99
101{
102 return inform().replace_r(L"[ \t]{2,}", L" ", true)
103 .replace_r(L" :", L":", true)
104 .replace_r(L"\r\n", L"\n", true)
105 .replace_r(L"Complete name[^\n]+\n", L"", true)
106 .trim();
107}
108
109String MediaInfo::option(const wchar_t* _option, const wchar_t* _value) const
110{
111 __DCL_ASSERT(_option != NULL);
112 String r = MediaInfo_Option(__handle, _option, _value ? _value : L"");
113 return r;
114}
115
116String MediaInfo::format() const
117{
118 return MediaInfo_Get(__handle, MediaInfo_Stream_General, 0, L"Format",
120}
121
122uint64_t MediaInfo::fileSize() const
123{
124 return UInt64::parse(
125 MediaInfo_Get(__handle, MediaInfo_Stream_General, 0, L"FileSize",
127 10, 0LLU
128 );
129}
130
131unsigned MediaInfo::videoCount() const
132{
133 return UInt32::parse(
134 MediaInfo_Get(__handle, MediaInfo_Stream_General, 0, L"VideoCount",
136 10, 0LU
137 );
138}
139
140unsigned MediaInfo::audioCount() const
141{
142 return UInt32::parse(
143 MediaInfo_Get(__handle, MediaInfo_Stream_General, 0, L"AudioCount",
145 10, 0LU
146 );
147}
148
149unsigned MediaInfo::imageCount() const
150{
151 return UInt32::parse(
152 MediaInfo_Get(__handle, MediaInfo_Stream_General, 0, L"ImageCount",
154 10, 0LU
155 );
156}
157
158String MediaInfo::videoFormat(size_t streamNumber) const
159{
160 return MediaInfo_Get(__handle, MediaInfo_Stream_Video, streamNumber, L"Format",
162}
163
164unsigned MediaInfo::videoWidth(size_t streamNumber) const
165{
166 return UInt32::parse(
167 MediaInfo_Get(__handle, MediaInfo_Stream_Video, streamNumber, L"Width",
169 10, 0LLU
170 );
171}
172
173unsigned MediaInfo::videoHeight(size_t streamNumber) const
174{
175 return UInt32::parse(
176 MediaInfo_Get(__handle, MediaInfo_Stream_Video, streamNumber, L"Height",
178 10, 0LLU
179 );
180}
181
182// Milliseconds
183uint64_t MediaInfo::videoDuration(size_t streamNumber) const
184{
185 return UInt64::parse(
186 MediaInfo_Get(__handle, MediaInfo_Stream_Video, streamNumber, L"Duration",
188 10, 0LLU
189 );
190}
191
192uint32_t MediaInfo::videoDurationAsSeconds(size_t streamNumber) const
193{
194 return (uint32_t) (videoDuration(streamNumber) / 1000);
195}
196
197unsigned MediaInfo::videoBitRate(size_t streamNumber) const
198{
199 return UInt32::parse(
200 MediaInfo_Get(__handle, MediaInfo_Stream_Video, streamNumber, L"BitRate",
202 10, 0LLU
203 );
204}
205
206unsigned MediaInfo::videoBitRateMinimum(size_t streamNumber) const
207{
208 return UInt32::parse(
209 MediaInfo_Get(__handle, MediaInfo_Stream_Video, streamNumber, L"BitRate_Minimum",
211 10, 0LLU
212 );
213}
214
215unsigned MediaInfo::videoBitRateMaximum(size_t streamNumber) const
216{
217 return UInt32::parse(
218 MediaInfo_Get(__handle, MediaInfo_Stream_Video, streamNumber, L"BitRate_Maxmum",
220 10, 0LLU
221 );
222}
223
224String MediaInfo::audioFormat(size_t streamNumber) const
225{
226 return MediaInfo_Get(__handle, MediaInfo_Stream_Audio, streamNumber, L"Format",
228}
229
230// Milliseconds
231uint64_t MediaInfo::audioDuration(size_t streamNumber) const
232{
233 return UInt64::parse(
234 MediaInfo_Get(__handle, MediaInfo_Stream_Audio, streamNumber, L"Duration",
236 10, 0LLU
237 );
238}
239
240uint32_t MediaInfo::audioDurationAsSeconds(size_t streamNumber) const
241{
242 return (uint32_t) (audioDuration(streamNumber) / 1000);
243}
244
245unsigned MediaInfo::audioBitRate(size_t streamNumber) const
246{
247 return UInt32::parse(
248 MediaInfo_Get(__handle, MediaInfo_Stream_Audio, streamNumber, L"BitRate",
250 10, 0LLU
251 );
252}
253
254unsigned MediaInfo::audioBitRateMinimum(size_t streamNumber) const
255{
256 return UInt32::parse(
257 MediaInfo_Get(__handle, MediaInfo_Stream_Audio, streamNumber, L"BitRate_Minimum",
259 10, 0LLU
260 );
261}
262
263unsigned MediaInfo::audioBitRateMaximum(size_t streamNumber) const
264{
265 return UInt32::parse(
266 MediaInfo_Get(__handle, MediaInfo_Stream_Audio, streamNumber, L"BitRate_Maxmum",
268 10, 0LLU
269 );
270}
271
272String MediaInfo::imageFormat(size_t streamNumber) const
273{
274 return MediaInfo_Get(__handle, MediaInfo_Stream_Image, streamNumber, L"Format",
276}
277
278unsigned MediaInfo::imageWidth(size_t streamNumber) const
279{
280 return UInt32::parse(
281 MediaInfo_Get(__handle, MediaInfo_Stream_Image, streamNumber, L"Width",
283 10, 0LLU
284 );
285}
286
287unsigned MediaInfo::imageHeight(size_t streamNumber) const
288{
289 return UInt32::parse(
290 MediaInfo_Get(__handle, MediaInfo_Stream_Image, streamNumber, L"Height",
292 10, 0LLU
293 );
294}
295
296static const wchar_t* __formatToType__[] = {
297 L"ShockWave", L"x-shockwave-flash"
298 , L"Flash Video", L"x-flv"
299 , L"AVI", L"avi"
300 , L"MPEG Audio", L"mpeg"
301 , L"MPEG Video", L"mpeg"
302 , L"CDXA/MPEG-PS", L"mp2p"
303 , L"MPEG-PS", L"mp2p"
304 , L"MPEG-4", L"mp4"
305 , L"OGG", L"ogg"
306 , L"WebM", L"webm"
307};
308
310{
311 if (imageCount() > 0) {
312 return L"image/" + format().toLowerCase();
313 }
314
315 String format = this->format();
316 const wchar_t* type = NULL;
317 for (size_t i = 0; i < __countof(__formatToType__, wchar_t*);
318 i++, i++) {
319 if (format.compareNoCase(__formatToType__[i]) == 0) {
320 type = __formatToType__[i + 1];
321 break;
322 }
323 }
324
325 StringBuilder r;
326 if (type != NULL) {
327 if (videoCount() > 0) {
328 r = L"video/";
329 }
330 else if (audioCount() > 0) {
331 r = L"audio/";
332 }
333 else {
334 r = L"application/";
335 }
336 r += type;
337 }
338 else {
339 r = L"application/octet-stream";
340 }
341 return r;
342}
343
344__DCL_END_NAMESPACE
DCLCAPI int __dlerror(String &r)
Definition _dlfcn.cpp:35
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
#define __countof(array, type)
Definition Config.h:336
wchar_t char_t
Definition Config.h:247
#define __DCL_THROWS2(e1, e2)
Definition Config.h:153
#define __DCL_THROWS1(e)
Definition Config.h:152
#define WINAPI_ERROR(n)
Definition Exception.h:72
IOException *size_t r
Definition MediaInfo.cpp:82
#define MEDIAINFODLL_NAME
@ MediaInfo_Stream_Audio
@ MediaInfo_Stream_Image
@ MediaInfo_Stream_General
@ MediaInfo_Stream_Video
@ MediaInfo_Info_Name
@ MediaInfo_Info_Text
#define __DCL_VERIFY(expr)
Definition Object.h:396
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define __T(str)
Definition Object.h:60
uint64_t fileSize() const
static void cleanup()
Definition MediaInfo.cpp:67
String videoFormat(size_t streamNumber) const
unsigned videoCount() const
unsigned imageHeight(size_t streamNumber) const
IOException * ~MediaInfo()
Definition MediaInfo.cpp:88
uint32_t audioDurationAsSeconds(size_t streamNumber) const
String inform_r() const
unsigned audioBitRateMaximum(size_t streamNumber) const
String format() const
unsigned videoWidth(size_t streamNumber) const
unsigned imageCount() const
unsigned videoBitRateMinimum(size_t streamNumber) const
String imageFormat(size_t streamNumber) const
String contentType() const
uint32_t videoDurationAsSeconds(size_t streamNumber) const
unsigned audioBitRateMinimum(size_t streamNumber) const
unsigned videoHeight(size_t streamNumber) const
uint64_t audioDuration(size_t streamNumber) const
unsigned videoBitRate(size_t streamNumber) const
unsigned videoBitRateMaximum(size_t streamNumber) const
String inform() const
Definition MediaInfo.cpp:94
static void initialize() __DCL_THROWS1(DllException *)
Definition MediaInfo.cpp:51
MediaInfo(const String &_filename) __DCL_THROWS2(DllException *
unsigned audioCount() const
String option(const wchar_t *_option, const wchar_t *_value=NULL) const
uint64_t videoDuration(size_t streamNumber) const
unsigned audioBitRate(size_t streamNumber) const
unsigned imageWidth(size_t streamNumber) const
String audioFormat(size_t streamNumber) const
static uint32_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:391
static uint64_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:623