2021-11-22 21:47:52 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2021-11-28 14:20:12 +01:00
|
|
|
using System.Runtime.CompilerServices;
|
2021-11-22 21:47:52 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-11-27 12:10:57 +01:00
|
|
|
using Emby.Naming.Audio;
|
|
|
|
using Emby.Naming.Common;
|
|
|
|
using Jellyfin.Extensions;
|
2021-11-22 21:47:52 +01:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Globalization;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
2021-11-30 21:05:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Resolves external audios for videos.
|
|
|
|
/// </summary>
|
2021-11-22 21:47:52 +01:00
|
|
|
public class AudioResolver
|
|
|
|
{
|
2021-11-28 14:03:52 +01:00
|
|
|
private readonly ILocalizationManager _localizationManager;
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
private readonly NamingOptions _namingOptions;
|
|
|
|
|
2021-11-30 21:05:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="AudioResolver"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="localizationManager">The localization manager.</param>
|
|
|
|
/// <param name="mediaEncoder">The media encoder.</param>
|
|
|
|
/// <param name="namingOptions">The naming options.</param>
|
2021-11-28 14:03:52 +01:00
|
|
|
public AudioResolver(
|
|
|
|
ILocalizationManager localizationManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
NamingOptions namingOptions)
|
|
|
|
{
|
|
|
|
_localizationManager = localizationManager;
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
_namingOptions = namingOptions;
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:05:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the audio streams found in the external audio files for the given video.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="video">The video to get the external audio streams from.</param>
|
|
|
|
/// <param name="startIndex">The stream index to start adding audio streams at.</param>
|
|
|
|
/// <param name="directoryService">The directory service to search for files.</param>
|
|
|
|
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
|
|
|
/// <returns>A list of external audio streams.</returns>
|
2021-11-28 14:03:52 +01:00
|
|
|
public async IAsyncEnumerable<MediaStream> GetExternalAudioStreams(
|
2021-11-22 21:47:52 +01:00
|
|
|
Video video,
|
|
|
|
int startIndex,
|
|
|
|
IDirectoryService directoryService,
|
2021-11-27 12:10:57 +01:00
|
|
|
bool clearCache,
|
2021-11-28 14:20:12 +01:00
|
|
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-28 14:20:12 +01:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2021-11-22 21:47:52 +01:00
|
|
|
if (!video.IsFileProtocol)
|
|
|
|
{
|
2021-11-28 14:03:52 +01:00
|
|
|
yield break;
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:03:52 +01:00
|
|
|
IEnumerable<string> paths = GetExternalAudioFiles(video, directoryService, clearCache);
|
|
|
|
foreach (string path in paths)
|
|
|
|
{
|
|
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
|
2021-11-30 20:45:47 +01:00
|
|
|
Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(path, cancellationToken).ConfigureAwait(false);
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-28 14:03:52 +01:00
|
|
|
foreach (MediaStream mediaStream in mediaInfo.MediaStreams)
|
|
|
|
{
|
|
|
|
mediaStream.Index = startIndex++;
|
|
|
|
mediaStream.Type = MediaStreamType.Audio;
|
|
|
|
mediaStream.IsExternal = true;
|
|
|
|
mediaStream.Path = path;
|
|
|
|
mediaStream.IsDefault = false;
|
|
|
|
mediaStream.Title = null;
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-28 14:03:52 +01:00
|
|
|
if (string.IsNullOrEmpty(mediaStream.Language))
|
|
|
|
{
|
|
|
|
// Try to translate to three character code
|
|
|
|
// Be flexible and check against both the full and three character versions
|
|
|
|
var language = StringExtensions.RightPart(fileNameWithoutExtension, '.').ToString();
|
|
|
|
|
|
|
|
if (language != fileNameWithoutExtension)
|
|
|
|
{
|
|
|
|
var culture = _localizationManager.FindLanguageInfo(language);
|
|
|
|
|
|
|
|
language = culture == null ? language : culture.ThreeLetterISOLanguageName;
|
|
|
|
mediaStream.Language = language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
yield return mediaStream;
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 21:05:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the external audio file paths for the given video.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="video">The video to get the external audio file paths from.</param>
|
|
|
|
/// <param name="directoryService">The directory service to search for files.</param>
|
|
|
|
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
|
|
/// <returns>A list of external audio file paths.</returns>
|
2021-11-27 16:58:37 +01:00
|
|
|
public IEnumerable<string> GetExternalAudioFiles(
|
2021-11-22 21:47:52 +01:00
|
|
|
Video video,
|
|
|
|
IDirectoryService directoryService,
|
|
|
|
bool clearCache)
|
|
|
|
{
|
|
|
|
if (!video.IsFileProtocol)
|
|
|
|
{
|
2021-11-28 14:03:52 +01:00
|
|
|
yield break;
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:03:52 +01:00
|
|
|
// Check if video folder exists
|
|
|
|
string folder = video.ContainingFolderPath;
|
2021-11-27 12:10:57 +01:00
|
|
|
if (!Directory.Exists(folder))
|
|
|
|
{
|
2021-11-28 14:03:52 +01:00
|
|
|
yield break;
|
2021-11-27 12:10:57 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 14:03:52 +01:00
|
|
|
string videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path);
|
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
var files = directoryService.GetFilePaths(folder, clearCache, true);
|
|
|
|
for (int i = 0; i < files.Count; i++)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-28 14:03:52 +01:00
|
|
|
string file = files[i];
|
2021-12-08 18:55:28 +01:00
|
|
|
if (string.Equals(video.Path, file, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| !AudioFileParser.IsAudioFile(file, _namingOptions)
|
|
|
|
|| Path.GetExtension(file.AsSpan()).Equals(".strm", StringComparison.OrdinalIgnoreCase))
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-30 20:44:57 +01:00
|
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
2021-11-22 21:47:52 +01:00
|
|
|
// The audio filename must either be equal to the video filename or start with the video filename followed by a dot
|
2021-11-27 16:55:19 +01:00
|
|
|
if (videoFileNameWithoutExtension.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| (fileNameWithoutExtension.Length > videoFileNameWithoutExtension.Length
|
|
|
|
&& fileNameWithoutExtension[videoFileNameWithoutExtension.Length] == '.'
|
|
|
|
&& fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension, StringComparison.OrdinalIgnoreCase)))
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-28 14:03:52 +01:00
|
|
|
yield return file;
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:05:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the media info of the given audio file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path to the audio file.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
|
|
|
/// <returns>The media info for the given audio file.</returns>
|
2021-11-28 14:03:52 +01:00
|
|
|
private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(string path, CancellationToken cancellationToken)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-28 14:03:52 +01:00
|
|
|
return _mediaEncoder.GetMediaInfo(
|
2021-11-22 21:47:52 +01:00
|
|
|
new MediaInfoRequest
|
|
|
|
{
|
|
|
|
MediaType = DlnaProfileType.Audio,
|
|
|
|
MediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
Path = path,
|
|
|
|
Protocol = MediaProtocol.File
|
|
|
|
}
|
|
|
|
},
|
2021-11-27 12:10:57 +01:00
|
|
|
cancellationToken);
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|