2016-03-27 23:11:27 +02:00
using MediaBrowser.Model.Dto ;
2015-04-04 21:35:29 +02:00
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Extensions ;
using System ;
using System.Collections.Generic ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
2016-02-24 21:20:22 +01:00
using System.Text ;
using System.Xml ;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO ;
2016-10-06 20:55:01 +02:00
using MediaBrowser.Common.IO ;
using MediaBrowser.Controller.IO ;
2016-08-18 17:13:18 +02:00
using MediaBrowser.Controller.Library ;
2016-10-23 21:14:57 +02:00
using MediaBrowser.Model.IO ;
2015-04-04 21:35:29 +02:00
using MediaBrowser.Model.Logging ;
using MediaBrowser.Model.MediaInfo ;
namespace MediaBrowser.MediaEncoding.Probing
{
public class ProbeResultNormalizer
{
private readonly CultureInfo _usCulture = new CultureInfo ( "en-US" ) ;
private readonly ILogger _logger ;
private readonly IFileSystem _fileSystem ;
2016-11-08 19:44:23 +01:00
private readonly IMemoryStreamFactory _memoryStreamProvider ;
2015-04-04 21:35:29 +02:00
2016-11-08 19:44:23 +01:00
public ProbeResultNormalizer ( ILogger logger , IFileSystem fileSystem , IMemoryStreamFactory memoryStreamProvider )
2015-04-04 21:35:29 +02:00
{
_logger = logger ;
_fileSystem = fileSystem ;
2016-10-06 20:55:01 +02:00
_memoryStreamProvider = memoryStreamProvider ;
2015-04-04 21:35:29 +02:00
}
2015-12-19 16:51:38 +01:00
public MediaInfo GetMediaInfo ( InternalMediaInfoResult data , VideoType videoType , bool isAudio , string path , MediaProtocol protocol )
2015-04-04 21:35:29 +02:00
{
2016-02-18 03:55:15 +01:00
var info = new MediaInfo
2015-04-04 21:35:29 +02:00
{
Path = path ,
Protocol = protocol
} ;
FFProbeHelpers . NormalizeFFProbeResult ( data ) ;
SetSize ( data , info ) ;
var internalStreams = data . streams ? ? new MediaStreamInfo [ ] { } ;
2015-05-10 06:29:04 +02:00
info . MediaStreams = internalStreams . Select ( s = > GetMediaStream ( isAudio , s , data . format ) )
2015-04-04 21:35:29 +02:00
. Where ( i = > i ! = null )
2017-01-24 06:52:47 +01:00
// Drop subtitle streams if we don't know the codec because it will just cause failures if we don't know how to handle them
. Where ( i = > i . Type ! = MediaStreamType . Subtitle | | ! string . IsNullOrWhiteSpace ( i . Codec ) )
2015-04-04 21:35:29 +02:00
. ToList ( ) ;
if ( data . format ! = null )
{
info . Container = data . format . format_name ;
if ( ! string . IsNullOrEmpty ( data . format . bit_rate ) )
{
2015-12-03 05:33:25 +01:00
int value ;
if ( int . TryParse ( data . format . bit_rate , NumberStyles . Any , _usCulture , out value ) )
{
info . Bitrate = value ;
}
2015-04-04 21:35:29 +02:00
}
}
2016-02-18 03:55:15 +01:00
var tags = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
2016-02-25 07:09:10 +01:00
var tagStreamType = isAudio ? "audio" : "video" ;
2015-04-04 21:35:29 +02:00
2016-02-18 03:55:15 +01:00
if ( data . streams ! = null )
{
var tagStream = data . streams . FirstOrDefault ( i = > string . Equals ( i . codec_type , tagStreamType , StringComparison . OrdinalIgnoreCase ) ) ;
2015-06-03 07:29:00 +02:00
2016-02-18 03:55:15 +01:00
if ( tagStream ! = null & & tagStream . tags ! = null )
2015-06-03 07:29:00 +02:00
{
2016-02-18 03:55:15 +01:00
foreach ( var pair in tagStream . tags )
2015-06-03 07:29:00 +02:00
{
2016-02-18 03:55:15 +01:00
tags [ pair . Key ] = pair . Value ;
2015-06-03 07:29:00 +02:00
}
}
2016-02-18 03:55:15 +01:00
}
2015-06-03 07:29:00 +02:00
2016-02-18 03:55:15 +01:00
if ( data . format ! = null & & data . format . tags ! = null )
{
foreach ( var pair in data . format . tags )
2015-04-04 21:35:29 +02:00
{
2016-02-18 03:55:15 +01:00
tags [ pair . Key ] = pair . Value ;
2015-04-04 21:35:29 +02:00
}
2016-02-18 03:55:15 +01:00
}
FetchGenres ( info , tags ) ;
2016-02-24 20:29:49 +01:00
var overview = FFProbeHelpers . GetDictionaryValue ( tags , "synopsis" ) ;
if ( string . IsNullOrWhiteSpace ( overview ) )
{
2017-01-26 21:27:12 +01:00
overview = FFProbeHelpers . GetDictionaryValue ( tags , "description" ) ;
2016-02-24 20:29:49 +01:00
}
if ( string . IsNullOrWhiteSpace ( overview ) )
{
overview = FFProbeHelpers . GetDictionaryValue ( tags , "desc" ) ;
}
2016-02-18 03:55:15 +01:00
if ( ! string . IsNullOrWhiteSpace ( overview ) )
{
info . Overview = overview ;
}
var title = FFProbeHelpers . GetDictionaryValue ( tags , "title" ) ;
if ( ! string . IsNullOrWhiteSpace ( title ) )
{
info . Name = title ;
}
info . ProductionYear = FFProbeHelpers . GetDictionaryNumericValue ( tags , "date" ) ;
// Several different forms of retaildate
info . PremiereDate = FFProbeHelpers . GetDictionaryDateTime ( tags , "retaildate" ) ? ?
FFProbeHelpers . GetDictionaryDateTime ( tags , "retail date" ) ? ?
FFProbeHelpers . GetDictionaryDateTime ( tags , "retail_date" ) ? ?
FFProbeHelpers . GetDictionaryDateTime ( tags , "date" ) ;
if ( isAudio )
{
SetAudioRuntimeTicks ( data , info ) ;
2016-02-24 20:29:49 +01:00
// tags are normally located under data.format, but we've seen some cases with ogg where they're part of the info stream
2016-02-18 03:55:15 +01:00
// so let's create a combined list of both
2015-06-03 07:29:00 +02:00
SetAudioInfoFromTags ( info , tags ) ;
2015-04-04 21:35:29 +02:00
}
else
{
2016-02-24 20:29:49 +01:00
FetchStudios ( info , tags , "copyright" ) ;
2016-02-18 03:55:15 +01:00
var iTunEXTC = FFProbeHelpers . GetDictionaryValue ( tags , "iTunEXTC" ) ;
if ( ! string . IsNullOrWhiteSpace ( iTunEXTC ) )
{
var parts = iTunEXTC . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
// Example
// mpaa|G|100|For crude humor
2016-04-13 22:49:16 +02:00
if ( parts . Length > 1 )
2016-02-18 03:55:15 +01:00
{
info . OfficialRating = parts [ 1 ] ;
2016-04-13 22:49:16 +02:00
if ( parts . Length > 3 )
{
info . OfficialRatingDescription = parts [ 3 ] ;
}
2016-02-18 03:55:15 +01:00
}
}
2016-02-24 20:29:49 +01:00
2016-02-18 03:55:15 +01:00
var itunesXml = FFProbeHelpers . GetDictionaryValue ( tags , "iTunMOVI" ) ;
if ( ! string . IsNullOrWhiteSpace ( itunesXml ) )
{
FetchFromItunesInfo ( itunesXml , info ) ;
}
2016-02-24 20:29:49 +01:00
2015-04-07 19:51:14 +02:00
if ( data . format ! = null & & ! string . IsNullOrEmpty ( data . format . duration ) )
{
info . RunTimeTicks = TimeSpan . FromSeconds ( double . Parse ( data . format . duration , _usCulture ) ) . Ticks ;
}
2015-04-04 21:35:29 +02:00
FetchWtvInfo ( info , data ) ;
if ( data . Chapters ! = null )
{
info . Chapters = data . Chapters . Select ( GetChapterInfo ) . ToList ( ) ;
}
ExtractTimestamp ( info ) ;
2016-08-09 07:08:36 +02:00
var stereoMode = GetDictionaryValue ( tags , "stereo_mode" ) ;
if ( string . Equals ( stereoMode , "left_right" , StringComparison . OrdinalIgnoreCase ) )
{
info . Video3DFormat = Video3DFormat . FullSideBySide ;
}
2016-12-20 06:23:33 +01:00
var videoStreamsBitrate = info . MediaStreams . Where ( i = > i . Type = = MediaStreamType . Video ) . Select ( i = > i . BitRate ? ? 0 ) . Sum ( ) ;
// If ffprobe reported the container bitrate as being the same as the video stream bitrate, then it's wrong
if ( videoStreamsBitrate = = ( info . Bitrate ? ? 0 ) )
{
2017-01-26 07:26:58 +01:00
info . InferTotalBitrate ( true ) ;
2016-12-20 06:23:33 +01:00
}
2015-04-04 21:35:29 +02:00
}
return info ;
}
2016-02-18 03:55:15 +01:00
private void FetchFromItunesInfo ( string xml , MediaInfo info )
{
2016-02-24 21:20:22 +01:00
// Make things simpler and strip out the dtd
2016-12-03 20:42:34 +01:00
var plistIndex = xml . IndexOf ( "<plist" , StringComparison . OrdinalIgnoreCase ) ;
if ( plistIndex ! = - 1 )
{
xml = xml . Substring ( plistIndex ) ;
}
2016-02-24 21:20:22 +01:00
xml = "<?xml version=\"1.0\"?>" + xml ;
2016-02-18 04:18:35 +01:00
// <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>cast</key>\n\t<array>\n\t\t<dict>\n\t\t\t<key>name</key>\n\t\t\t<string>Blender Foundation</string>\n\t\t</dict>\n\t\t<dict>\n\t\t\t<key>name</key>\n\t\t\t<string>Janus Bager Kristensen</string>\n\t\t</dict>\n\t</array>\n\t<key>directors</key>\n\t<array>\n\t\t<dict>\n\t\t\t<key>name</key>\n\t\t\t<string>Sacha Goedegebure</string>\n\t\t</dict>\n\t</array>\n\t<key>studio</key>\n\t<string>Blender Foundation</string>\n</dict>\n</plist>\n
2016-10-06 20:55:01 +02:00
using ( var stream = _memoryStreamProvider . CreateNew ( Encoding . UTF8 . GetBytes ( xml ) ) )
2016-02-24 21:20:22 +01:00
{
using ( var streamReader = new StreamReader ( stream ) )
{
2016-12-03 21:00:41 +01:00
try
2016-02-24 21:20:22 +01:00
{
2016-12-03 21:00:41 +01:00
// Use XmlReader for best performance
using ( var reader = XmlReader . Create ( streamReader ) )
2016-02-24 21:20:22 +01:00
{
2016-12-03 21:00:41 +01:00
reader . MoveToContent ( ) ;
reader . Read ( ) ;
// Loop through each element
2016-12-03 22:46:06 +01:00
while ( ! reader . EOF & & reader . ReadState = = ReadState . Interactive )
2016-02-24 21:20:22 +01:00
{
2016-12-03 21:00:41 +01:00
if ( reader . NodeType = = XmlNodeType . Element )
2016-02-24 21:20:22 +01:00
{
2016-12-03 21:00:41 +01:00
switch ( reader . Name )
{
case "dict" :
2016-12-04 00:57:34 +01:00
if ( reader . IsEmptyElement )
{
reader . Read ( ) ;
continue ;
}
2016-12-03 21:00:41 +01:00
using ( var subtree = reader . ReadSubtree ( ) )
{
ReadFromDictNode ( subtree , info ) ;
}
break ;
default :
reader . Skip ( ) ;
break ;
}
}
else
{
reader . Read ( ) ;
2016-02-24 21:20:22 +01:00
}
2016-11-02 18:08:20 +01:00
}
2016-02-24 21:20:22 +01:00
}
}
2016-12-03 21:00:41 +01:00
catch ( XmlException )
{
// I've seen probe examples where the iTunMOVI value is just "<"
// So we should not allow this to fail the entire probing operation
}
2016-02-24 21:20:22 +01:00
}
}
}
private void ReadFromDictNode ( XmlReader reader , MediaInfo info )
{
string currentKey = null ;
List < NameValuePair > pairs = new List < NameValuePair > ( ) ;
2016-11-02 18:08:20 +01:00
reader . MoveToContent ( ) ;
reader . Read ( ) ;
2016-02-24 21:20:22 +01:00
// Loop through each element
2016-12-03 22:46:06 +01:00
while ( ! reader . EOF & & reader . ReadState = = ReadState . Interactive )
2016-02-24 21:20:22 +01:00
{
if ( reader . NodeType = = XmlNodeType . Element )
{
switch ( reader . Name )
{
case "key" :
if ( ! string . IsNullOrWhiteSpace ( currentKey ) )
{
ProcessPairs ( currentKey , pairs , info ) ;
}
currentKey = reader . ReadElementContentAsString ( ) ;
pairs = new List < NameValuePair > ( ) ;
break ;
case "string" :
var value = reader . ReadElementContentAsString ( ) ;
if ( ! string . IsNullOrWhiteSpace ( value ) )
{
pairs . Add ( new NameValuePair
{
Name = value ,
Value = value
} ) ;
}
break ;
case "array" :
2016-12-04 00:57:34 +01:00
if ( reader . IsEmptyElement )
2016-02-24 21:20:22 +01:00
{
2016-12-04 00:57:34 +01:00
reader . Read ( ) ;
continue ;
}
using ( var subtree = reader . ReadSubtree ( ) )
{
if ( ! string . IsNullOrWhiteSpace ( currentKey ) )
2016-02-24 21:20:22 +01:00
{
pairs . AddRange ( ReadValueArray ( subtree ) ) ;
}
}
break ;
default :
reader . Skip ( ) ;
break ;
}
}
2016-11-02 18:08:20 +01:00
else
{
reader . Read ( ) ;
}
2016-02-24 21:20:22 +01:00
}
}
private List < NameValuePair > ReadValueArray ( XmlReader reader )
{
List < NameValuePair > pairs = new List < NameValuePair > ( ) ;
2016-11-02 18:08:20 +01:00
reader . MoveToContent ( ) ;
reader . Read ( ) ;
2016-02-24 21:20:22 +01:00
// Loop through each element
2016-12-03 22:46:06 +01:00
while ( ! reader . EOF & & reader . ReadState = = ReadState . Interactive )
2016-02-24 21:20:22 +01:00
{
if ( reader . NodeType = = XmlNodeType . Element )
{
switch ( reader . Name )
{
case "dict" :
2016-12-04 00:57:34 +01:00
if ( reader . IsEmptyElement )
{
reader . Read ( ) ;
continue ;
}
2016-02-24 21:20:22 +01:00
using ( var subtree = reader . ReadSubtree ( ) )
{
var dict = GetNameValuePair ( subtree ) ;
if ( dict ! = null )
{
pairs . Add ( dict ) ;
}
}
break ;
default :
reader . Skip ( ) ;
break ;
}
}
2016-11-02 18:08:20 +01:00
else
{
reader . Read ( ) ;
}
2016-02-24 21:20:22 +01:00
}
return pairs ;
}
private void ProcessPairs ( string key , List < NameValuePair > pairs , MediaInfo info )
{
if ( string . Equals ( key , "studio" , StringComparison . OrdinalIgnoreCase ) )
{
foreach ( var pair in pairs )
{
info . Studios . Add ( pair . Value ) ;
}
info . Studios = info . Studios
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. ToList ( ) ;
}
else if ( string . Equals ( key , "screenwriters" , StringComparison . OrdinalIgnoreCase ) )
{
foreach ( var pair in pairs )
{
info . People . Add ( new BaseItemPerson
{
Name = pair . Value ,
Type = PersonType . Writer
} ) ;
}
}
else if ( string . Equals ( key , "producers" , StringComparison . OrdinalIgnoreCase ) )
{
foreach ( var pair in pairs )
{
info . People . Add ( new BaseItemPerson
{
Name = pair . Value ,
Type = PersonType . Producer
} ) ;
}
}
else if ( string . Equals ( key , "directors" , StringComparison . OrdinalIgnoreCase ) )
{
foreach ( var pair in pairs )
{
info . People . Add ( new BaseItemPerson
{
Name = pair . Value ,
Type = PersonType . Director
} ) ;
}
}
}
private NameValuePair GetNameValuePair ( XmlReader reader )
{
string name = null ;
string value = null ;
2016-11-02 18:08:20 +01:00
reader . MoveToContent ( ) ;
reader . Read ( ) ;
2016-02-24 21:20:22 +01:00
// Loop through each element
2016-12-03 22:46:06 +01:00
while ( ! reader . EOF & & reader . ReadState = = ReadState . Interactive )
2016-02-24 21:20:22 +01:00
{
if ( reader . NodeType = = XmlNodeType . Element )
{
switch ( reader . Name )
{
case "key" :
name = reader . ReadElementContentAsString ( ) ;
break ;
case "string" :
value = reader . ReadElementContentAsString ( ) ;
break ;
default :
reader . Skip ( ) ;
break ;
}
}
2016-11-02 18:08:20 +01:00
else
{
reader . Read ( ) ;
}
2016-02-24 21:20:22 +01:00
}
if ( string . IsNullOrWhiteSpace ( name ) | |
string . IsNullOrWhiteSpace ( value ) )
{
return null ;
}
return new NameValuePair
{
Name = name ,
Value = value
} ;
2016-02-18 03:55:15 +01:00
}
2016-09-20 17:21:09 +02:00
private string NormalizeSubtitleCodec ( string codec )
{
2016-10-29 22:02:31 +02:00
if ( string . Equals ( codec , "dvb_subtitle" , StringComparison . OrdinalIgnoreCase ) )
{
codec = "dvbsub" ;
}
else if ( ( codec ? ? string . Empty ) . IndexOf ( "PGS" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
2016-09-20 17:21:09 +02:00
{
codec = "PGSSUB" ;
}
else if ( ( codec ? ? string . Empty ) . IndexOf ( "DVD" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
codec = "DVDSUB" ;
}
return codec ;
}
2015-04-04 21:35:29 +02:00
/// <summary>
/// Converts ffprobe stream info to our MediaStream class
/// </summary>
2016-02-24 20:29:49 +01:00
/// <param name="isAudio">if set to <c>true</c> [is info].</param>
2015-04-04 21:35:29 +02:00
/// <param name="streamInfo">The stream info.</param>
/// <param name="formatInfo">The format info.</param>
/// <returns>MediaStream.</returns>
2015-05-10 06:29:04 +02:00
private MediaStream GetMediaStream ( bool isAudio , MediaStreamInfo streamInfo , MediaFormatInfo formatInfo )
2015-04-04 21:35:29 +02:00
{
2015-08-08 18:16:34 +02:00
// These are mp4 chapters
if ( string . Equals ( streamInfo . codec_name , "mov_text" , StringComparison . OrdinalIgnoreCase ) )
{
2016-08-05 22:35:11 +02:00
// Edit: but these are also sometimes subtitles?
//return null;
2015-08-08 18:16:34 +02:00
}
2015-04-04 21:35:29 +02:00
var stream = new MediaStream
{
Codec = streamInfo . codec_name ,
Profile = streamInfo . profile ,
Level = streamInfo . level ,
Index = streamInfo . index ,
2016-04-18 05:50:44 +02:00
PixelFormat = streamInfo . pix_fmt ,
2016-06-06 23:13:00 +02:00
NalLengthSize = streamInfo . nal_length_size ,
TimeBase = streamInfo . time_base ,
CodecTimeBase = streamInfo . codec_time_base
2015-04-04 21:35:29 +02:00
} ;
2016-04-27 21:23:05 +02:00
if ( string . Equals ( streamInfo . is_avc , "true" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( streamInfo . is_avc , "1" , StringComparison . OrdinalIgnoreCase ) )
{
stream . IsAVC = true ;
}
else if ( string . Equals ( streamInfo . is_avc , "false" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( streamInfo . is_avc , "0" , StringComparison . OrdinalIgnoreCase ) )
{
stream . IsAVC = false ;
}
2015-10-30 16:26:36 +01:00
// Filter out junk
if ( ! string . IsNullOrWhiteSpace ( streamInfo . codec_tag_string ) & & streamInfo . codec_tag_string . IndexOf ( "[0]" , StringComparison . OrdinalIgnoreCase ) = = - 1 )
{
stream . CodecTag = streamInfo . codec_tag_string ;
}
2015-04-04 21:35:29 +02:00
if ( streamInfo . tags ! = null )
{
stream . Language = GetDictionaryValue ( streamInfo . tags , "language" ) ;
2016-01-11 17:52:22 +01:00
stream . Comment = GetDictionaryValue ( streamInfo . tags , "comment" ) ;
2016-05-14 20:02:06 +02:00
stream . Title = GetDictionaryValue ( streamInfo . tags , "title" ) ;
2015-04-04 21:35:29 +02:00
}
2016-02-25 07:09:10 +01:00
if ( string . Equals ( streamInfo . codec_type , "audio" , StringComparison . OrdinalIgnoreCase ) )
2015-04-04 21:35:29 +02:00
{
stream . Type = MediaStreamType . Audio ;
stream . Channels = streamInfo . channels ;
if ( ! string . IsNullOrEmpty ( streamInfo . sample_rate ) )
{
2015-10-05 00:04:56 +02:00
int value ;
if ( int . TryParse ( streamInfo . sample_rate , NumberStyles . Any , _usCulture , out value ) )
{
stream . SampleRate = value ;
}
2015-04-04 21:35:29 +02:00
}
stream . ChannelLayout = ParseChannelLayout ( streamInfo . channel_layout ) ;
2015-12-27 03:39:11 +01:00
if ( streamInfo . bits_per_sample > 0 )
{
stream . BitDepth = streamInfo . bits_per_sample ;
}
2015-12-27 07:05:37 +01:00
else if ( streamInfo . bits_per_raw_sample > 0 )
{
stream . BitDepth = streamInfo . bits_per_raw_sample ;
}
2015-04-04 21:35:29 +02:00
}
else if ( string . Equals ( streamInfo . codec_type , "subtitle" , StringComparison . OrdinalIgnoreCase ) )
{
stream . Type = MediaStreamType . Subtitle ;
2016-09-20 17:21:09 +02:00
stream . Codec = NormalizeSubtitleCodec ( stream . Codec ) ;
2015-04-04 21:35:29 +02:00
}
else if ( string . Equals ( streamInfo . codec_type , "video" , StringComparison . OrdinalIgnoreCase ) )
{
2016-03-28 05:37:33 +02:00
stream . Type = isAudio | | string . Equals ( stream . Codec , "mjpeg" , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( stream . Codec , "gif" , StringComparison . OrdinalIgnoreCase ) | | string . Equals ( stream . Codec , "png" , StringComparison . OrdinalIgnoreCase )
2015-04-04 21:35:29 +02:00
? MediaStreamType . EmbeddedImage
: MediaStreamType . Video ;
2017-03-07 20:05:44 +01:00
stream . AverageFrameRate = GetFrameRate ( streamInfo . avg_frame_rate ) ;
stream . RealFrameRate = GetFrameRate ( streamInfo . r_frame_rate ) ;
if ( isAudio | | string . Equals ( stream . Codec , "gif" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( stream . Codec , "png" , StringComparison . OrdinalIgnoreCase ) )
{
stream . Type = MediaStreamType . EmbeddedImage ;
}
else if ( string . Equals ( stream . Codec , "mjpeg" , StringComparison . OrdinalIgnoreCase ) )
{
// How to differentiate between video and embedded image?
// The only difference I've seen thus far is presence of codec tag, also embedded images have high (unusual) framerates
if ( ! string . IsNullOrWhiteSpace ( stream . CodecTag ) )
{
stream . Type = MediaStreamType . Video ;
}
else
{
stream . Type = MediaStreamType . EmbeddedImage ;
}
}
else
{
stream . Type = MediaStreamType . Video ;
}
2015-04-04 21:35:29 +02:00
stream . Width = streamInfo . width ;
stream . Height = streamInfo . height ;
stream . AspectRatio = GetAspectRatio ( streamInfo ) ;
2015-12-27 03:39:11 +01:00
if ( streamInfo . bits_per_sample > 0 )
{
stream . BitDepth = streamInfo . bits_per_sample ;
}
2015-12-27 07:05:37 +01:00
else if ( streamInfo . bits_per_raw_sample > 0 )
{
stream . BitDepth = streamInfo . bits_per_raw_sample ;
}
2015-04-04 21:35:29 +02:00
//stream.IsAnamorphic = string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase) ||
// string.Equals(stream.AspectRatio, "2.35:1", StringComparison.OrdinalIgnoreCase) ||
// string.Equals(stream.AspectRatio, "2.40:1", StringComparison.OrdinalIgnoreCase);
2015-05-17 05:17:23 +02:00
// http://stackoverflow.com/questions/17353387/how-to-detect-anamorphic-video-with-ffprobe
stream . IsAnamorphic = string . Equals ( streamInfo . sample_aspect_ratio , "0:1" , StringComparison . OrdinalIgnoreCase ) ;
2015-10-30 16:26:36 +01:00
if ( streamInfo . refs > 0 )
{
stream . RefFrames = streamInfo . refs ;
}
2015-04-04 21:35:29 +02:00
}
else
{
return null ;
}
// Get stream bitrate
var bitrate = 0 ;
if ( ! string . IsNullOrEmpty ( streamInfo . bit_rate ) )
{
2015-10-05 00:04:56 +02:00
int value ;
if ( int . TryParse ( streamInfo . bit_rate , NumberStyles . Any , _usCulture , out value ) )
{
bitrate = value ;
}
2015-04-04 21:35:29 +02:00
}
2015-10-05 00:04:56 +02:00
if ( bitrate = = 0 & & formatInfo ! = null & & ! string . IsNullOrEmpty ( formatInfo . bit_rate ) & & stream . Type = = MediaStreamType . Video )
2015-04-04 21:35:29 +02:00
{
// If the stream info doesn't have a bitrate get the value from the media format info
2015-10-05 00:04:56 +02:00
int value ;
if ( int . TryParse ( formatInfo . bit_rate , NumberStyles . Any , _usCulture , out value ) )
{
bitrate = value ;
}
2015-04-04 21:35:29 +02:00
}
if ( bitrate > 0 )
{
stream . BitRate = bitrate ;
}
if ( streamInfo . disposition ! = null )
{
var isDefault = GetDictionaryValue ( streamInfo . disposition , "default" ) ;
var isForced = GetDictionaryValue ( streamInfo . disposition , "forced" ) ;
stream . IsDefault = string . Equals ( isDefault , "1" , StringComparison . OrdinalIgnoreCase ) ;
stream . IsForced = string . Equals ( isForced , "1" , StringComparison . OrdinalIgnoreCase ) ;
}
2016-05-14 20:02:06 +02:00
NormalizeStreamTitle ( stream ) ;
2015-04-04 21:35:29 +02:00
return stream ;
}
2016-05-14 20:02:06 +02:00
private void NormalizeStreamTitle ( MediaStream stream )
{
2016-10-06 20:55:01 +02:00
if ( string . Equals ( stream . Title , "cc" , StringComparison . OrdinalIgnoreCase ) )
2016-05-14 20:02:06 +02:00
{
stream . Title = null ;
}
if ( stream . Type = = MediaStreamType . EmbeddedImage )
{
stream . Title = null ;
}
}
2015-04-04 21:35:29 +02:00
/// <summary>
/// Gets a string from an FFProbeResult tags dictionary
/// </summary>
/// <param name="tags">The tags.</param>
/// <param name="key">The key.</param>
/// <returns>System.String.</returns>
private string GetDictionaryValue ( Dictionary < string , string > tags , string key )
{
if ( tags = = null )
{
return null ;
}
string val ;
tags . TryGetValue ( key , out val ) ;
return val ;
}
private string ParseChannelLayout ( string input )
{
if ( string . IsNullOrEmpty ( input ) )
{
return input ;
}
return input . Split ( '(' ) . FirstOrDefault ( ) ;
}
private string GetAspectRatio ( MediaStreamInfo info )
{
var original = info . display_aspect_ratio ;
int height ;
int width ;
var parts = ( original ? ? string . Empty ) . Split ( ':' ) ;
if ( ! ( parts . Length = = 2 & &
int . TryParse ( parts [ 0 ] , NumberStyles . Any , _usCulture , out width ) & &
int . TryParse ( parts [ 1 ] , NumberStyles . Any , _usCulture , out height ) & &
width > 0 & &
height > 0 ) )
{
width = info . width ;
height = info . height ;
}
if ( width > 0 & & height > 0 )
{
double ratio = width ;
ratio / = height ;
if ( IsClose ( ratio , 1.777777778 , . 03 ) )
{
return "16:9" ;
}
if ( IsClose ( ratio , 1.3333333333 , . 05 ) )
{
return "4:3" ;
}
if ( IsClose ( ratio , 1.41 ) )
{
return "1.41:1" ;
}
if ( IsClose ( ratio , 1.5 ) )
{
return "1.5:1" ;
}
if ( IsClose ( ratio , 1.6 ) )
{
return "1.6:1" ;
}
if ( IsClose ( ratio , 1.66666666667 ) )
{
return "5:3" ;
}
if ( IsClose ( ratio , 1.85 , . 02 ) )
{
return "1.85:1" ;
}
if ( IsClose ( ratio , 2.35 , . 025 ) )
{
return "2.35:1" ;
}
if ( IsClose ( ratio , 2.4 , . 025 ) )
{
return "2.40:1" ;
}
}
return original ;
}
private bool IsClose ( double d1 , double d2 , double variance = . 005 )
{
return Math . Abs ( d1 - d2 ) < = variance ;
}
/// <summary>
/// Gets a frame rate from a string value in ffprobe output
/// This could be a number or in the format of 2997/125.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.Nullable{System.Single}.</returns>
private float? GetFrameRate ( string value )
{
if ( ! string . IsNullOrEmpty ( value ) )
{
var parts = value . Split ( '/' ) ;
float result ;
if ( parts . Length = = 2 )
{
result = float . Parse ( parts [ 0 ] , _usCulture ) / float . Parse ( parts [ 1 ] , _usCulture ) ;
}
else
{
result = float . Parse ( parts [ 0 ] , _usCulture ) ;
}
return float . IsNaN ( result ) ? ( float? ) null : result ;
}
return null ;
}
2016-02-25 07:09:10 +01:00
private void SetAudioRuntimeTicks ( InternalMediaInfoResult result , MediaInfo data )
2015-04-04 21:35:29 +02:00
{
if ( result . streams ! = null )
{
2016-02-24 20:29:49 +01:00
// Get the first info stream
2016-02-25 07:09:10 +01:00
var stream = result . streams . FirstOrDefault ( s = > string . Equals ( s . codec_type , "audio" , StringComparison . OrdinalIgnoreCase ) ) ;
2015-04-04 21:35:29 +02:00
if ( stream ! = null )
{
// Get duration from stream properties
var duration = stream . duration ;
// If it's not there go into format properties
if ( string . IsNullOrEmpty ( duration ) )
{
duration = result . format . duration ;
}
// If we got something, parse it
if ( ! string . IsNullOrEmpty ( duration ) )
{
data . RunTimeTicks = TimeSpan . FromSeconds ( double . Parse ( duration , _usCulture ) ) . Ticks ;
}
}
}
}
2015-04-05 17:01:57 +02:00
private void SetSize ( InternalMediaInfoResult data , Model . MediaInfo . MediaInfo info )
2015-04-04 21:35:29 +02:00
{
if ( data . format ! = null )
{
if ( ! string . IsNullOrEmpty ( data . format . size ) )
{
info . Size = long . Parse ( data . format . size , _usCulture ) ;
}
else
{
info . Size = null ;
}
}
}
2016-02-18 03:55:15 +01:00
private void SetAudioInfoFromTags ( MediaInfo audio , Dictionary < string , string > tags )
2015-04-04 21:35:29 +02:00
{
var composer = FFProbeHelpers . GetDictionaryValue ( tags , "composer" ) ;
if ( ! string . IsNullOrWhiteSpace ( composer ) )
{
foreach ( var person in Split ( composer , false ) )
{
audio . People . Add ( new BaseItemPerson { Name = person , Type = PersonType . Composer } ) ;
}
}
2016-11-01 19:28:36 +01:00
//var conductor = FFProbeHelpers.GetDictionaryValue(tags, "conductor");
//if (!string.IsNullOrWhiteSpace(conductor))
//{
// foreach (var person in Split(conductor, false))
// {
// audio.People.Add(new BaseItemPerson { Name = person, Type = PersonType.Conductor });
// }
//}
//var lyricist = FFProbeHelpers.GetDictionaryValue(tags, "lyricist");
//if (!string.IsNullOrWhiteSpace(lyricist))
//{
// foreach (var person in Split(lyricist, false))
// {
// audio.People.Add(new BaseItemPerson { Name = person, Type = PersonType.Lyricist });
// }
//}
2016-02-21 17:42:56 +01:00
// Check for writer some music is tagged that way as alternative to composer/lyricist
var writer = FFProbeHelpers . GetDictionaryValue ( tags , "writer" ) ;
if ( ! string . IsNullOrWhiteSpace ( writer ) )
{
foreach ( var person in Split ( writer , false ) )
{
audio . People . Add ( new BaseItemPerson { Name = person , Type = PersonType . Writer } ) ;
}
}
2015-04-04 21:35:29 +02:00
audio . Album = FFProbeHelpers . GetDictionaryValue ( tags , "album" ) ;
var artists = FFProbeHelpers . GetDictionaryValue ( tags , "artists" ) ;
if ( ! string . IsNullOrWhiteSpace ( artists ) )
{
2016-01-05 18:05:30 +01:00
audio . Artists = SplitArtists ( artists , new [ ] { '/' , ';' } , false )
2016-08-18 17:13:18 +02:00
. DistinctNames ( )
2015-04-04 21:35:29 +02:00
. ToList ( ) ;
}
else
{
var artist = FFProbeHelpers . GetDictionaryValue ( tags , "artist" ) ;
if ( string . IsNullOrWhiteSpace ( artist ) )
{
audio . Artists . Clear ( ) ;
}
else
{
2015-07-14 21:04:16 +02:00
audio . Artists = SplitArtists ( artist , _nameDelimiters , true )
2016-08-18 17:13:18 +02:00
. DistinctNames ( )
2015-04-04 21:35:29 +02:00
. ToList ( ) ;
}
}
var albumArtist = FFProbeHelpers . GetDictionaryValue ( tags , "albumartist" ) ;
if ( string . IsNullOrWhiteSpace ( albumArtist ) )
{
albumArtist = FFProbeHelpers . GetDictionaryValue ( tags , "album artist" ) ;
}
if ( string . IsNullOrWhiteSpace ( albumArtist ) )
{
albumArtist = FFProbeHelpers . GetDictionaryValue ( tags , "album_artist" ) ;
}
if ( string . IsNullOrWhiteSpace ( albumArtist ) )
{
audio . AlbumArtists = new List < string > ( ) ;
}
else
{
2015-07-14 21:04:16 +02:00
audio . AlbumArtists = SplitArtists ( albumArtist , _nameDelimiters , true )
2016-08-18 17:13:18 +02:00
. DistinctNames ( )
2015-04-04 21:35:29 +02:00
. ToList ( ) ;
}
2016-03-30 20:30:04 +02:00
if ( audio . AlbumArtists . Count = = 0 )
{
audio . AlbumArtists = audio . Artists . Take ( 1 ) . ToList ( ) ;
}
2015-04-04 21:35:29 +02:00
// Track number
audio . IndexNumber = GetDictionaryDiscValue ( tags , "track" ) ;
// Disc number
audio . ParentIndexNumber = GetDictionaryDiscValue ( tags , "disc" ) ;
// If we don't have a ProductionYear try and get it from PremiereDate
if ( audio . PremiereDate . HasValue & & ! audio . ProductionYear . HasValue )
{
audio . ProductionYear = audio . PremiereDate . Value . ToLocalTime ( ) . Year ;
}
// There's several values in tags may or may not be present
FetchStudios ( audio , tags , "organization" ) ;
FetchStudios ( audio , tags , "ensemble" ) ;
FetchStudios ( audio , tags , "publisher" ) ;
2015-05-22 21:16:14 +02:00
FetchStudios ( audio , tags , "label" ) ;
2015-04-04 21:35:29 +02:00
// These support mulitple values, but for now we only store the first.
2015-09-02 16:46:00 +02:00
var mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MusicBrainz Album Artist Id" ) ) ;
2015-10-05 00:04:56 +02:00
if ( mb = = null ) mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MUSICBRAINZ_ALBUMARTISTID" ) ) ;
2015-09-02 16:46:00 +02:00
audio . SetProviderId ( MetadataProviders . MusicBrainzAlbumArtist , mb ) ;
2015-10-05 00:04:56 +02:00
2015-09-02 16:46:00 +02:00
mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MusicBrainz Artist Id" ) ) ;
2015-10-05 00:04:56 +02:00
if ( mb = = null ) mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MUSICBRAINZ_ARTISTID" ) ) ;
2015-09-02 16:46:00 +02:00
audio . SetProviderId ( MetadataProviders . MusicBrainzArtist , mb ) ;
mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MusicBrainz Album Id" ) ) ;
2015-10-05 00:04:56 +02:00
if ( mb = = null ) mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MUSICBRAINZ_ALBUMID" ) ) ;
2015-09-02 16:46:00 +02:00
audio . SetProviderId ( MetadataProviders . MusicBrainzAlbum , mb ) ;
2015-10-05 00:04:56 +02:00
2015-09-02 16:46:00 +02:00
mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MusicBrainz Release Group Id" ) ) ;
2015-10-05 00:04:56 +02:00
if ( mb = = null ) mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MUSICBRAINZ_RELEASEGROUPID" ) ) ;
2015-09-02 16:46:00 +02:00
audio . SetProviderId ( MetadataProviders . MusicBrainzReleaseGroup , mb ) ;
mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MusicBrainz Release Track Id" ) ) ;
2015-10-05 00:04:56 +02:00
if ( mb = = null ) mb = GetMultipleMusicBrainzId ( FFProbeHelpers . GetDictionaryValue ( tags , "MUSICBRAINZ_RELEASETRACKID" ) ) ;
2015-09-02 16:46:00 +02:00
audio . SetProviderId ( MetadataProviders . MusicBrainzTrack , mb ) ;
2015-04-04 21:35:29 +02:00
}
private string GetMultipleMusicBrainzId ( string value )
{
if ( string . IsNullOrWhiteSpace ( value ) )
{
return null ;
}
return value . Split ( new [ ] { '/' } , StringSplitOptions . RemoveEmptyEntries )
. Select ( i = > i . Trim ( ) )
. FirstOrDefault ( i = > ! string . IsNullOrWhiteSpace ( i ) ) ;
}
private readonly char [ ] _nameDelimiters = { '/' , '|' , ';' , '\\' } ;
/// <summary>
/// Splits the specified val.
/// </summary>
/// <param name="val">The val.</param>
/// <param name="allowCommaDelimiter">if set to <c>true</c> [allow comma delimiter].</param>
/// <returns>System.String[][].</returns>
private IEnumerable < string > Split ( string val , bool allowCommaDelimiter )
{
// Only use the comma as a delimeter if there are no slashes or pipes.
// We want to be careful not to split names that have commas in them
var delimeter = ! allowCommaDelimiter | | _nameDelimiters . Any ( i = > val . IndexOf ( i ) ! = - 1 ) ?
_nameDelimiters :
new [ ] { ',' } ;
return val . Split ( delimeter , StringSplitOptions . RemoveEmptyEntries )
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Select ( i = > i . Trim ( ) ) ;
}
private const string ArtistReplaceValue = " | " ;
2015-07-14 21:04:16 +02:00
private IEnumerable < string > SplitArtists ( string val , char [ ] delimiters , bool splitFeaturing )
2015-04-04 21:35:29 +02:00
{
2015-07-14 21:04:16 +02:00
if ( splitFeaturing )
{
val = val . Replace ( " featuring " , ArtistReplaceValue , StringComparison . OrdinalIgnoreCase )
. Replace ( " feat. " , ArtistReplaceValue , StringComparison . OrdinalIgnoreCase ) ;
}
2015-04-04 21:35:29 +02:00
var artistsFound = new List < string > ( ) ;
foreach ( var whitelistArtist in GetSplitWhitelist ( ) )
{
var originalVal = val ;
val = val . Replace ( whitelistArtist , "|" , StringComparison . OrdinalIgnoreCase ) ;
if ( ! string . Equals ( originalVal , val , StringComparison . OrdinalIgnoreCase ) )
{
artistsFound . Add ( whitelistArtist ) ;
}
}
2015-07-14 21:04:16 +02:00
var artists = val . Split ( delimiters , StringSplitOptions . RemoveEmptyEntries )
2015-04-04 21:35:29 +02:00
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Select ( i = > i . Trim ( ) ) ;
artistsFound . AddRange ( artists ) ;
return artistsFound ;
}
private List < string > _splitWhiteList = null ;
private IEnumerable < string > GetSplitWhitelist ( )
{
if ( _splitWhiteList = = null )
{
2016-10-30 08:11:37 +01:00
_splitWhiteList = new List < string >
2015-04-04 21:35:29 +02:00
{
2016-11-19 09:40:13 +01:00
"AC/DC"
2016-10-30 08:11:37 +01:00
} ;
2015-04-04 21:35:29 +02:00
}
return _splitWhiteList ;
}
/// <summary>
/// Gets the studios from the tags collection
/// </summary>
2016-02-24 20:29:49 +01:00
/// <param name="info">The info.</param>
2015-04-04 21:35:29 +02:00
/// <param name="tags">The tags.</param>
/// <param name="tagName">Name of the tag.</param>
2016-02-24 20:29:49 +01:00
private void FetchStudios ( MediaInfo info , Dictionary < string , string > tags , string tagName )
2015-04-04 21:35:29 +02:00
{
var val = FFProbeHelpers . GetDictionaryValue ( tags , tagName ) ;
if ( ! string . IsNullOrEmpty ( val ) )
{
var studios = Split ( val , true ) ;
foreach ( var studio in studios )
{
// Sometimes the artist name is listed here, account for that
2016-02-24 20:29:49 +01:00
if ( info . Artists . Contains ( studio , StringComparer . OrdinalIgnoreCase ) )
2015-04-04 21:35:29 +02:00
{
continue ;
}
2016-02-24 20:29:49 +01:00
if ( info . AlbumArtists . Contains ( studio , StringComparer . OrdinalIgnoreCase ) )
2015-04-04 21:35:29 +02:00
{
continue ;
}
2016-02-24 20:29:49 +01:00
info . Studios . Add ( studio ) ;
2015-04-04 21:35:29 +02:00
}
2016-02-24 20:29:49 +01:00
info . Studios = info . Studios
2015-04-04 21:35:29 +02:00
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. ToList ( ) ;
}
}
/// <summary>
/// Gets the genres from the tags collection
/// </summary>
/// <param name="info">The information.</param>
/// <param name="tags">The tags.</param>
2016-02-18 03:55:15 +01:00
private void FetchGenres ( MediaInfo info , Dictionary < string , string > tags )
2015-04-04 21:35:29 +02:00
{
var val = FFProbeHelpers . GetDictionaryValue ( tags , "genre" ) ;
if ( ! string . IsNullOrEmpty ( val ) )
{
foreach ( var genre in Split ( val , true ) )
{
info . Genres . Add ( genre ) ;
}
info . Genres = info . Genres
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. ToList ( ) ;
}
}
/// <summary>
/// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
/// </summary>
/// <param name="tags">The tags.</param>
/// <param name="tagName">Name of the tag.</param>
/// <returns>System.Nullable{System.Int32}.</returns>
private int? GetDictionaryDiscValue ( Dictionary < string , string > tags , string tagName )
{
var disc = FFProbeHelpers . GetDictionaryValue ( tags , tagName ) ;
if ( ! string . IsNullOrEmpty ( disc ) )
{
disc = disc . Split ( '/' ) [ 0 ] ;
int num ;
if ( int . TryParse ( disc , out num ) )
{
return num ;
}
}
return null ;
}
private ChapterInfo GetChapterInfo ( MediaChapter chapter )
{
var info = new ChapterInfo ( ) ;
if ( chapter . tags ! = null )
{
string name ;
if ( chapter . tags . TryGetValue ( "title" , out name ) )
{
info . Name = name ;
}
}
// Limit accuracy to milliseconds to match xml saving
var secondsString = chapter . start_time ;
double seconds ;
if ( double . TryParse ( secondsString , NumberStyles . Any , CultureInfo . InvariantCulture , out seconds ) )
{
var ms = Math . Round ( TimeSpan . FromSeconds ( seconds ) . TotalMilliseconds ) ;
info . StartPositionTicks = TimeSpan . FromMilliseconds ( ms ) . Ticks ;
}
return info ;
}
private const int MaxSubtitleDescriptionExtractionLength = 100 ; // When extracting subtitles, the maximum length to consider (to avoid invalid filenames)
2016-02-18 03:55:15 +01:00
private void FetchWtvInfo ( MediaInfo video , InternalMediaInfoResult data )
2015-04-04 21:35:29 +02:00
{
if ( data . format = = null | | data . format . tags = = null )
{
return ;
}
var genres = FFProbeHelpers . GetDictionaryValue ( data . format . tags , "WM/Genre" ) ;
if ( ! string . IsNullOrWhiteSpace ( genres ) )
{
2016-02-18 03:55:15 +01:00
var genreList = genres . Split ( new [ ] { ';' , '/' , ',' } , StringSplitOptions . RemoveEmptyEntries )
2015-04-04 21:35:29 +02:00
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Select ( i = > i . Trim ( ) )
. ToList ( ) ;
2016-02-18 03:55:15 +01:00
// If this is empty then don't overwrite genres that might have been fetched earlier
if ( genreList . Count > 0 )
{
video . Genres = genreList ;
}
2015-04-04 21:35:29 +02:00
}
var officialRating = FFProbeHelpers . GetDictionaryValue ( data . format . tags , "WM/ParentalRating" ) ;
if ( ! string . IsNullOrWhiteSpace ( officialRating ) )
{
video . OfficialRating = officialRating ;
}
var people = FFProbeHelpers . GetDictionaryValue ( data . format . tags , "WM/MediaCredits" ) ;
if ( ! string . IsNullOrEmpty ( people ) )
{
video . People = people . Split ( new [ ] { ';' , '/' } , StringSplitOptions . RemoveEmptyEntries )
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Select ( i = > new BaseItemPerson { Name = i . Trim ( ) , Type = PersonType . Actor } )
. ToList ( ) ;
}
var year = FFProbeHelpers . GetDictionaryValue ( data . format . tags , "WM/OriginalReleaseTime" ) ;
if ( ! string . IsNullOrWhiteSpace ( year ) )
{
int val ;
if ( int . TryParse ( year , NumberStyles . Integer , _usCulture , out val ) )
{
video . ProductionYear = val ;
}
}
var premiereDateString = FFProbeHelpers . GetDictionaryValue ( data . format . tags , "WM/MediaOriginalBroadcastDateTime" ) ;
if ( ! string . IsNullOrWhiteSpace ( premiereDateString ) )
{
DateTime val ;
// Credit to MCEBuddy: https://mcebuddy2x.codeplex.com/
// DateTime is reported along with timezone info (typically Z i.e. UTC hence assume None)
if ( DateTime . TryParse ( year , null , DateTimeStyles . None , out val ) )
{
video . PremiereDate = val . ToUniversalTime ( ) ;
}
}
var description = FFProbeHelpers . GetDictionaryValue ( data . format . tags , "WM/SubTitleDescription" ) ;
var subTitle = FFProbeHelpers . GetDictionaryValue ( data . format . tags , "WM/SubTitle" ) ;
// For below code, credit to MCEBuddy: https://mcebuddy2x.codeplex.com/
// Sometimes for TV Shows the Subtitle field is empty and the subtitle description contains the subtitle, extract if possible. See ticket https://mcebuddy2x.codeplex.com/workitem/1910
// The format is -> EPISODE/TOTAL_EPISODES_IN_SEASON. SUBTITLE: DESCRIPTION
// OR -> COMMENT. SUBTITLE: DESCRIPTION
// e.g. -> 4/13. The Doctor's Wife: Science fiction drama. When he follows a Time Lord distress signal, the Doctor puts Amy, Rory and his beloved TARDIS in grave danger. Also in HD. [AD,S]
// e.g. -> CBeebies Bedtime Hour. The Mystery: Animated adventures of two friends who live on an island in the middle of the big city. Some of Abney and Teal's favourite objects are missing. [S]
if ( String . IsNullOrWhiteSpace ( subTitle ) & & ! String . IsNullOrWhiteSpace ( description ) & & description . Substring ( 0 , Math . Min ( description . Length , MaxSubtitleDescriptionExtractionLength ) ) . Contains ( ":" ) ) // Check within the Subtitle size limit, otherwise from description it can get too long creating an invalid filename
{
string [ ] parts = description . Split ( ':' ) ;
if ( parts . Length > 0 )
{
string subtitle = parts [ 0 ] ;
try
{
if ( subtitle . Contains ( "/" ) ) // It contains a episode number and season number
{
string [ ] numbers = subtitle . Split ( ' ' ) ;
video . IndexNumber = int . Parse ( numbers [ 0 ] . Replace ( "." , "" ) . Split ( '/' ) [ 0 ] ) ;
int totalEpisodesInSeason = int . Parse ( numbers [ 0 ] . Replace ( "." , "" ) . Split ( '/' ) [ 1 ] ) ;
description = String . Join ( " " , numbers , 1 , numbers . Length - 1 ) . Trim ( ) ; // Skip the first, concatenate the rest, clean up spaces and save it
}
else
throw new Exception ( ) ; // Switch to default parsing
}
catch // Default parsing
{
if ( subtitle . Contains ( "." ) ) // skip the comment, keep the subtitle
description = String . Join ( "." , subtitle . Split ( '.' ) , 1 , subtitle . Split ( '.' ) . Length - 1 ) . Trim ( ) ; // skip the first
else
description = subtitle . Trim ( ) ; // Clean up whitespaces and save it
}
}
}
if ( ! string . IsNullOrWhiteSpace ( description ) )
{
video . Overview = description ;
}
}
2015-12-20 05:37:59 +01:00
private void ExtractTimestamp ( MediaInfo video )
2015-04-04 21:35:29 +02:00
{
if ( video . VideoType = = VideoType . VideoFile )
{
if ( string . Equals ( video . Container , "mpeg2ts" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( video . Container , "m2ts" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( video . Container , "ts" , StringComparison . OrdinalIgnoreCase ) )
{
try
{
video . Timestamp = GetMpegTimestamp ( video . Path ) ;
_logger . Debug ( "Video has {0} timestamp" , video . Timestamp ) ;
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error extracting timestamp info from {0}" , ex , video . Path ) ;
video . Timestamp = null ;
}
}
}
}
private TransportStreamTimestamp GetMpegTimestamp ( string path )
{
var packetBuffer = new byte [ 'Å' ] ;
2016-10-25 21:02:04 +02:00
using ( var fs = _fileSystem . GetFileStream ( path , FileOpenMode . Open , FileAccessMode . Read , FileShareMode . Read ) )
2015-04-04 21:35:29 +02:00
{
fs . Read ( packetBuffer , 0 , packetBuffer . Length ) ;
}
if ( packetBuffer [ 0 ] = = 71 )
{
return TransportStreamTimestamp . None ;
}
if ( ( packetBuffer [ 4 ] = = 71 ) & & ( packetBuffer [ 'Ä' ] = = 71 ) )
{
if ( ( packetBuffer [ 0 ] = = 0 ) & & ( packetBuffer [ 1 ] = = 0 ) & & ( packetBuffer [ 2 ] = = 0 ) & & ( packetBuffer [ 3 ] = = 0 ) )
{
return TransportStreamTimestamp . Zero ;
}
return TransportStreamTimestamp . Valid ;
}
return TransportStreamTimestamp . None ;
}
}
}