/
MediaInfoWrapper.cs
628 lines (550 loc) · 19.8 KB
/
MediaInfoWrapper.cs
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#region Copyright (C) 2005-2011 Team MediaPortal
// Copyright (C) 2005-2011 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal 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.
//
// MediaPortal 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 MediaPortal. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
using MediaPortal.GUI.Library;
using MediaPortal.Profile;
using MediaPortal.Util;
#region API
# endregion
namespace MediaPortal.Player
{
public class MediaInfoWrapper
{
#region private vars
private MediaInfo _mI = null;
//Video
private double _framerate = 0;
private int _width = 0;
private int _height = 0;
private string _aspectRatio = string.Empty;
private string _videoCodec = string.Empty;
private string _scanType = string.Empty;
private bool _isInterlaced = false;
private string _videoResolution = string.Empty;
private int _videoDuration = 0;
private bool _DVDenabled = false;
private string _ParseSpeed;
//Audio
private int _audioRate = 0;
private int _audioChannels = 0;
private string _audioChannelsFriendly = string.Empty;
private string _audioCodec = string.Empty;
//Detection
private bool _hasAudio;
private bool _hasVideo;
//Subtitles
private int _numsubtitles = 0;
private bool _hasSubtitles = false;
private static HashSet<string> _subTitleExtensions = new HashSet<string>();
private bool _mediaInfoNotloaded = false;
#endregion
#region ctor's
public static bool MediaInfoExist()
{
string dll = Configuration.Config.GetFolder(Configuration.Config.Dir.Base) + "\\MediaInfo.dll";
bool enable = File.Exists(dll);
if (!enable)
{
Log.Warn("MediaInfoWrapper: disabled because \"{0}\" is missing", dll);
}
return enable;
}
public MediaInfoWrapper(string strFile)
{
if (!MediaInfoExist())
{
_mediaInfoNotloaded = true;
return;
}
using (Settings xmlreader = new MPSettings())
{
_DVDenabled = xmlreader.GetValueAsBool("dvdplayer", "mediainfoused", false);
_ParseSpeed = xmlreader.GetValueAsString("debug", "MediaInfoParsespeed", "0.3");
// fix delay introduced after 0.7.26: http://sourceforge.net/tracker/?func=detail&aid=3013548&group_id=86862&atid=581181
}
bool isTV = Util.Utils.IsLiveTv(strFile);
bool isRadio = Util.Utils.IsLiveRadio(strFile);
bool isRTSP = Util.Utils.IsRTSP(strFile); //rtsp for live TV and recordings.
bool isDVD = Util.Utils.IsDVD(strFile);
bool isVideo = Util.Utils.IsVideo(strFile);
bool isAVStream = Util.Utils.IsAVStream(strFile); //other AV streams
//currently disabled for all tv/radio/streaming video
if (isTV || isRadio || isRTSP || isAVStream)
{
Log.Debug("MediaInfoWrapper: isTv:{0}, isRadio:{1}, isRTSP:{2}, isAVStream:{3}", isTV, isRadio, isRTSP,
isAVStream);
Log.Debug("MediaInfoWrapper: disabled for this content");
_mediaInfoNotloaded = true;
return;
}
if (strFile.ToLowerInvariant().EndsWith(".wtv"))
{
Log.Debug("MediaInfoWrapper: WTV file is not handled");
_mediaInfoNotloaded = true;
return;
}
// Check if video file is from image file
string vDrive = DaemonTools.GetVirtualDrive();
string bDrive = Path.GetPathRoot(strFile);
if (vDrive == Util.Utils.RemoveTrailingSlash(bDrive))
isDVD = false;
//currently mediainfo is only used for local video related material (if enabled)
if ((!isVideo && !isDVD) || (isDVD && !_DVDenabled))
{
Log.Debug("MediaInfoWrapper: isVideo:{0}, isDVD:{1}[enabled:{2}]", isVideo, isDVD, _DVDenabled);
Log.Debug("MediaInfoWrapper: disabled for this content");
_mediaInfoNotloaded = true;
return;
}
try
{
_mI = new MediaInfo();
_mI.Option("ParseSpeed", _ParseSpeed);
if (Util.VirtualDirectory.IsImageFile(System.IO.Path.GetExtension(strFile)))
{
strFile = Util.DaemonTools.GetVirtualDrive() + @"\VIDEO_TS\VIDEO_TS.IFO";
if (!File.Exists(strFile))
{
strFile = Util.DaemonTools.GetVirtualDrive() + @"\BDMV\index.bdmv";
if (!File.Exists(strFile))
{
_mediaInfoNotloaded = true;
return;
}
}
}
if (strFile.ToLowerInvariant().EndsWith(".ifo"))
{
string path = Path.GetDirectoryName(strFile);
string mainTitle = GetLargestFileInDirectory(path, "VTS_*1.VOB");
string titleSearch = Path.GetFileName(mainTitle);
titleSearch = titleSearch.Substring(0, titleSearch.LastIndexOf('_')) + "*.VOB";
string[] vobs = Directory.GetFiles(path, titleSearch, SearchOption.TopDirectoryOnly);
foreach (string vob in vobs)
{
int vobDuration = 0;
_mI.Open(vob);
int.TryParse(_mI.Get(StreamKind.General, 0, "Duration"), out vobDuration);
_mI.Close();
_videoDuration += vobDuration;
}
// get all other info from main title's 1st vob
strFile = mainTitle;
}
else if (strFile.ToLowerInvariant().EndsWith(".bdmv"))
{
string path = Path.GetDirectoryName(strFile) + @"\STREAM";
strFile = GetLargestFileInDirectory(path, "*.m2ts");
}
if (strFile != null)
{
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: Opening file : {0}", strFile);
_mI.Open(strFile);
}
else
{
_mediaInfoNotloaded = true;
return;
}
NumberFormatInfo providerNumber = new NumberFormatInfo();
providerNumber.NumberDecimalSeparator = ".";
//Video
double.TryParse(_mI.Get(StreamKind.Video, 0, "FrameRate"), NumberStyles.AllowDecimalPoint, providerNumber,
out _framerate);
int.TryParse(_mI.Get(StreamKind.Video, 0, "Width"), out _width);
int.TryParse(_mI.Get(StreamKind.Video, 0, "Height"), out _height);
_aspectRatio = _mI.Get(StreamKind.Video, 0, "DisplayAspectRatio");
if ((_aspectRatio == "4:3") || (_aspectRatio == "1.333"))
{
_aspectRatio = "fullscreen";
}
else
{
_aspectRatio = "widescreen";
}
_videoCodec = GetFullCodecName(StreamKind.Video);
_scanType = _mI.Get(StreamKind.Video, 0, "ScanType").ToLowerInvariant();
_isInterlaced = _scanType.Contains("interlaced");
if (_width >= 1280 || _height >= 720)
{
_videoResolution = "HD";
}
else
{
_videoResolution = "SD";
}
if (_videoResolution == "HD")
{
if ((_width >= 7680 || _height >= 4320) && !_isInterlaced)
{
if (File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\4320P.png")) ||
File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\resolution\4320P.png")))
{
_videoResolution = "4320P";
}
}
else if ((_width >= 3840 || _height >= 2160) && !_isInterlaced)
{
if (File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\2160P.png")) ||
File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\resolution\2160P.png")))
{
_videoResolution = "2160P";
}
}
else if ((_width >= 1920 || _height >= 1080) && _isInterlaced)
{
_videoResolution = "1080I";
}
else if ((_width >= 1920 || _height >= 1080) && !_isInterlaced)
{
_videoResolution = "1080P";
}
else if ((_width >= 1280 || _height >= 720) && !_isInterlaced)
{
_videoResolution = "720P";
}
}
else
{
if (_height >= 576)
{
if (File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\576.png")) ||
File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\resolution\576.png")))
{
_videoResolution = "576";
}
}
else if (_height >= 480)
{
if (File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\480.png")) ||
File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\resolution\480.png")))
{
_videoResolution = "480";
}
}
else if (_height >= 360)
{
if (File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\360.png")) ||
File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\resolution\360.png")))
{
_videoResolution = "360";
}
}
else if (_height >= 240)
{
if (File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\240.png")) ||
File.Exists(GUIGraphicsContext.GetThemedSkinFile(@"\Media\Logos\resolution\240.png")))
{
_videoResolution = "240";
}
}
}
if (_videoDuration == 0)
{
int.TryParse(_mI.Get(StreamKind.Video, 0, "Duration"), out _videoDuration);
}
//Audio
int iAudioStreams = _mI.Count_Get(StreamKind.Audio);
for (int i = 0; i < iAudioStreams; i++)
{
int intValue;
string sChannels = _mI.Get(StreamKind.Audio, i, "Channel(s)").Split(new char[] {'/'})[0].Trim();
if (int.TryParse(sChannels, out intValue) && intValue > _audioChannels)
{
int.TryParse(_mI.Get(StreamKind.Audio, i, "SamplingRate"), out _audioRate);
_audioChannels = intValue;
_audioCodec = GetFullCodecName(StreamKind.Audio, i);
}
}
switch (_audioChannels)
{
case 8:
_audioChannelsFriendly = "7.1";
break;
case 7:
_audioChannelsFriendly = "6.1";
break;
case 6:
_audioChannelsFriendly = "5.1";
break;
case 2:
_audioChannelsFriendly = "stereo";
break;
case 1:
_audioChannelsFriendly = "mono";
break;
default:
_audioChannelsFriendly = _audioChannels.ToString();
break;
}
//Detection
_hasAudio = _mI.Count_Get(StreamKind.Audio) > 0;
_hasVideo = _mI.Count_Get(StreamKind.Video) > 0;
//Subtitles
_numsubtitles = _mI.Count_Get(StreamKind.Text);
if (checkHasExternalSubtitles(strFile))
{
_hasSubtitles = true;
}
else
{
_hasSubtitles = _numsubtitles > 0;
}
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: DLL Version : {0}", _mI.Option("Info_Version"));
Log.Info("MediaInfoWrapper.MediaInfoWrapper: Inspecting media : {0}", strFile);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: Parse speed : {0}", _ParseSpeed);
//Video
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: FrameRate : {0}", _framerate);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: Width : {0}", _width);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: Height : {0}", _height);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: AspectRatio : {0}", _aspectRatio);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: VideoCodec : {0} [ \"{1}.png\" ]", _videoCodec,
Util.Utils.MakeFileName(_videoCodec).ToLowerInvariant());
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: Scan type : {0}", _scanType);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: IsInterlaced : {0}", _isInterlaced);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: VideoResolution : {0}", _videoResolution);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: VideoDuration : {0}", _videoDuration);
//Audio
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: AudioRate : {0}", _audioRate);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: AudioChannels : {0} [ \"{1}.png\" ]", _audioChannels,
_audioChannelsFriendly);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: AudioCodec : {0} [ \"{1}.png\" ]", _audioCodec,
Util.Utils.MakeFileName(_audioCodec).ToLowerInvariant());
//Detection
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: HasAudio : {0}", _hasAudio);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: HasVideo : {0}", _hasVideo);
//Subtitles
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: HasSubtitles : {0}", _hasSubtitles);
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: NumSubtitles : {0}", _numsubtitles);
}
catch (Exception)
{
Log.Error(
"MediaInfoWrapper.MediaInfoWrapper: Error occurred while scanning media: '{0}'",
strFile);
}
finally
{
if (_mI != null)
{
_mI.Close();
Log.Debug("MediaInfoWrapper.MediaInfoWrapper: Closing file : {0}", strFile);
}
}
}
#endregion
#region private methods
private string GetLargestFileInDirectory(string targetDir, string fileMask)
{
string largestFile = null;
long largestSize = 0;
DirectoryInfo dir = new DirectoryInfo(targetDir);
try
{
FileInfo[] files = dir.GetFiles(fileMask, SearchOption.TopDirectoryOnly);
foreach (FileInfo file in files)
{
long fileSize = file.Length;
if (fileSize > largestSize)
{
largestSize = fileSize;
largestFile = file.FullName;
}
}
}
catch (Exception e)
{
Log.Error("Error while retrieving files for: {0} {1}" + targetDir, e.Message);
}
return largestFile;
}
private bool checkHasExternalSubtitles(string strFile)
{
if (_subTitleExtensions.Count == 0)
{
// load them in first time
_subTitleExtensions.Add(".aqt");
_subTitleExtensions.Add(".asc");
_subTitleExtensions.Add(".ass");
_subTitleExtensions.Add(".dat");
_subTitleExtensions.Add(".dks");
_subTitleExtensions.Add(".idx");
_subTitleExtensions.Add(".js");
_subTitleExtensions.Add(".jss");
_subTitleExtensions.Add(".lrc");
_subTitleExtensions.Add(".mpl");
_subTitleExtensions.Add(".ovr");
_subTitleExtensions.Add(".pan");
_subTitleExtensions.Add(".pjs");
_subTitleExtensions.Add(".psb");
_subTitleExtensions.Add(".rt");
_subTitleExtensions.Add(".rtf");
_subTitleExtensions.Add(".s2k");
_subTitleExtensions.Add(".sbt");
_subTitleExtensions.Add(".scr");
_subTitleExtensions.Add(".smi");
_subTitleExtensions.Add(".son");
_subTitleExtensions.Add(".srt");
_subTitleExtensions.Add(".ssa");
_subTitleExtensions.Add(".sst");
_subTitleExtensions.Add(".ssts");
_subTitleExtensions.Add(".stl");
_subTitleExtensions.Add(".sub");
_subTitleExtensions.Add(".txt");
_subTitleExtensions.Add(".vkt");
_subTitleExtensions.Add(".vsf");
_subTitleExtensions.Add(".zeg");
}
string filenameNoExt = Path.GetFileNameWithoutExtension(strFile);
try
{
foreach (string file in Directory.GetFiles(Path.GetDirectoryName(strFile), filenameNoExt + "*"))
{
System.IO.FileInfo fi = new FileInfo(file);
if (_subTitleExtensions.Contains(fi.Extension.ToLowerInvariant()))
{
return true;
}
}
}
catch (Exception)
{
// most likley path not available
}
return false;
}
private string GetFullCodecName(StreamKind type, int audiotrack)
{
string strCodec = _mI.Get(type, 0, "Format").ToUpperInvariant();
string strCodecVer = _mI.Get(type, 0, "Format_Version").ToUpperInvariant();
if (strCodec == "MPEG-4 VISUAL")
{
strCodec = _mI.Get(type, 0, "CodecID").ToUpperInvariant();
}
else
{
if (!String.IsNullOrEmpty(strCodecVer))
{
strCodec = (strCodec + " " + strCodecVer).Trim();
string strCodecProf = _mI.Get(type, 0, "Format_Profile").ToUpperInvariant();
if (type == StreamKind.Video && strCodecProf != "MAIN@MAIN")
{
strCodec = (strCodec + " " + strCodecProf).Trim();
}
}
}
if (type == StreamKind.Audio)
{
strCodec =
(strCodec + " " + _mI.Get(type, audiotrack, "Format_Profile").Split(new char[] {'/'})[0].ToUpperInvariant()).Trim();
}
//
// Workarround because skin engine ( string.equals/string.contains ) doesn't handle the "+" as last digit
//
return strCodec.Replace("+", "PLUS");
}
private string GetFullCodecName(StreamKind type)
{
return GetFullCodecName(type, 0);
}
#endregion
#region public video related properties
public double Framerate
{
get { return _framerate; }
}
public int Width
{
get { return _width; }
}
public int Height
{
get { return _height; }
}
public string AspectRatio
{
get { return _aspectRatio; }
}
public string VideoCodec
{
get { return _videoCodec; }
}
public string ScanType
{
get { return _scanType; }
}
public bool IsInterlaced
{
get { return _isInterlaced; }
}
public string VideoResolution
{
get { return _videoResolution; }
}
public int VideoDuration
{
get { return _videoDuration; }
}
#endregion
#region public audio related properties
public int AudioRate
{
get { return _audioRate; }
}
public int AudioChannels
{
get { return _audioChannels; }
}
public string AudioCodec
{
get { return _audioCodec; }
}
public string AudioChannelsFriendly
{
get { return _audioChannelsFriendly; }
}
#endregion
#region public detection properties
public bool hasVideo
{
get { return _hasVideo; }
}
public bool hasAudio
{
get { return _hasAudio; }
}
#endregion
#region public subtitles related properties
public int NumSubtitles
{
get { return _numsubtitles; }
}
public bool HasSubtitles
{
get { return _hasSubtitles; }
}
public bool MediaInfoNotloaded
{
get { return _mediaInfoNotloaded; }
}
#endregion
}
}