Skip to content

Commit

Permalink
#19 not work on iis
Browse files Browse the repository at this point in the history
Path to mediaInfo.dll is detected more accurately. Checks current process directory, current process directory + /x86 or /64, assembly location and assembly location + /x86 or /64
Add constructor with path to dll
  • Loading branch information
yartat committed Jun 21, 2020
1 parent 9845b6f commit f9282fc
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 329 deletions.
376 changes: 188 additions & 188 deletions MediaInfo.Wrapper.Tests/AudioTests.cs

Large diffs are not rendered by default.

122 changes: 61 additions & 61 deletions MediaInfo.Wrapper.Tests/VideoTests.cs

Large diffs are not rendered by default.

32 changes: 11 additions & 21 deletions MediaInfo.Wrapper/Builder/AudioStreamBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public override AudioStream Build()
result.Codec = formatProfile;
baseIndex = 1;
}
else
else
{
formatProfile = GetMlpCodecIdByAdditionalFeatures(Get((int)NativeMethods.Audio.Audio_Format_AdditionalFeatures, InfoKind.Text).Trim());
if (formatProfile != AudioCodec.Undefined)
Expand Down Expand Up @@ -338,28 +338,18 @@ public override AudioStream Build()
return result;
}

private static string ExtractInfo(string source, int index)
{
return source.IndexOf("/", StringComparison.Ordinal) >= 0 ?
source.Split('/').Skip(index).FirstOrDefault()?.Trim() :
source;
}
private static string ExtractInfo(string source, int index) =>
source.IndexOf("/", StringComparison.Ordinal) >= 0 ?
source.Split('/').Skip(index).FirstOrDefault()?.Trim() :
source;

private static bool TryGetCodecByCodecId(string source, out AudioCodec result)
{
return CodecIds.TryGetValue(source, out result);
}
private static bool TryGetCodecByCodecId(string source, out AudioCodec result) =>
CodecIds.TryGetValue(source, out result);

private static AudioCodec GetCodecIdByCodecName(string source)
{
AudioCodec result;
return Codecs.TryGetValue(source, out result) ? result : AudioCodec.Undefined;
}
private static AudioCodec GetCodecIdByCodecName(string source) =>
Codecs.TryGetValue(source, out var result) ? result : AudioCodec.Undefined;

private static AudioCodec GetMlpCodecIdByAdditionalFeatures(string source)
{
AudioCodec result;
return MlpCodecsAdditionalFeatures.TryGetValue(source, out result) ? result : AudioCodec.Undefined;
}
private static AudioCodec GetMlpCodecIdByAdditionalFeatures(string source) =>
MlpCodecsAdditionalFeatures.TryGetValue(source, out var result) ? result : AudioCodec.Undefined;
}
}
6 changes: 3 additions & 3 deletions MediaInfo.Wrapper/MediaInfo.Wrapper.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>MediaInfo</RootNamespace>
<Version>19.09.3</Version>
<AssemblyVersion>19.09.03.0</AssemblyVersion>
<FileVersion>19.09.03.0</FileVersion>
<Version>19.09.4</Version>
<AssemblyVersion>19.09.04.0</AssemblyVersion>
<FileVersion>19.09.04.0</FileVersion>
<Company>Team MediaPortal</Company>
<Authors>yartat</Authors>
<Product>MP-MediaInfo</Product>
Expand Down
6 changes: 3 additions & 3 deletions MediaInfo.Wrapper/MediaInfo.Wrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<TargetFrameworks>net4.0;net4.5</TargetFrameworks>
<RootNamespace>MediaInfo</RootNamespace>
<Version>19.09.3</Version>
<AssemblyVersion>19.09.03.0</AssemblyVersion>
<FileVersion>19.09.03.0</FileVersion>
<Version>19.09.4</Version>
<AssemblyVersion>19.09.04.0</AssemblyVersion>
<FileVersion>19.09.04.0</FileVersion>
<Company>Team MediaPortal</Company>
<Authors>yartat</Authors>
<Product>MP-MediaInfo</Product>
Expand Down
130 changes: 78 additions & 52 deletions MediaInfo.Wrapper/MediaInfoWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MediaInfoWrapper
{
#region private vars

private static readonly Dictionary<string, bool> SubTitleExtensions = new Dictionary<string, bool>
private static readonly Dictionary<string, bool> SubTitleExtensions = new Dictionary<string, bool>
{
{ ".AQT", true },
{ ".ASC", true },
Expand Down Expand Up @@ -95,7 +95,7 @@ public MediaInfoWrapper(string filePath, ILogger logger = null)
/// <param name="filePath">The file path.</param>
/// <param name="pathToDll">The path to DLL.</param>
/// <param name="logger">the logger instance.</param>
protected MediaInfoWrapper(string filePath, string pathToDll, ILogger logger)
public MediaInfoWrapper(string filePath, string pathToDll, ILogger logger)
#endif
{
_filePath = filePath;
Expand All @@ -115,7 +115,24 @@ protected MediaInfoWrapper(string filePath, string pathToDll, ILogger logger)
}

#if (NET40 || NET45)
var realPathToDll = IfExistsPath(logger, ".\\", () => IfExistsPath(logger, pathToDll, () => null));
var realPathToDll = ((string)null)
.IfExistsPath("./", logger)
.IfExistsPath(pathToDll, logger)
.IfExistsPath(
Path.GetDirectoryName(typeof(MediaInfoWrapper).Assembly.Location),
logger)
.IfExistsPath(
Path.IsPathRooted(pathToDll) ?
null :
Path.Combine(Path.GetDirectoryName(typeof(MediaInfoWrapper).Assembly.Location), pathToDll),
logger);

if (string.IsNullOrEmpty(realPathToDll))
{
logger.LogError("MediaInfo.dll was not found");
MediaInfoNotloaded = true;
return;
}
#endif

var isTv = filePath.IsLiveTv();
Expand Down Expand Up @@ -186,13 +203,13 @@ public void WriteInfo()
{
_logger.LogInformation($"Inspecting media : {_filePath}");
if (MediaInfoNotloaded)
{
_logger.LogWarning($"MediaInfo.dll was not loaded!");
{
_logger.LogWarning("MediaInfo.dll was not loaded!");
}
else
{
_logger.LogDebug($"DLL version : {Version}");

// General
_logger.LogDebug($"Media duration : {TimeSpan.FromMilliseconds(Duration)}");
_logger.LogDebug($"Has audio : {(AudioStreams?.Count ?? 0) > 0}");
Expand Down Expand Up @@ -230,7 +247,7 @@ public void WriteInfo()

// Subtitles
if (HasSubtitles)
{
{
_logger.LogDebug($"Subtitles count : {Subtitles?.Count ?? 0}");
}

Expand All @@ -242,27 +259,6 @@ public void WriteInfo()
}
}

#if (NET40 || NET45)
private string IfExistsPath(ILogger logger, string pathToDll, Func<string> anotherPath)
{
var result = anotherPath();
if (!string.IsNullOrEmpty(result))
{
return result;
}

logger.LogDebug("Check MediaInfo.dll from {0}.", pathToDll);
if (!MediaInfoExist(pathToDll))
{
MediaInfoNotloaded = true;
logger.LogWarning($"Library MediaInfo.dll was not found at {pathToDll}");
return null;
}

return pathToDll;
}
#endif

private static long GetDirectorySize(string folderName)
{
if (!Directory.Exists(folderName))
Expand Down Expand Up @@ -476,10 +472,10 @@ private void SetupProperties(MediaInfo mediaInfo)
? mediaInfo.Get(StreamKind.Video, BestVideoStream.StreamPosition, "DisplayAspectRatio")
: string.Empty;
AspectRatio = BestVideoStream != null ?
AspectRatio == "4:3" || AspectRatio == "1.333" ? "fullscreen" : "widescreen" :
AspectRatio == "4:3" || AspectRatio == "1.333" ? "fullscreen" : "widescreen" :
string.Empty;

BestAudioStream = AudioStreams.OrderByDescending(x => x.Channel * 10000000 + x.Bitrate).FirstOrDefault();
BestAudioStream = AudioStreams.OrderByDescending(x => (x.Channel * 10000000) + x.Bitrate).FirstOrDefault();
AudioCodec = BestAudioStream?.CodecName ?? string.Empty;
AudioRate = (int?)BestAudioStream?.Bitrate ?? 0;
AudioSampleRate = (int?)BestAudioStream?.SamplingRate ?? 0;
Expand All @@ -489,17 +485,7 @@ private void SetupProperties(MediaInfo mediaInfo)

#endregion

/// <summary>
/// Checks if mediaInfo.dll file exist.
/// </summary>
/// <param name="pathToDll">The path to mediaInfo.dll</param>
/// <returns>Returns <b>true</b> if mediaInfo.dll is exists; elsewhere <b>false</b>.</returns>
public static bool MediaInfoExist(string pathToDll)
{
return File.Exists(Path.Combine(pathToDll, "MediaInfo.dll"));
}

#region private methods
#region private methods

private static bool CheckHasExternalSubtitles(string strFile)
{
Expand All @@ -521,9 +507,9 @@ private static bool CheckHasExternalSubtitles(string strFile)
}
}

#endregion
#endregion

#region public video related properties
#region public video related properties

/// <summary>
/// Gets a value indicating whether this instance has video.
Expand Down Expand Up @@ -637,9 +623,9 @@ private static bool CheckHasExternalSubtitles(string strFile)
/// </value>
public int VideoRate { get; private set; }

#endregion
#endregion

#region public audio related properties
#region public audio related properties

/// <summary>
/// Gets the audio streams.
Expand Down Expand Up @@ -697,9 +683,9 @@ private static bool CheckHasExternalSubtitles(string strFile)
/// </value>
public string AudioChannelsFriendly { get; private set; }

#endregion
#endregion

#region public subtitles related properties
#region public subtitles related properties

/// <summary>
/// Gets the list of media subtitles.
Expand All @@ -725,9 +711,9 @@ private static bool CheckHasExternalSubtitles(string strFile)
/// </value>
public bool HasExternalSubtitles { get; }

#endregion
#endregion

#region public chapters related properties
#region public chapters related properties

/// <summary>
/// Gets the media chapters.
Expand All @@ -745,9 +731,9 @@ private static bool CheckHasExternalSubtitles(string strFile)
/// </value>
public bool HasChapters => Chapters.Count > 0;

#endregion
#endregion

#region public menu related properties
#region public menu related properties

/// <summary>
/// Gets the menu streams from media.
Expand All @@ -765,7 +751,9 @@ private static bool CheckHasExternalSubtitles(string strFile)
/// </value>
public bool HasMenu => MenuStreams.Count > 0;

#endregion
#endregion

#region public common properties

/// <summary>
/// Gets a value indicating whether media is DVD.
Expand Down Expand Up @@ -846,5 +834,43 @@ private static bool CheckHasExternalSubtitles(string strFile)
/// The tags.
/// </value>
public AudioTags Tags { get; private set; }

#endregion
}

#if (NET40 || NET45)
internal static class PathExtensions
{
public static string IfExistsPath(this string sourcePath, string anotherPath, ILogger logger)
{
if (!string.IsNullOrEmpty(sourcePath))
{
return sourcePath;
}

if (string.IsNullOrEmpty(anotherPath))
{
return null;
}

logger.LogDebug("Check MediaInfo.dll from {0}.", anotherPath);
if (!anotherPath.MediaInfoExist())
{
logger.LogWarning($"Library MediaInfo.dll was not found at {anotherPath}");
return null;
}

logger.LogInformation($"Library MediaInfo.dll was found at {anotherPath}");
return anotherPath;
}

/// <summary>
/// Checks if mediaInfo.dll file exist.
/// </summary>
/// <param name="pathToDll">The path to mediaInfo.dll</param>
/// <returns>Returns <b>true</b> if mediaInfo.dll is exists; elsewhere <b>false</b>.</returns>
public static bool MediaInfoExist(this string pathToDll) =>
File.Exists(Path.Combine(pathToDll, "MediaInfo.dll"));
}
#endif
}
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 19.09.03
version: 19.09.04

branches:
only:
Expand Down

0 comments on commit f9282fc

Please sign in to comment.