2019-01-13 20:02:23 +00:00
|
|
|
using System;
|
2018-12-14 09:40:55 +00:00
|
|
|
using System.Collections.Generic;
|
2021-04-04 13:04:01 +00:00
|
|
|
using System.Globalization;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Probing
|
|
|
|
{
|
2020-08-04 14:20:52 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Class containing helper methods for working with FFprobe output.
|
|
|
|
/// </summary>
|
2018-12-14 09:40:55 +00:00
|
|
|
public static class FFProbeHelpers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Normalizes the FF probe result.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="result">The result.</param>
|
|
|
|
public static void NormalizeFFProbeResult(InternalMediaInfoResult result)
|
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(result);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (result.Format?.Tags is not null)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2020-01-08 17:14:01 +00:00
|
|
|
result.Format.Tags = ConvertDictionaryToCaseInsensitive(result.Format.Tags);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (result.Streams is not null)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
// Convert all dictionaries to case insensitive
|
Fix exceptions while scanning
Fixes these exceptions:
```
[2019-12-22 20:48:14.779 +01:00] [ERR] Error in WaitForExit
System.InvalidOperationException: No process is associated with this object.
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.GetWaitState()
at System.Diagnostics.Process.WaitForExitCore(Int32 milliseconds)
at System.Diagnostics.Process.WaitForExit(Int32 milliseconds)
at Emby.Server.Implementations.Diagnostics.CommonProcess.WaitForExit(Int32 timeMs) in /home/pi/dev/jellyfin/Emby.Server.Implementations/Diagnostics/CommonProcess.cs:line 100
at MediaBrowser.MediaEncoding.Encoder.MediaEncoder.StopProcess(ProcessWrapper process, Int32 waitTimeMs) in /home/pi/dev/jellyfin/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs:line 785
[2019-12-22 20:48:14.790 +01:00] [INF] Killing ffmpeg process
[2019-12-22 20:48:14.795 +01:00] [ERR] Error killing process
System.InvalidOperationException: No process is associated with this object.
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.Kill()
at Emby.Server.Implementations.Diagnostics.CommonProcess.Kill() in /home/pi/dev/jellyfin/Emby.Server.Implementations/Diagnostics/CommonProcess.cs:line 95
at MediaBrowser.MediaEncoding.Encoder.MediaEncoder.StopProcess(ProcessWrapper process, Int32 waitTimeMs) in /home/pi/dev/jellyfin/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs:line 799
[2019-12-22 20:48:14.808 +01:00] [ERR] Error in "ffprobe"
System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $.streams[0].start_pts | LineNumber: 32 | BytePositionInLine: 26.
---> System.InvalidOperationException: Cannot get the value of a token type 'Number' as a string.
at System.Text.Json.Utf8JsonReader.GetString()
at System.Text.Json.Serialization.Converters.JsonConverterString.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
--- End of inner exception stack trace ---
at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadAsync[TValue](Stream utf8Json, Type returnType, JsonSerializerOptions options, CancellationToken cancellationToken)
at MediaBrowser.MediaEncoding.Encoder.MediaEncoder.GetMediaInfoInternal(String inputPath, String primaryPath, MediaProtocol protocol, Boolean extractChapters, String probeSizeArgument, Boolean isAudio, Nullable`1 videoType, Boolean forceEnableLogging, CancellationToken cancellationToken) in /home/pi/dev/jellyfin/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs:line 399
at MediaBrowser.Providers.MediaInfo.FFProbeVideoInfo.ProbeVideo[T](T item, MetadataRefreshOptions options, CancellationToken cancellationToken) in /home/pi/dev/jellyfin/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs:line 122
at MediaBrowser.Providers.Manager.MetadataService`2.RunCustomProvider(ICustomMetadataProvider`1 provider, TItemType item, String logName, MetadataRefreshOptions options, RefreshResult refreshResult, CancellationToken cancellationToken) in /home/pi/dev/jellyfin/MediaBrowser.Providers/Manager/MetadataService.cs:line 815
```
2019-12-22 20:39:26 +00:00
|
|
|
foreach (var stream in result.Streams)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2022-12-05 14:01:13 +00:00
|
|
|
if (stream.Tags is not null)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2020-01-08 17:14:01 +00:00
|
|
|
stream.Tags = ConvertDictionaryToCaseInsensitive(stream.Tags);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Gets an int from an FFProbeResult tags dictionary.
|
2018-12-14 09:40:55 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="tags">The tags.</param>
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
/// <returns>System.Nullable{System.Int32}.</returns>
|
2021-07-09 00:06:38 +00:00
|
|
|
public static int? GetDictionaryNumericValue(IReadOnlyDictionary<string, string> tags, string key)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2021-07-09 00:06:38 +00:00
|
|
|
if (tags.TryGetValue(key, out var val) && int.TryParse(val, out var i))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2021-07-09 00:06:38 +00:00
|
|
|
return i;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Gets a DateTime from an FFProbeResult tags dictionary.
|
2018-12-14 09:40:55 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="tags">The tags.</param>
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
/// <returns>System.Nullable{DateTime}.</returns>
|
2021-07-09 00:06:38 +00:00
|
|
|
public static DateTime? GetDictionaryDateTime(IReadOnlyDictionary<string, string> tags, string key)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2021-07-09 00:06:38 +00:00
|
|
|
if (tags.TryGetValue(key, out var val)
|
2021-09-20 23:21:45 +00:00
|
|
|
&& (DateTime.TryParse(val, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var dateTime)
|
|
|
|
|| DateTime.TryParseExact(val, "yyyy", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out dateTime)))
|
2021-04-04 12:58:20 +00:00
|
|
|
{
|
2021-09-20 23:21:45 +00:00
|
|
|
return dateTime;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Converts a dictionary to case insensitive.
|
2018-12-14 09:40:55 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="dict">The dict.</param>
|
|
|
|
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
2020-01-08 17:14:01 +00:00
|
|
|
private static Dictionary<string, string> ConvertDictionaryToCaseInsensitive(IReadOnlyDictionary<string, string> dict)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|