SubtitleService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.MediaEncoding;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Controller.Subtitles;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Providers;
  8. using ServiceStack;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Api.Library
  16. {
  17. [Route("/Videos/{Id}/{MediaSourceId}/Subtitles/{Index}/Stream.{Format}", "GET", Summary = "Gets subtitles in a specified format (vtt).")]
  18. public class GetSubtitle
  19. {
  20. /// <summary>
  21. /// Gets or sets the id.
  22. /// </summary>
  23. /// <value>The id.</value>
  24. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  25. public string Id { get; set; }
  26. [ApiMember(Name = "MediaSourceId", Description = "MediaSourceId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  27. public string MediaSourceId { get; set; }
  28. [ApiMember(Name = "Index", Description = "The subtitle stream index", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
  29. public int Index { get; set; }
  30. [ApiMember(Name = "Format", Description = "Format", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  31. public string Format { get; set; }
  32. [ApiMember(Name = "StartPositionTicks", Description = "StartPositionTicks", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  33. public long StartPositionTicks { get; set; }
  34. }
  35. [Route("/Videos/{Id}/Subtitles/{Index}", "DELETE", Summary = "Deletes an external subtitle file")]
  36. public class DeleteSubtitle
  37. {
  38. /// <summary>
  39. /// Gets or sets the id.
  40. /// </summary>
  41. /// <value>The id.</value>
  42. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  43. public string Id { get; set; }
  44. [ApiMember(Name = "Index", Description = "The subtitle stream index", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "DELETE")]
  45. public int Index { get; set; }
  46. }
  47. [Route("/Items/{Id}/RemoteSearch/Subtitles/{Language}", "GET")]
  48. public class SearchRemoteSubtitles : IReturn<List<RemoteSubtitleInfo>>
  49. {
  50. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  51. public string Id { get; set; }
  52. [ApiMember(Name = "Language", Description = "Language", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  53. public string Language { get; set; }
  54. }
  55. [Route("/Items/{Id}/RemoteSearch/Subtitles/Providers", "GET")]
  56. public class GetSubtitleProviders : IReturn<List<SubtitleProviderInfo>>
  57. {
  58. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  59. public string Id { get; set; }
  60. }
  61. [Route("/Items/{Id}/RemoteSearch/Subtitles/{SubtitleId}", "POST")]
  62. public class DownloadRemoteSubtitles : IReturnVoid
  63. {
  64. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  65. public string Id { get; set; }
  66. [ApiMember(Name = "SubtitleId", Description = "SubtitleId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  67. public string SubtitleId { get; set; }
  68. }
  69. [Route("/Providers/Subtitles/Subtitles/{Id}", "GET")]
  70. public class GetRemoteSubtitles : IReturnVoid
  71. {
  72. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  73. public string Id { get; set; }
  74. }
  75. public class SubtitleService : BaseApiService
  76. {
  77. private readonly ILibraryManager _libraryManager;
  78. private readonly ISubtitleManager _subtitleManager;
  79. private readonly ISubtitleEncoder _subtitleEncoder;
  80. public SubtitleService(ILibraryManager libraryManager, ISubtitleManager subtitleManager, ISubtitleEncoder subtitleEncoder)
  81. {
  82. _libraryManager = libraryManager;
  83. _subtitleManager = subtitleManager;
  84. _subtitleEncoder = subtitleEncoder;
  85. }
  86. public object Get(SearchRemoteSubtitles request)
  87. {
  88. var video = (Video)_libraryManager.GetItemById(request.Id);
  89. var response = _subtitleManager.SearchSubtitles(video, request.Language, CancellationToken.None).Result;
  90. return ToOptimizedResult(response);
  91. }
  92. public object Get(GetSubtitle request)
  93. {
  94. if (string.IsNullOrEmpty(request.Format))
  95. {
  96. var item = (Video)_libraryManager.GetItemById(new Guid(request.Id));
  97. var mediaSource = item.GetMediaSources(false)
  98. .First(i => string.Equals(i.Id, request.MediaSourceId ?? request.Id));
  99. var subtitleStream = mediaSource.MediaStreams
  100. .First(i => i.Type == MediaStreamType.Subtitle && i.Index == request.Index);
  101. return ToStaticFileResult(subtitleStream.Path);
  102. }
  103. var stream = GetSubtitles(request).Result;
  104. return ResultFactory.GetResult(stream, Common.Net.MimeTypes.GetMimeType("file." + request.Format));
  105. }
  106. private async Task<Stream> GetSubtitles(GetSubtitle request)
  107. {
  108. return await _subtitleEncoder.GetSubtitles(request.Id,
  109. request.MediaSourceId,
  110. request.Index,
  111. request.Format,
  112. request.StartPositionTicks,
  113. CancellationToken.None).ConfigureAwait(false);
  114. }
  115. public void Delete(DeleteSubtitle request)
  116. {
  117. var task = _subtitleManager.DeleteSubtitles(request.Id, request.Index);
  118. Task.WaitAll(task);
  119. }
  120. public object Get(GetSubtitleProviders request)
  121. {
  122. var result = _subtitleManager.GetProviders(request.Id);
  123. return ToOptimizedResult(result);
  124. }
  125. public object Get(GetRemoteSubtitles request)
  126. {
  127. var result = _subtitleManager.GetRemoteSubtitles(request.Id, CancellationToken.None).Result;
  128. return ResultFactory.GetResult(result.Stream, Common.Net.MimeTypes.GetMimeType("file." + result.Format));
  129. }
  130. public void Post(DownloadRemoteSubtitles request)
  131. {
  132. var video = (Video)_libraryManager.GetItemById(request.Id);
  133. Task.Run(async () =>
  134. {
  135. try
  136. {
  137. await _subtitleManager.DownloadSubtitles(video, request.SubtitleId, CancellationToken.None)
  138. .ConfigureAwait(false);
  139. await video.RefreshMetadata(new MetadataRefreshOptions(), CancellationToken.None).ConfigureAwait(false);
  140. }
  141. catch (Exception ex)
  142. {
  143. Logger.ErrorException("Error downloading subtitles", ex);
  144. }
  145. });
  146. }
  147. }
  148. }