2020-07-22 07:34:51 -04:00
#pragma warning disable CS1591
2019-01-13 15:02:23 -05:00
using System ;
2018-12-14 04:40:55 -05:00
using System.Collections.Concurrent ;
2020-03-26 19:10:16 -04:00
using System.Diagnostics ;
2021-05-20 16:10:19 -04:00
using System.Diagnostics.CodeAnalysis ;
2018-12-14 04:40:55 -05:00
using System.Globalization ;
using System.IO ;
using System.Linq ;
2020-08-31 13:07:40 -04:00
using System.Net.Http ;
2018-12-14 04:40:55 -05:00
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
2021-10-10 12:48:11 -04:00
using MediaBrowser.Common ;
2019-01-06 14:59:13 -05:00
using MediaBrowser.Common.Configuration ;
using MediaBrowser.Common.Extensions ;
using MediaBrowser.Common.Net ;
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.MediaEncoding ;
2018-12-14 04:40:55 -05:00
using MediaBrowser.Model.Dto ;
2019-01-06 14:59:13 -05:00
using MediaBrowser.Model.Entities ;
2019-01-13 14:26:04 -05:00
using MediaBrowser.Model.IO ;
2019-01-06 14:59:13 -05:00
using MediaBrowser.Model.MediaInfo ;
using Microsoft.Extensions.Logging ;
2019-01-16 14:50:40 -05:00
using UtfUnknown ;
2018-12-14 04:40:55 -05:00
namespace MediaBrowser.MediaEncoding.Subtitles
{
2021-03-08 21:04:47 -05:00
public sealed class SubtitleEncoder : ISubtitleEncoder
2018-12-14 04:40:55 -05:00
{
2020-06-05 20:15:56 -04:00
private readonly ILogger < SubtitleEncoder > _logger ;
2018-12-14 04:40:55 -05:00
private readonly IApplicationPaths _appPaths ;
private readonly IFileSystem _fileSystem ;
private readonly IMediaEncoder _mediaEncoder ;
2020-08-31 13:07:40 -04:00
private readonly IHttpClientFactory _httpClientFactory ;
2018-12-14 04:40:55 -05:00
private readonly IMediaSourceManager _mediaSourceManager ;
2020-08-04 11:08:09 -04:00
/// <summary>
/// The _semaphoreLocks.
/// </summary>
private readonly ConcurrentDictionary < string , SemaphoreSlim > _semaphoreLocks =
new ConcurrentDictionary < string , SemaphoreSlim > ( ) ;
2019-01-17 17:55:05 -05:00
public SubtitleEncoder (
2019-10-26 16:53:53 -04:00
ILogger < SubtitleEncoder > logger ,
2019-01-04 12:46:52 -05:00
IApplicationPaths appPaths ,
IFileSystem fileSystem ,
IMediaEncoder mediaEncoder ,
2020-08-31 13:07:40 -04:00
IHttpClientFactory httpClientFactory ,
2020-03-26 19:45:48 -04:00
IMediaSourceManager mediaSourceManager )
2018-12-14 04:40:55 -05:00
{
2019-10-26 16:53:53 -04:00
_logger = logger ;
2018-12-14 04:40:55 -05:00
_appPaths = appPaths ;
_fileSystem = fileSystem ;
_mediaEncoder = mediaEncoder ;
2020-08-31 13:07:40 -04:00
_httpClientFactory = httpClientFactory ;
2019-01-04 12:46:52 -05:00
_mediaSourceManager = mediaSourceManager ;
2018-12-14 04:40:55 -05:00
}
2019-01-13 15:31:14 -05:00
private string SubtitleCachePath = > Path . Combine ( _appPaths . DataPath , "subtitles" ) ;
2018-12-14 04:40:55 -05:00
2019-10-26 16:53:53 -04:00
private Stream ConvertSubtitles (
Stream stream ,
2018-12-14 04:40:55 -05:00
string inputFormat ,
string outputFormat ,
long startTimeTicks ,
2019-01-20 02:17:31 -05:00
long endTimeTicks ,
2018-12-14 04:40:55 -05:00
bool preserveOriginalTimestamps ,
CancellationToken cancellationToken )
{
var ms = new MemoryStream ( ) ;
try
{
2021-05-20 16:10:19 -04:00
var reader = GetReader ( inputFormat ) ;
2018-12-14 04:40:55 -05:00
var trackInfo = reader . Parse ( stream , cancellationToken ) ;
FilterEvents ( trackInfo , startTimeTicks , endTimeTicks , preserveOriginalTimestamps ) ;
var writer = GetWriter ( outputFormat ) ;
writer . Write ( trackInfo , ms , cancellationToken ) ;
ms . Position = 0 ;
}
catch
{
ms . Dispose ( ) ;
throw ;
}
return ms ;
}
2019-01-20 02:17:31 -05:00
private void FilterEvents ( SubtitleTrackInfo track , long startPositionTicks , long endTimeTicks , bool preserveTimestamps )
2018-12-14 04:40:55 -05:00
{
// Drop subs that are earlier than what we're looking for
track . TrackEvents = track . TrackEvents
. SkipWhile ( i = > ( i . StartPositionTicks - startPositionTicks ) < 0 | | ( i . EndPositionTicks - startPositionTicks ) < 0 )
. ToArray ( ) ;
2019-01-20 02:17:31 -05:00
if ( endTimeTicks > 0 )
2018-12-14 04:40:55 -05:00
{
track . TrackEvents = track . TrackEvents
2019-01-20 11:06:40 -05:00
. TakeWhile ( i = > i . StartPositionTicks < = endTimeTicks )
2018-12-14 04:40:55 -05:00
. ToArray ( ) ;
}
if ( ! preserveTimestamps )
{
foreach ( var trackEvent in track . TrackEvents )
{
trackEvent . EndPositionTicks - = startPositionTicks ;
trackEvent . StartPositionTicks - = startPositionTicks ;
}
}
}
async Task < Stream > ISubtitleEncoder . GetSubtitles ( BaseItem item , string mediaSourceId , int subtitleStreamIndex , string outputFormat , long startTimeTicks , long endTimeTicks , bool preserveOriginalTimestamps , CancellationToken cancellationToken )
{
if ( item = = null )
{
2019-01-06 15:50:43 -05:00
throw new ArgumentNullException ( nameof ( item ) ) ;
2018-12-14 04:40:55 -05:00
}
2020-06-15 17:43:52 -04:00
2018-12-14 04:40:55 -05:00
if ( string . IsNullOrWhiteSpace ( mediaSourceId ) )
{
2019-01-06 15:50:43 -05:00
throw new ArgumentNullException ( nameof ( mediaSourceId ) ) ;
2018-12-14 04:40:55 -05:00
}
2019-12-04 15:39:27 -05:00
var mediaSources = await _mediaSourceManager . GetPlaybackMediaSources ( item , null , true , false , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
var mediaSource = mediaSources
. First ( i = > string . Equals ( i . Id , mediaSourceId , StringComparison . OrdinalIgnoreCase ) ) ;
var subtitleStream = mediaSource . MediaStreams
. First ( i = > i . Type = = MediaStreamType . Subtitle & & i . Index = = subtitleStreamIndex ) ;
var subtitle = await GetSubtitleStream ( mediaSource , subtitleStream , cancellationToken )
. ConfigureAwait ( false ) ;
2021-12-24 16:18:24 -05:00
var inputFormat = subtitle . Format ;
2018-12-14 04:40:55 -05:00
// Return the original if the same format is being requested
// Character encoding was already handled in GetSubtitleStream
if ( string . Equals ( inputFormat , outputFormat , StringComparison . OrdinalIgnoreCase ) )
{
2021-12-24 16:18:24 -05:00
return subtitle . Stream ;
2018-12-14 04:40:55 -05:00
}
2021-12-24 16:18:24 -05:00
using ( var stream = subtitle . Stream )
2018-12-14 04:40:55 -05:00
{
2019-01-06 14:59:13 -05:00
return ConvertSubtitles ( stream , inputFormat , outputFormat , startTimeTicks , endTimeTicks , preserveOriginalTimestamps , cancellationToken ) ;
2018-12-14 04:40:55 -05:00
}
}
2021-12-24 16:18:24 -05:00
private async Task < ( Stream Stream , string Format ) > GetSubtitleStream (
2019-01-06 14:59:13 -05:00
MediaSourceInfo mediaSource ,
2018-12-14 04:40:55 -05:00
MediaStream subtitleStream ,
CancellationToken cancellationToken )
{
2021-02-21 10:53:20 -05:00
var fileInfo = await GetReadableFile ( mediaSource , subtitleStream , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
2021-02-21 10:53:20 -05:00
var stream = await GetSubtitleStream ( fileInfo , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
2019-01-06 14:59:13 -05:00
return ( stream , fileInfo . Format ) ;
2018-12-14 04:40:55 -05:00
}
2021-02-21 10:53:20 -05:00
private async Task < Stream > GetSubtitleStream ( SubtitleInfo fileInfo , CancellationToken cancellationToken )
2018-12-14 04:40:55 -05:00
{
2021-02-21 10:53:20 -05:00
if ( fileInfo . IsExternal )
2018-12-14 04:40:55 -05:00
{
2021-02-21 10:53:20 -05:00
using ( var stream = await GetStream ( fileInfo . Path , fileInfo . Protocol , cancellationToken ) . ConfigureAwait ( false ) )
2018-12-14 04:40:55 -05:00
{
2019-10-26 16:53:53 -04:00
var result = CharsetDetector . DetectFromStream ( stream ) . Detected ;
2020-03-12 12:18:49 -04:00
stream . Position = 0 ;
2019-10-26 16:53:53 -04:00
if ( result ! = null )
2018-12-14 04:40:55 -05:00
{
2021-02-21 10:53:20 -05:00
_logger . LogDebug ( "charset {CharSet} detected for {Path}" , result . EncodingName , fileInfo . Path ) ;
2018-12-14 04:40:55 -05:00
2019-10-26 16:53:53 -04:00
using var reader = new StreamReader ( stream , result . Encoding ) ;
var text = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
2019-10-26 16:53:53 -04:00
return new MemoryStream ( Encoding . UTF8 . GetBytes ( text ) ) ;
2018-12-14 04:40:55 -05:00
}
}
2020-03-12 12:18:49 -04:00
}
2020-03-23 12:39:12 -04:00
2021-06-12 16:20:35 -04:00
return AsyncFile . OpenRead ( fileInfo . Path ) ;
2018-12-14 04:40:55 -05:00
}
2021-09-18 09:08:17 -04:00
internal async Task < SubtitleInfo > GetReadableFile (
2020-09-24 02:41:42 -04:00
MediaSourceInfo mediaSource ,
2018-12-14 04:40:55 -05:00
MediaStream subtitleStream ,
CancellationToken cancellationToken )
{
if ( ! subtitleStream . IsExternal )
{
string outputFormat ;
string outputCodec ;
2021-09-18 09:08:17 -04:00
if ( string . Equals ( subtitleStream . Codec , "ass" , StringComparison . OrdinalIgnoreCase )
| | string . Equals ( subtitleStream . Codec , "ssa" , StringComparison . OrdinalIgnoreCase )
| | string . Equals ( subtitleStream . Codec , "srt" , StringComparison . OrdinalIgnoreCase ) )
2018-12-14 04:40:55 -05:00
{
2019-01-06 14:59:13 -05:00
// Extract
2018-12-14 04:40:55 -05:00
outputCodec = "copy" ;
outputFormat = subtitleStream . Codec ;
}
else if ( string . Equals ( subtitleStream . Codec , "subrip" , StringComparison . OrdinalIgnoreCase ) )
{
2019-01-06 14:59:13 -05:00
// Extract
2018-12-14 04:40:55 -05:00
outputCodec = "copy" ;
outputFormat = "srt" ;
}
else
{
2019-01-06 14:59:13 -05:00
// Extract
2018-12-14 04:40:55 -05:00
outputCodec = "srt" ;
outputFormat = "srt" ;
}
2019-01-06 14:59:13 -05:00
// Extract
2021-02-21 10:53:20 -05:00
var outputPath = GetSubtitleCachePath ( mediaSource , subtitleStream . Index , "." + outputFormat ) ;
2018-12-14 04:40:55 -05:00
2021-02-21 10:53:20 -05:00
await ExtractTextSubtitle ( mediaSource , subtitleStream . Index , outputCodec , outputPath , cancellationToken )
2018-12-14 04:40:55 -05:00
. ConfigureAwait ( false ) ;
2019-01-06 14:59:13 -05:00
return new SubtitleInfo ( outputPath , MediaProtocol . File , outputFormat , false ) ;
2018-12-14 04:40:55 -05:00
}
var currentFormat = ( Path . GetExtension ( subtitleStream . Path ) ? ? subtitleStream . Codec )
. TrimStart ( '.' ) ;
2021-09-18 09:08:17 -04:00
if ( ! TryGetReader ( currentFormat , out _ ) )
2018-12-14 04:40:55 -05:00
{
2019-01-06 14:59:13 -05:00
// Convert
2021-02-21 10:53:20 -05:00
var outputPath = GetSubtitleCachePath ( mediaSource , subtitleStream . Index , ".srt" ) ;
2018-12-14 04:40:55 -05:00
2020-09-24 02:41:42 -04:00
await ConvertTextSubtitleToSrt ( subtitleStream . Path , subtitleStream . Language , mediaSource , outputPath , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
2019-01-06 14:59:13 -05:00
return new SubtitleInfo ( outputPath , MediaProtocol . File , "srt" , true ) ;
}
2021-09-18 09:08:17 -04:00
// It's possbile that the subtitleStream and mediaSource don't share the same protocol (e.g. .STRM file with local subs)
return new SubtitleInfo ( subtitleStream . Path , _mediaSourceManager . GetPathProtocol ( subtitleStream . Path ) , currentFormat , true ) ;
2019-01-06 14:59:13 -05:00
}
2021-05-20 16:10:19 -04:00
private bool TryGetReader ( string format , [ NotNullWhen ( true ) ] out ISubtitleParser ? value )
2018-12-14 04:40:55 -05:00
{
if ( string . Equals ( format , SubtitleFormat . SRT , StringComparison . OrdinalIgnoreCase ) )
{
2021-05-20 16:10:19 -04:00
value = new SrtParser ( _logger ) ;
return true ;
2018-12-14 04:40:55 -05:00
}
2020-06-15 17:43:52 -04:00
2018-12-14 04:40:55 -05:00
if ( string . Equals ( format , SubtitleFormat . SSA , StringComparison . OrdinalIgnoreCase ) )
{
2021-05-20 16:10:19 -04:00
value = new SsaParser ( _logger ) ;
return true ;
2018-12-14 04:40:55 -05:00
}
2020-06-15 17:43:52 -04:00
2018-12-14 04:40:55 -05:00
if ( string . Equals ( format , SubtitleFormat . ASS , StringComparison . OrdinalIgnoreCase ) )
{
2021-05-20 16:10:19 -04:00
value = new AssParser ( _logger ) ;
return true ;
2018-12-14 04:40:55 -05:00
}
2021-05-20 16:10:19 -04:00
value = null ;
return false ;
}
private ISubtitleParser GetReader ( string format )
{
if ( TryGetReader ( format , out var reader ) )
2018-12-14 04:40:55 -05:00
{
2021-05-20 16:10:19 -04:00
return reader ;
2018-12-14 04:40:55 -05:00
}
2021-05-20 16:10:19 -04:00
throw new ArgumentException ( "Unsupported format: " + format ) ;
2018-12-14 04:40:55 -05:00
}
2021-05-20 16:10:19 -04:00
private bool TryGetWriter ( string format , [ NotNullWhen ( true ) ] out ISubtitleWriter ? value )
2018-12-14 04:40:55 -05:00
{
if ( string . IsNullOrEmpty ( format ) )
{
2019-01-06 15:50:43 -05:00
throw new ArgumentNullException ( nameof ( format ) ) ;
2018-12-14 04:40:55 -05:00
}
if ( string . Equals ( format , "json" , StringComparison . OrdinalIgnoreCase ) )
{
2021-05-20 16:10:19 -04:00
value = new JsonWriter ( ) ;
return true ;
2018-12-14 04:40:55 -05:00
}
2020-06-15 17:43:52 -04:00
2018-12-14 04:40:55 -05:00
if ( string . Equals ( format , SubtitleFormat . SRT , StringComparison . OrdinalIgnoreCase ) )
{
2021-05-20 16:10:19 -04:00
value = new SrtWriter ( ) ;
return true ;
2018-12-14 04:40:55 -05:00
}
2020-06-15 17:43:52 -04:00
2018-12-14 04:40:55 -05:00
if ( string . Equals ( format , SubtitleFormat . VTT , StringComparison . OrdinalIgnoreCase ) )
{
2021-05-20 16:10:19 -04:00
value = new VttWriter ( ) ;
return true ;
2018-12-14 04:40:55 -05:00
}
2020-06-15 17:43:52 -04:00
2018-12-14 04:40:55 -05:00
if ( string . Equals ( format , SubtitleFormat . TTML , StringComparison . OrdinalIgnoreCase ) )
{
2021-05-20 16:10:19 -04:00
value = new TtmlWriter ( ) ;
return true ;
2018-12-14 04:40:55 -05:00
}
2021-05-20 16:10:19 -04:00
value = null ;
return false ;
2018-12-14 04:40:55 -05:00
}
private ISubtitleWriter GetWriter ( string format )
{
2021-05-20 16:10:19 -04:00
if ( TryGetWriter ( format , out var writer ) )
2018-12-14 04:40:55 -05:00
{
return writer ;
}
throw new ArgumentException ( "Unsupported format: " + format ) ;
}
/// <summary>
/// Gets the lock.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>System.Object.</returns>
private SemaphoreSlim GetLock ( string filename )
{
2020-07-22 07:34:51 -04:00
return _semaphoreLocks . GetOrAdd ( filename , _ = > new SemaphoreSlim ( 1 , 1 ) ) ;
2018-12-14 04:40:55 -05:00
}
/// <summary>
/// Converts the text subtitle to SRT.
/// </summary>
/// <param name="inputPath">The input path.</param>
2020-08-04 10:20:52 -04:00
/// <param name="language">The language.</param>
2020-09-24 02:41:42 -04:00
/// <param name="mediaSource">The input mediaSource.</param>
2018-12-14 04:40:55 -05:00
/// <param name="outputPath">The output path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2020-09-24 02:41:42 -04:00
private async Task ConvertTextSubtitleToSrt ( string inputPath , string language , MediaSourceInfo mediaSource , string outputPath , CancellationToken cancellationToken )
2018-12-14 04:40:55 -05:00
{
var semaphore = GetLock ( outputPath ) ;
await semaphore . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
try
{
2019-01-26 16:59:53 -05:00
if ( ! File . Exists ( outputPath ) )
2018-12-14 04:40:55 -05:00
{
2020-09-24 02:41:42 -04:00
await ConvertTextSubtitleToSrtInternal ( inputPath , language , mediaSource , outputPath , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
}
}
finally
{
semaphore . Release ( ) ;
}
}
/// <summary>
/// Converts the text subtitle to SRT internal.
/// </summary>
/// <param name="inputPath">The input path.</param>
2020-08-04 10:20:52 -04:00
/// <param name="language">The language.</param>
2020-09-24 02:41:42 -04:00
/// <param name="mediaSource">The input mediaSource.</param>
2018-12-14 04:40:55 -05:00
/// <param name="outputPath">The output path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2019-01-13 15:37:13 -05:00
/// <exception cref="ArgumentNullException">
2020-08-04 11:08:09 -04:00
/// The <c>inputPath</c> or <c>outputPath</c> is <c>null</c>.
2018-12-14 04:40:55 -05:00
/// </exception>
2020-09-24 02:41:42 -04:00
private async Task ConvertTextSubtitleToSrtInternal ( string inputPath , string language , MediaSourceInfo mediaSource , string outputPath , CancellationToken cancellationToken )
2018-12-14 04:40:55 -05:00
{
if ( string . IsNullOrEmpty ( inputPath ) )
{
2019-01-06 15:50:43 -05:00
throw new ArgumentNullException ( nameof ( inputPath ) ) ;
2018-12-14 04:40:55 -05:00
}
if ( string . IsNullOrEmpty ( outputPath ) )
{
2019-01-06 15:50:43 -05:00
throw new ArgumentNullException ( nameof ( outputPath ) ) ;
2018-12-14 04:40:55 -05:00
}
2021-05-20 16:10:19 -04:00
Directory . CreateDirectory ( Path . GetDirectoryName ( outputPath ) ? ? throw new ArgumentException ( $"Provided path ({outputPath}) is not valid." , nameof ( outputPath ) ) ) ;
2018-12-14 04:40:55 -05:00
2020-09-24 02:41:42 -04:00
var encodingParam = await GetSubtitleFileCharacterSet ( inputPath , language , mediaSource . Protocol , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
2019-07-14 10:05:45 -04:00
// FFmpeg automatically convert character encoding when it is UTF-16
// If we specify character encoding, it rejects with "do not specify a character encoding" and "Unable to recode subtitle event"
2020-08-20 06:16:24 -04:00
if ( ( inputPath . EndsWith ( ".smi" , StringComparison . Ordinal ) | | inputPath . EndsWith ( ".sami" , StringComparison . Ordinal ) ) & &
2020-04-15 13:30:23 -04:00
( encodingParam . Equals ( "UTF-16BE" , StringComparison . OrdinalIgnoreCase ) | |
encodingParam . Equals ( "UTF-16LE" , StringComparison . OrdinalIgnoreCase ) ) )
2019-07-14 10:05:45 -04:00
{
2020-08-04 11:08:09 -04:00
encodingParam = string . Empty ;
2019-07-14 10:05:45 -04:00
}
else if ( ! string . IsNullOrEmpty ( encodingParam ) )
2018-12-14 04:40:55 -05:00
{
encodingParam = " -sub_charenc " + encodingParam ;
}
2020-03-26 20:53:08 -04:00
int exitCode ;
2018-12-14 04:40:55 -05:00
2020-04-11 13:25:50 -04:00
using ( var process = new Process
{
2020-04-11 13:46:31 -04:00
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true ,
UseShellExecute = false ,
FileName = _mediaEncoder . EncoderPath ,
2020-08-07 13:26:28 -04:00
Arguments = string . Format ( CultureInfo . InvariantCulture , "{0} -i \"{1}\" -c:s srt \"{2}\"" , encodingParam , inputPath , outputPath ) ,
2020-04-11 13:46:31 -04:00
WindowStyle = ProcessWindowStyle . Hidden ,
ErrorDialog = false
} ,
EnableRaisingEvents = true
} )
2018-12-14 04:40:55 -05:00
{
2020-03-26 20:53:08 -04:00
_logger . LogInformation ( "{0} {1}" , process . StartInfo . FileName , process . StartInfo . Arguments ) ;
2018-12-14 04:40:55 -05:00
try
{
2020-03-26 20:53:08 -04:00
process . Start ( ) ;
2018-12-14 04:40:55 -05:00
}
catch ( Exception ex )
{
2020-03-26 20:53:08 -04:00
_logger . LogError ( ex , "Error starting ffmpeg" ) ;
throw ;
2018-12-14 04:40:55 -05:00
}
2022-02-14 10:33:11 -05:00
var ranToCompletion = await process . WaitForExitAsync ( TimeSpan . FromMinutes ( 30 ) ) . ConfigureAwait ( false ) ;
2020-03-26 20:53:08 -04:00
if ( ! ranToCompletion )
{
try
{
_logger . LogInformation ( "Killing ffmpeg subtitle conversion process" ) ;
process . Kill ( ) ;
}
catch ( Exception ex )
{
_logger . LogError ( ex , "Error killing subtitle conversion process" ) ;
}
}
exitCode = ranToCompletion ? process . ExitCode : - 1 ;
}
2018-12-14 04:40:55 -05:00
var failed = false ;
if ( exitCode = = - 1 )
{
failed = true ;
2019-01-26 16:59:53 -05:00
if ( File . Exists ( outputPath ) )
2018-12-14 04:40:55 -05:00
{
try
{
2018-12-14 18:48:06 -05:00
_logger . LogInformation ( "Deleting converted subtitle due to failure: " , outputPath ) ;
2018-12-14 04:40:55 -05:00
_fileSystem . DeleteFile ( outputPath ) ;
}
catch ( IOException ex )
{
2019-01-06 14:59:13 -05:00
_logger . LogError ( ex , "Error deleting converted subtitle {Path}" , outputPath ) ;
2018-12-14 04:40:55 -05:00
}
}
}
2019-01-26 16:59:53 -05:00
else if ( ! File . Exists ( outputPath ) )
2018-12-14 04:40:55 -05:00
{
failed = true ;
}
if ( failed )
{
2019-09-20 06:42:08 -04:00
_logger . LogError ( "ffmpeg subtitle conversion failed for {Path}" , inputPath ) ;
2018-12-14 04:40:55 -05:00
2021-03-08 21:04:47 -05:00
throw new FfmpegException (
2019-09-20 06:42:08 -04:00
string . Format ( CultureInfo . InvariantCulture , "ffmpeg subtitle conversion failed for {0}" , inputPath ) ) ;
2018-12-14 04:40:55 -05:00
}
2019-09-20 06:42:08 -04:00
2020-08-20 06:16:24 -04:00
await SetAssFont ( outputPath , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
2019-01-06 14:59:13 -05:00
_logger . LogInformation ( "ffmpeg subtitle conversion succeeded for {Path}" , inputPath ) ;
2018-12-14 04:40:55 -05:00
}
/// <summary>
/// Extracts the text subtitle.
/// </summary>
2020-09-24 02:41:42 -04:00
/// <param name="mediaSource">The mediaSource.</param>
2018-12-14 04:40:55 -05:00
/// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
/// <param name="outputCodec">The output codec.</param>
/// <param name="outputPath">The output path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2020-08-04 11:08:09 -04:00
/// <exception cref="ArgumentException">Must use inputPath list overload.</exception>
2019-01-06 14:59:13 -05:00
private async Task ExtractTextSubtitle (
2020-09-24 02:41:42 -04:00
MediaSourceInfo mediaSource ,
2019-01-06 14:59:13 -05:00
int subtitleStreamIndex ,
string outputCodec ,
string outputPath ,
CancellationToken cancellationToken )
2018-12-14 04:40:55 -05:00
{
var semaphore = GetLock ( outputPath ) ;
await semaphore . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
try
{
2019-01-26 16:59:53 -05:00
if ( ! File . Exists ( outputPath ) )
2018-12-14 04:40:55 -05:00
{
2019-10-26 16:53:53 -04:00
await ExtractTextSubtitleInternal (
2021-02-21 10:53:20 -05:00
_mediaEncoder . GetInputArgument ( mediaSource . Path , mediaSource ) ,
2019-10-26 16:53:53 -04:00
subtitleStreamIndex ,
outputCodec ,
outputPath ,
cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
}
}
finally
{
semaphore . Release ( ) ;
}
}
2019-01-06 14:59:13 -05:00
private async Task ExtractTextSubtitleInternal (
string inputPath ,
int subtitleStreamIndex ,
string outputCodec ,
string outputPath ,
CancellationToken cancellationToken )
2018-12-14 04:40:55 -05:00
{
if ( string . IsNullOrEmpty ( inputPath ) )
{
2019-01-06 15:50:43 -05:00
throw new ArgumentNullException ( nameof ( inputPath ) ) ;
2018-12-14 04:40:55 -05:00
}
if ( string . IsNullOrEmpty ( outputPath ) )
{
2019-01-06 15:50:43 -05:00
throw new ArgumentNullException ( nameof ( outputPath ) ) ;
2018-12-14 04:40:55 -05:00
}
2021-05-20 16:10:19 -04:00
Directory . CreateDirectory ( Path . GetDirectoryName ( outputPath ) ? ? throw new ArgumentException ( $"Provided path ({outputPath}) is not valid." , nameof ( outputPath ) ) ) ;
2018-12-14 04:40:55 -05:00
2019-10-26 16:53:53 -04:00
var processArgs = string . Format (
CultureInfo . InvariantCulture ,
"-i {0} -map 0:{1} -an -vn -c:s {2} \"{3}\"" ,
inputPath ,
subtitleStreamIndex ,
outputCodec ,
outputPath ) ;
2018-12-14 04:40:55 -05:00
2020-03-26 20:53:08 -04:00
int exitCode ;
2018-12-14 04:40:55 -05:00
2020-04-11 13:25:50 -04:00
using ( var process = new Process
{
2020-04-11 13:46:31 -04:00
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true ,
UseShellExecute = false ,
FileName = _mediaEncoder . EncoderPath ,
Arguments = processArgs ,
WindowStyle = ProcessWindowStyle . Hidden ,
ErrorDialog = false
} ,
EnableRaisingEvents = true
} )
2018-12-14 04:40:55 -05:00
{
2020-03-26 20:53:08 -04:00
_logger . LogInformation ( "{File} {Arguments}" , process . StartInfo . FileName , process . StartInfo . Arguments ) ;
2018-12-14 04:40:55 -05:00
try
{
2020-03-26 20:53:08 -04:00
process . Start ( ) ;
2018-12-14 04:40:55 -05:00
}
catch ( Exception ex )
{
2020-03-26 20:53:08 -04:00
_logger . LogError ( ex , "Error starting ffmpeg" ) ;
throw ;
2018-12-14 04:40:55 -05:00
}
2020-03-26 20:53:08 -04:00
var ranToCompletion = await process . WaitForExitAsync ( TimeSpan . FromMinutes ( 5 ) ) . ConfigureAwait ( false ) ;
if ( ! ranToCompletion )
{
try
{
_logger . LogWarning ( "Killing ffmpeg subtitle extraction process" ) ;
process . Kill ( ) ;
}
catch ( Exception ex )
{
_logger . LogError ( ex , "Error killing subtitle extraction process" ) ;
}
}
exitCode = ranToCompletion ? process . ExitCode : - 1 ;
}
2018-12-14 04:40:55 -05:00
var failed = false ;
if ( exitCode = = - 1 )
{
failed = true ;
try
{
2019-01-06 14:59:13 -05:00
_logger . LogWarning ( "Deleting extracted subtitle due to failure: {Path}" , outputPath ) ;
2018-12-14 04:40:55 -05:00
_fileSystem . DeleteFile ( outputPath ) ;
}
catch ( FileNotFoundException )
{
}
catch ( IOException ex )
{
2019-01-06 14:59:13 -05:00
_logger . LogError ( ex , "Error deleting extracted subtitle {Path}" , outputPath ) ;
2018-12-14 04:40:55 -05:00
}
}
2019-01-26 16:59:53 -05:00
else if ( ! File . Exists ( outputPath ) )
2018-12-14 04:40:55 -05:00
{
failed = true ;
}
if ( failed )
{
2021-11-09 16:29:33 -05:00
_logger . LogError ( "ffmpeg subtitle extraction failed for {InputPath} to {OutputPath}" , inputPath , outputPath ) ;
2018-12-14 04:40:55 -05:00
2021-11-09 16:29:33 -05:00
throw new FfmpegException (
string . Format ( CultureInfo . InvariantCulture , "ffmpeg subtitle extraction failed for {0} to {1}" , inputPath , outputPath ) ) ;
2018-12-14 04:40:55 -05:00
}
else
{
2021-11-09 16:29:33 -05:00
_logger . LogInformation ( "ffmpeg subtitle extraction completed for {InputPath} to {OutputPath}" , inputPath , outputPath ) ;
2018-12-14 04:40:55 -05:00
}
if ( string . Equals ( outputCodec , "ass" , StringComparison . OrdinalIgnoreCase ) )
{
2020-08-20 06:16:24 -04:00
await SetAssFont ( outputPath , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
}
}
/// <summary>
/// Sets the ass font.
/// </summary>
/// <param name="file">The file.</param>
2020-08-20 06:16:24 -04:00
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <c>System.Threading.CancellationToken.None</c>.</param>
2018-12-14 04:40:55 -05:00
/// <returns>Task.</returns>
2020-08-20 06:16:24 -04:00
private async Task SetAssFont ( string file , CancellationToken cancellationToken = default )
2018-12-14 04:40:55 -05:00
{
2019-01-06 14:59:13 -05:00
_logger . LogInformation ( "Setting ass font within {File}" , file ) ;
2018-12-14 04:40:55 -05:00
string text ;
Encoding encoding ;
2021-06-12 16:20:35 -04:00
using ( var fileStream = AsyncFile . OpenRead ( file ) )
2019-01-06 14:59:13 -05:00
using ( var reader = new StreamReader ( fileStream , true ) )
2018-12-14 04:40:55 -05:00
{
2019-01-06 14:59:13 -05:00
encoding = reader . CurrentEncoding ;
2018-12-14 04:40:55 -05:00
2019-01-06 14:59:13 -05:00
text = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
}
2020-08-20 06:16:24 -04:00
var newText = text . Replace ( ",Arial," , ",Arial Unicode MS," , StringComparison . Ordinal ) ;
2018-12-14 04:40:55 -05:00
2020-08-20 06:16:24 -04:00
if ( ! string . Equals ( text , newText , StringComparison . Ordinal ) )
2018-12-14 04:40:55 -05:00
{
2022-01-22 17:36:42 -05:00
var fileStream = new FileStream ( file , FileMode . Create , FileAccess . Write , FileShare . None , IODefaults . FileStreamBufferSize , FileOptions . Asynchronous ) ;
var writer = new StreamWriter ( fileStream , encoding ) ;
await using ( fileStream . ConfigureAwait ( false ) )
await using ( writer . ConfigureAwait ( false ) )
2018-12-14 04:40:55 -05:00
{
2020-08-20 06:16:24 -04:00
await writer . WriteAsync ( newText . AsMemory ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
2018-12-14 04:40:55 -05:00
}
}
}
2021-02-21 10:53:20 -05:00
private string GetSubtitleCachePath ( MediaSourceInfo mediaSource , int subtitleStreamIndex , string outputSubtitleExtension )
2018-12-14 04:40:55 -05:00
{
2020-09-24 02:41:42 -04:00
if ( mediaSource . Protocol = = MediaProtocol . File )
2018-12-14 04:40:55 -05:00
{
var ticksParam = string . Empty ;
2021-02-21 10:53:20 -05:00
var date = _fileSystem . GetLastWriteTimeUtc ( mediaSource . Path ) ;
2018-12-14 04:40:55 -05:00
2021-02-21 10:53:20 -05:00
ReadOnlySpan < char > filename = ( mediaSource . Path + "_" + subtitleStreamIndex . ToString ( CultureInfo . InvariantCulture ) + "_" + date . Ticks . ToString ( CultureInfo . InvariantCulture ) + ticksParam ) . GetMD5 ( ) + outputSubtitleExtension ;
2018-12-14 04:40:55 -05:00
2020-07-29 07:17:01 -04:00
var prefix = filename . Slice ( 0 , 1 ) ;
2018-12-14 04:40:55 -05:00
2020-07-29 07:17:01 -04:00
return Path . Join ( SubtitleCachePath , prefix , filename ) ;
2018-12-14 04:40:55 -05:00
}
else
{
2021-02-21 10:53:20 -05:00
ReadOnlySpan < char > filename = ( mediaSource . Path + "_" + subtitleStreamIndex . ToString ( CultureInfo . InvariantCulture ) ) . GetMD5 ( ) + outputSubtitleExtension ;
2018-12-14 04:40:55 -05:00
2020-07-29 07:17:01 -04:00
var prefix = filename . Slice ( 0 , 1 ) ;
2018-12-14 04:40:55 -05:00
2020-07-29 07:17:01 -04:00
return Path . Join ( SubtitleCachePath , prefix , filename ) ;
2018-12-14 04:40:55 -05:00
}
}
2019-10-26 16:53:53 -04:00
/// <inheritdoc />
2018-12-14 04:40:55 -05:00
public async Task < string > GetSubtitleFileCharacterSet ( string path , string language , MediaProtocol protocol , CancellationToken cancellationToken )
{
2019-10-26 16:53:53 -04:00
using ( var stream = await GetStream ( path , protocol , cancellationToken ) . ConfigureAwait ( false ) )
{
2021-05-20 16:10:19 -04:00
var charset = CharsetDetector . DetectFromStream ( stream ) . Detected ? . EncodingName ? ? string . Empty ;
2018-12-14 04:40:55 -05:00
2020-03-30 02:46:05 -04:00
// UTF16 is automatically converted to UTF8 by FFmpeg, do not specify a character encoding
2020-08-20 06:16:24 -04:00
if ( ( path . EndsWith ( ".ass" , StringComparison . Ordinal ) | | path . EndsWith ( ".ssa" , StringComparison . Ordinal ) | | path . EndsWith ( ".srt" , StringComparison . Ordinal ) )
2020-03-30 02:46:05 -04:00
& & ( string . Equals ( charset , "utf-16le" , StringComparison . OrdinalIgnoreCase )
| | string . Equals ( charset , "utf-16be" , StringComparison . OrdinalIgnoreCase ) ) )
{
2020-08-04 11:08:09 -04:00
charset = string . Empty ;
2020-03-30 02:46:05 -04:00
}
2021-05-20 16:10:19 -04:00
_logger . LogDebug ( "charset {0} detected for {Path}" , charset , path ) ;
2018-12-14 04:40:55 -05:00
2019-10-26 16:53:53 -04:00
return charset ;
}
2018-12-14 04:40:55 -05:00
}
2020-08-31 13:07:40 -04:00
private async Task < Stream > GetStream ( string path , MediaProtocol protocol , CancellationToken cancellationToken )
2018-12-14 04:40:55 -05:00
{
2019-10-26 16:53:53 -04:00
switch ( protocol )
2018-12-14 04:40:55 -05:00
{
2019-10-26 16:53:53 -04:00
case MediaProtocol . Http :
2020-08-31 13:07:40 -04:00
{
using var response = await _httpClientFactory . CreateClient ( NamedClient . Default )
2020-11-13 11:04:31 -05:00
. GetAsync ( new Uri ( path ) , cancellationToken )
2020-08-31 13:07:40 -04:00
. ConfigureAwait ( false ) ;
2020-11-17 13:43:00 -05:00
return await response . Content . ReadAsStreamAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
2020-08-31 13:07:40 -04:00
}
2018-12-14 04:40:55 -05:00
2020-03-24 11:12:06 -04:00
case MediaProtocol . File :
2021-06-12 16:20:35 -04:00
return AsyncFile . OpenRead ( path ) ;
2020-03-24 11:12:06 -04:00
default :
throw new ArgumentOutOfRangeException ( nameof ( protocol ) ) ;
2019-10-26 16:53:53 -04:00
}
2018-12-14 04:40:55 -05:00
}
2020-08-04 11:08:09 -04:00
2021-09-18 09:08:17 -04:00
internal readonly struct SubtitleInfo
2020-08-04 11:08:09 -04:00
{
public SubtitleInfo ( string path , MediaProtocol protocol , string format , bool isExternal )
{
Path = path ;
Protocol = protocol ;
Format = format ;
IsExternal = isExternal ;
}
2021-09-18 09:08:17 -04:00
public string Path { get ; }
2020-08-04 11:08:09 -04:00
2021-09-18 09:08:17 -04:00
public MediaProtocol Protocol { get ; }
2020-08-04 11:08:09 -04:00
2021-09-18 09:08:17 -04:00
public string Format { get ; }
2020-08-04 11:08:09 -04:00
2021-09-18 09:08:17 -04:00
public bool IsExternal { get ; }
2020-08-04 11:08:09 -04:00
}
2018-12-14 04:40:55 -05:00
}
2018-12-14 18:48:06 -05:00
}