2014-07-04 02:22:57 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-05-17 04:24:10 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2014-07-02 18:34:08 +00:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2014-05-17 04:24:10 +00: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;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2014-07-04 02:22:57 +00:00
|
|
|
|
namespace MediaBrowser.Api.Subtitles
|
2014-05-17 04:24:10 +00:00
|
|
|
|
{
|
|
|
|
|
[Route("/Videos/{Id}/Subtitles/{Index}", "DELETE", Summary = "Deletes an external subtitle file")]
|
2014-07-04 02:22:57 +00:00
|
|
|
|
[Authenticated]
|
2014-05-17 04:24:10 +00: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 02:22:57 +00:00
|
|
|
|
[Authenticated]
|
2014-05-17 04:24:10 +00: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 02:22:57 +00:00
|
|
|
|
[Authenticated]
|
2014-05-17 04:24:10 +00: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 02:22:57 +00:00
|
|
|
|
[Authenticated]
|
2014-05-17 04:24:10 +00: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 02:22:57 +00:00
|
|
|
|
[Authenticated]
|
2014-05-17 04:24:10 +00: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-07-04 02:22:57 +00:00
|
|
|
|
[Route("/Videos/{Id}/{MediaSourceId}/Subtitles/{Index}/Stream.{Format}", "GET", Summary = "Gets subtitles in a specified format (vtt).")]
|
|
|
|
|
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-05-17 04:24:10 +00:00
|
|
|
|
public class SubtitleService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ISubtitleManager _subtitleManager;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
private readonly ISubtitleEncoder _subtitleEncoder;
|
2014-05-17 04:24:10 +00:00
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
public SubtitleService(ILibraryManager libraryManager, ISubtitleManager subtitleManager, ISubtitleEncoder subtitleEncoder)
|
2014-05-17 04:24:10 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_subtitleManager = subtitleManager;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
_subtitleEncoder = subtitleEncoder;
|
2014-05-17 04:24:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetSubtitle request)
|
|
|
|
|
{
|
2014-06-11 14:42:03 +00:00
|
|
|
|
if (string.IsNullOrEmpty(request.Format))
|
2014-05-17 04:24:10 +00:00
|
|
|
|
{
|
2014-06-11 14:42:03 +00:00
|
|
|
|
var item = (Video)_libraryManager.GetItemById(new Guid(request.Id));
|
2014-05-17 04:24:10 +00:00
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
var mediaSource = item.GetMediaSources(false)
|
|
|
|
|
.First(i => string.Equals(i.Id, request.MediaSourceId ?? request.Id));
|
2014-05-17 04:24:10 +00:00
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
var subtitleStream = mediaSource.MediaStreams
|
|
|
|
|
.First(i => i.Type == MediaStreamType.Subtitle && i.Index == request.Index);
|
2014-05-17 04:24:10 +00:00
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
return ToStaticFileResult(subtitleStream.Path);
|
2014-05-17 04:24:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
var stream = GetSubtitles(request).Result;
|
|
|
|
|
|
|
|
|
|
return ResultFactory.GetResult(stream, Common.Net.MimeTypes.GetMimeType("file." + request.Format));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Stream> GetSubtitles(GetSubtitle request)
|
|
|
|
|
{
|
2014-07-04 02:22:57 +00:00
|
|
|
|
return await _subtitleEncoder.GetSubtitles(request.Id,
|
|
|
|
|
request.MediaSourceId,
|
|
|
|
|
request.Index,
|
2014-06-11 19:31:33 +00:00
|
|
|
|
request.Format,
|
|
|
|
|
request.StartPositionTicks,
|
|
|
|
|
CancellationToken.None).ConfigureAwait(false);
|
2014-05-17 04:24:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 02:22:57 +00: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 04:24:10 +00: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-06-11 14:42:03 +00:00
|
|
|
|
return ResultFactory.GetResult(result.Stream, Common.Net.MimeTypes.GetMimeType("file." + result.Format));
|
2014-05-17 04:24:10 +00: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);
|
|
|
|
|
|
|
|
|
|
await video.RefreshMetadata(new MetadataRefreshOptions(), CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error downloading subtitles", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|