2014-08-06 01:59:24 +02:00
using MediaBrowser.Controller.Entities ;
2014-05-17 06:24:10 +02:00
using MediaBrowser.Controller.Library ;
2014-06-11 16:42:03 +02:00
using MediaBrowser.Controller.MediaEncoding ;
2014-07-02 20:34:08 +02:00
using MediaBrowser.Controller.Net ;
2014-05-17 06:24:10 +02:00
using MediaBrowser.Controller.Providers ;
using MediaBrowser.Controller.Subtitles ;
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Providers ;
using ServiceStack ;
using System ;
using System.Collections.Generic ;
2014-08-06 03:09:03 +02:00
using System.Globalization ;
2014-08-06 01:59:24 +02:00
using System.IO ;
using System.Linq ;
2014-08-06 03:09:03 +02:00
using System.Text ;
2014-05-17 06:24:10 +02:00
using System.Threading ;
using System.Threading.Tasks ;
2015-10-04 06:23:11 +02:00
using CommonIO ;
2014-12-26 18:45:06 +01:00
using MimeTypes = MediaBrowser . Model . Net . MimeTypes ;
2014-05-17 06:24:10 +02:00
2014-07-04 04:22:57 +02:00
namespace MediaBrowser.Api.Subtitles
2014-05-17 06:24:10 +02:00
{
[Route("/Videos/{Id}/Subtitles/{Index}", "DELETE", Summary = "Deletes an external subtitle file")]
2014-11-15 03:31:03 +01:00
[Authenticated(Roles = "Admin")]
2014-05-17 06:24:10 +02:00
public class DeleteSubtitle
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get ; set ; }
[ApiMember(Name = "Index", Description = "The subtitle stream index", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "DELETE")]
public int Index { get ; set ; }
}
[Route("/Items/{Id}/RemoteSearch/Subtitles/{Language}", "GET")]
2014-07-04 04:22:57 +02:00
[Authenticated]
2014-05-17 06:24:10 +02:00
public class SearchRemoteSubtitles : IReturn < List < RemoteSubtitleInfo > >
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
[ApiMember(Name = "Language", Description = "Language", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Language { get ; set ; }
}
[Route("/Items/{Id}/RemoteSearch/Subtitles/Providers", "GET")]
2014-07-04 04:22:57 +02:00
[Authenticated]
2014-05-17 06:24:10 +02:00
public class GetSubtitleProviders : IReturn < List < SubtitleProviderInfo > >
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
}
[Route("/Items/{Id}/RemoteSearch/Subtitles/{SubtitleId}", "POST")]
2014-07-04 04:22:57 +02:00
[Authenticated]
2014-05-17 06:24:10 +02:00
public class DownloadRemoteSubtitles : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get ; set ; }
[ApiMember(Name = "SubtitleId", Description = "SubtitleId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SubtitleId { get ; set ; }
}
[Route("/Providers/Subtitles/Subtitles/{Id}", "GET")]
2014-07-04 04:22:57 +02:00
[Authenticated]
2014-05-17 06:24:10 +02:00
public class GetRemoteSubtitles : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
}
2014-08-06 01:59:24 +02:00
[Route("/Videos/{Id}/{MediaSourceId}/Subtitles/{Index}/Stream.{Format}", "GET", Summary = "Gets subtitles in a specified format.")]
[Route("/Videos/{Id}/{MediaSourceId}/Subtitles/{Index}/{StartPositionTicks}/Stream.{Format}", "GET", Summary = "Gets subtitles in a specified format.")]
2014-07-04 04:22:57 +02:00
public class GetSubtitle
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
[ApiMember(Name = "MediaSourceId", Description = "MediaSourceId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string MediaSourceId { get ; set ; }
[ApiMember(Name = "Index", Description = "The subtitle stream index", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
public int Index { get ; set ; }
[ApiMember(Name = "Format", Description = "Format", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Format { get ; set ; }
[ApiMember(Name = "StartPositionTicks", Description = "StartPositionTicks", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public long StartPositionTicks { get ; set ; }
2014-08-06 03:09:03 +02:00
[ApiMember(Name = "EndPositionTicks", Description = "EndPositionTicks", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public long? EndPositionTicks { get ; set ; }
2016-05-27 19:17:57 +02:00
[ApiMember(Name = "CopyTimestamps", Description = "CopyTimestamps", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool CopyTimestamps { get ; set ; }
2016-06-05 20:51:18 +02:00
public bool AddVttTimeMap { get ; set ; }
2014-08-06 03:09:03 +02:00
}
[Route("/Videos/{Id}/{MediaSourceId}/Subtitles/{Index}/subtitles.m3u8", "GET", Summary = "Gets an HLS subtitle playlist.")]
2015-04-30 16:07:24 +02:00
[Authenticated]
2014-08-06 03:09:03 +02:00
public class GetSubtitlePlaylist
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
[ApiMember(Name = "MediaSourceId", Description = "MediaSourceId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string MediaSourceId { get ; set ; }
[ApiMember(Name = "Index", Description = "The subtitle stream index", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
public int Index { get ; set ; }
[ApiMember(Name = "SegmentLength", Description = "The subtitle srgment length", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
public int SegmentLength { get ; set ; }
2014-07-04 04:22:57 +02:00
}
2014-05-17 06:24:10 +02:00
public class SubtitleService : BaseApiService
{
private readonly ILibraryManager _libraryManager ;
private readonly ISubtitleManager _subtitleManager ;
2014-06-11 16:42:03 +02:00
private readonly ISubtitleEncoder _subtitleEncoder ;
2015-03-07 23:43:53 +01:00
private readonly IMediaSourceManager _mediaSourceManager ;
2015-03-14 05:50:23 +01:00
private readonly IProviderManager _providerManager ;
2015-09-14 01:07:54 +02:00
private readonly IFileSystem _fileSystem ;
2014-05-17 06:24:10 +02:00
2015-09-14 01:07:54 +02:00
public SubtitleService ( ILibraryManager libraryManager , ISubtitleManager subtitleManager , ISubtitleEncoder subtitleEncoder , IMediaSourceManager mediaSourceManager , IProviderManager providerManager , IFileSystem fileSystem )
2014-05-17 06:24:10 +02:00
{
_libraryManager = libraryManager ;
_subtitleManager = subtitleManager ;
2014-06-11 16:42:03 +02:00
_subtitleEncoder = subtitleEncoder ;
2015-03-07 23:43:53 +01:00
_mediaSourceManager = mediaSourceManager ;
2015-03-14 05:50:23 +01:00
_providerManager = providerManager ;
2015-09-14 01:07:54 +02:00
_fileSystem = fileSystem ;
2014-05-17 06:24:10 +02:00
}
2015-04-09 07:20:23 +02:00
public async Task < object > Get ( GetSubtitlePlaylist request )
2014-08-06 03:09:03 +02:00
{
var item = ( Video ) _libraryManager . GetItemById ( new Guid ( request . Id ) ) ;
2015-04-09 07:20:23 +02:00
var mediaSource = await _mediaSourceManager . GetMediaSource ( item , request . MediaSourceId , false ) . ConfigureAwait ( false ) ;
2014-08-06 03:09:03 +02:00
var builder = new StringBuilder ( ) ;
var runtime = mediaSource . RunTimeTicks ? ? - 1 ;
if ( runtime < = 0 )
{
throw new ArgumentException ( "HLS Subtitles are not supported for this media." ) ;
}
builder . AppendLine ( "#EXTM3U" ) ;
builder . AppendLine ( "#EXT-X-TARGETDURATION:" + request . SegmentLength . ToString ( CultureInfo . InvariantCulture ) ) ;
builder . AppendLine ( "#EXT-X-VERSION:3" ) ;
builder . AppendLine ( "#EXT-X-MEDIA-SEQUENCE:0" ) ;
2016-05-25 04:06:11 +02:00
builder . AppendLine ( "#EXT-X-PLAYLIST-TYPE:VOD" ) ;
2014-08-06 03:09:03 +02:00
long positionTicks = 0 ;
var segmentLengthTicks = TimeSpan . FromSeconds ( request . SegmentLength ) . Ticks ;
2016-03-07 19:50:58 +01:00
var accessToken = AuthorizationContext . GetAuthorizationInfo ( Request ) . Token ;
2014-08-06 03:09:03 +02:00
while ( positionTicks < runtime )
{
var remaining = runtime - positionTicks ;
var lengthTicks = Math . Min ( remaining , segmentLengthTicks ) ;
2016-05-25 04:06:11 +02:00
builder . AppendLine ( "#EXTINF:" + TimeSpan . FromTicks ( lengthTicks ) . TotalSeconds . ToString ( CultureInfo . InvariantCulture ) + "," ) ;
2014-08-06 03:09:03 +02:00
var endPositionTicks = Math . Min ( runtime , positionTicks + segmentLengthTicks ) ;
2016-06-05 20:54:21 +02:00
var url = string . Format ( "stream.vtt?CopyTimestamps=true&AddVttTimeMap=true&StartPositionTicks={0}&EndPositionTicks={1}&api_key={2}" ,
2014-08-06 03:09:03 +02:00
positionTicks . ToString ( CultureInfo . InvariantCulture ) ,
2016-03-07 19:50:58 +01:00
endPositionTicks . ToString ( CultureInfo . InvariantCulture ) ,
accessToken ) ;
2014-08-06 03:09:03 +02:00
builder . AppendLine ( url ) ;
positionTicks + = segmentLengthTicks ;
}
builder . AppendLine ( "#EXT-X-ENDLIST" ) ;
2014-08-22 17:47:44 +02:00
2014-12-26 18:45:06 +01:00
return ResultFactory . GetResult ( builder . ToString ( ) , MimeTypes . GetMimeType ( "playlist.m3u8" ) , new Dictionary < string , string > ( ) ) ;
2014-08-06 03:09:03 +02:00
}
2016-06-05 20:51:18 +02:00
public async Task < object > Get ( GetSubtitle request )
2014-05-17 06:24:10 +02:00
{
2014-08-22 17:47:44 +02:00
if ( string . Equals ( request . Format , "js" , StringComparison . OrdinalIgnoreCase ) )
{
request . Format = "json" ;
}
2014-06-11 16:42:03 +02:00
if ( string . IsNullOrEmpty ( request . Format ) )
2014-05-17 06:24:10 +02:00
{
2014-06-11 16:42:03 +02:00
var item = ( Video ) _libraryManager . GetItemById ( new Guid ( request . Id ) ) ;
2014-05-17 06:24:10 +02:00
2015-03-28 21:22:27 +01:00
var mediaSource = _mediaSourceManager . GetStaticMediaSources ( item , false , null )
2014-06-11 16:42:03 +02:00
. First ( i = > string . Equals ( i . Id , request . MediaSourceId ? ? request . Id ) ) ;
2014-05-17 06:24:10 +02:00
2014-06-11 16:42:03 +02:00
var subtitleStream = mediaSource . MediaStreams
. First ( i = > i . Type = = MediaStreamType . Subtitle & & i . Index = = request . Index ) ;
2014-05-17 06:24:10 +02:00
2014-06-11 16:42:03 +02:00
return ToStaticFileResult ( subtitleStream . Path ) ;
2014-05-17 06:24:10 +02:00
}
2016-06-05 20:51:18 +02:00
using ( var stream = await GetSubtitles ( request ) . ConfigureAwait ( false ) )
{
using ( var reader = new StreamReader ( stream ) )
{
var text = reader . ReadToEnd ( ) ;
2016-06-05 20:54:21 +02:00
if ( string . Equals ( request . Format , "vtt" , StringComparison . OrdinalIgnoreCase ) & & request . AddVttTimeMap )
2016-06-05 20:51:18 +02:00
{
2016-06-06 23:13:00 +02:00
text = text . Replace ( "WEBVTT" , "WEBVTT\nX-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000" ) ;
2016-06-05 20:51:18 +02:00
}
2014-06-11 16:42:03 +02:00
2016-06-05 20:51:18 +02:00
return ResultFactory . GetResult ( text , MimeTypes . GetMimeType ( "file." + request . Format ) ) ;
}
}
2014-06-11 16:42:03 +02:00
}
2016-06-05 20:51:18 +02:00
private Task < Stream > GetSubtitles ( GetSubtitle request )
2014-06-11 16:42:03 +02:00
{
2016-06-05 20:51:18 +02:00
return _subtitleEncoder . GetSubtitles ( request . Id ,
2014-07-04 04:22:57 +02:00
request . MediaSourceId ,
request . Index ,
2014-06-11 21:31:33 +02:00
request . Format ,
request . StartPositionTicks ,
2014-08-06 03:09:03 +02:00
request . EndPositionTicks ,
2016-05-27 19:17:57 +02:00
request . CopyTimestamps ,
2016-06-05 20:51:18 +02:00
CancellationToken . None ) ;
2014-05-17 06:24:10 +02:00
}
2014-07-04 04:22:57 +02:00
public object Get ( SearchRemoteSubtitles request )
{
var video = ( Video ) _libraryManager . GetItemById ( request . Id ) ;
var response = _subtitleManager . SearchSubtitles ( video , request . Language , CancellationToken . None ) . Result ;
return ToOptimizedResult ( response ) ;
}
2014-05-17 06:24:10 +02:00
public void Delete ( DeleteSubtitle request )
{
var task = _subtitleManager . DeleteSubtitles ( request . Id , request . Index ) ;
Task . WaitAll ( task ) ;
}
public object Get ( GetSubtitleProviders request )
{
var result = _subtitleManager . GetProviders ( request . Id ) ;
return ToOptimizedResult ( result ) ;
}
public object Get ( GetRemoteSubtitles request )
{
var result = _subtitleManager . GetRemoteSubtitles ( request . Id , CancellationToken . None ) . Result ;
2014-12-26 18:45:06 +01:00
return ResultFactory . GetResult ( result . Stream , MimeTypes . GetMimeType ( "file." + result . Format ) ) ;
2014-05-17 06:24:10 +02:00
}
public void Post ( DownloadRemoteSubtitles request )
{
var video = ( Video ) _libraryManager . GetItemById ( request . Id ) ;
Task . Run ( async ( ) = >
{
try
{
await _subtitleManager . DownloadSubtitles ( video , request . SubtitleId , CancellationToken . None )
. ConfigureAwait ( false ) ;
2015-09-13 23:32:02 +02:00
_providerManager . QueueRefresh ( video . Id , new MetadataRefreshOptions ( _fileSystem ) ) ;
2014-05-17 06:24:10 +02:00
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error downloading subtitles" , ex ) ;
}
} ) ;
}
}
}