2021-11-22 21:47:52 +01:00
|
|
|
#pragma warning disable CA1002, CS1591
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
public class AudioResolver
|
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
public async Task<List<MediaStream>> GetExternalAudioStreams(
|
2021-11-22 21:47:52 +01:00
|
|
|
Video video,
|
|
|
|
int startIndex,
|
|
|
|
IDirectoryService directoryService,
|
2021-11-27 12:10:57 +01:00
|
|
|
NamingOptions namingOptions,
|
|
|
|
bool clearCache,
|
|
|
|
ILocalizationManager localizationManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
CancellationToken cancellationToken)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
|
|
|
var streams = new List<MediaStream>();
|
|
|
|
|
|
|
|
if (!video.IsFileProtocol)
|
|
|
|
{
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
List<string> paths = GetExternalAudioFiles(video, directoryService, namingOptions, clearCache);
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
await AddExternalAudioStreams(streams, paths, startIndex, localizationManager, mediaEncoder, cancellationToken);
|
2021-11-22 21:47:52 +01:00
|
|
|
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
public List<string> GetExternalAudioFiles(
|
2021-11-22 21:47:52 +01:00
|
|
|
Video video,
|
|
|
|
IDirectoryService directoryService,
|
2021-11-27 12:10:57 +01:00
|
|
|
NamingOptions namingOptions,
|
2021-11-22 21:47:52 +01:00
|
|
|
bool clearCache)
|
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
List<string> paths = new List<string>();
|
|
|
|
|
2021-11-22 21:47:52 +01:00
|
|
|
if (!video.IsFileProtocol)
|
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
return paths;
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
paths.AddRange(GetAudioFilesFromFolder(video.ContainingFolderPath, video.Path, directoryService, namingOptions, clearCache));
|
|
|
|
paths.AddRange(GetAudioFilesFromFolder(video.GetInternalMetadataPath(), video.Path, directoryService, namingOptions, clearCache));
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
return paths;
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
private List<string> GetAudioFilesFromFolder(
|
|
|
|
string folder,
|
|
|
|
string videoFileName,
|
|
|
|
IDirectoryService directoryService,
|
|
|
|
NamingOptions namingOptions,
|
|
|
|
bool clearCache)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
List<string> paths = new List<string>();
|
|
|
|
string videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(videoFileName);
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
if (!Directory.Exists(folder))
|
|
|
|
{
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-27 12:10:57 +01:00
|
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(files[i]);
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
if (!AudioFileParser.IsAudioFile(files[i], namingOptions))
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The audio filename must either be equal to the video filename or start with the video filename followed by a dot
|
2021-11-27 12:10:57 +01:00
|
|
|
if (videoFileNameWithoutExtension.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
(fileNameWithoutExtension.Length > videoFileNameWithoutExtension.Length
|
2021-11-22 21:47:52 +01:00
|
|
|
&& fileNameWithoutExtension[videoFileNameWithoutExtension.Length] == '.'
|
2021-11-27 12:10:57 +01:00
|
|
|
&& fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension, StringComparison.OrdinalIgnoreCase)))
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
paths.Add(files[i]);
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-27 12:10:57 +01:00
|
|
|
|
|
|
|
return paths;
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
public async Task AddExternalAudioStreams(
|
|
|
|
List<MediaStream> streams,
|
|
|
|
List<string> paths,
|
|
|
|
int startIndex,
|
|
|
|
ILocalizationManager localizationManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
CancellationToken cancellationToken)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
foreach (string path in paths)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
|
|
|
|
Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(path, mediaEncoder, cancellationToken);
|
2021-11-22 21:47:52 +01:00
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
foreach (MediaStream mediaStream in mediaInfo.MediaStreams)
|
2021-11-22 21:47:52 +01:00
|
|
|
{
|
2021-11-27 12:10:57 +01:00
|
|
|
mediaStream.Index = startIndex++;
|
|
|
|
mediaStream.Type = MediaStreamType.Audio;
|
|
|
|
mediaStream.IsExternal = true;
|
|
|
|
mediaStream.Path = path;
|
|
|
|
mediaStream.IsDefault = false;
|
|
|
|
mediaStream.Title = null;
|
|
|
|
|
|
|
|
if (mediaStream.Language == null)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
streams.Add(mediaStream);
|
2021-11-22 21:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:10:57 +01:00
|
|
|
private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(string path, IMediaEncoder mediaEncoder, 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-27 12:10:57 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|