mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 22:07:13 -04:00
Merge pull request #3288 from Ullmie02/api-subtitles
Move SubtitleService to Jellyfin.Api
This commit is contained in:
commit
293187480f
349
Jellyfin.Api/Controllers/SubtitleController.cs
Normal file
349
Jellyfin.Api/Controllers/SubtitleController.cs
Normal file
@ -0,0 +1,349 @@
|
|||||||
|
#nullable enable
|
||||||
|
#pragma warning disable CA1801
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Mime;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Jellyfin.Api.Constants;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
|
using MediaBrowser.Controller.Library;
|
||||||
|
using MediaBrowser.Controller.MediaEncoding;
|
||||||
|
using MediaBrowser.Controller.Net;
|
||||||
|
using MediaBrowser.Controller.Providers;
|
||||||
|
using MediaBrowser.Controller.Subtitles;
|
||||||
|
using MediaBrowser.Model.Entities;
|
||||||
|
using MediaBrowser.Model.IO;
|
||||||
|
using MediaBrowser.Model.Net;
|
||||||
|
using MediaBrowser.Model.Providers;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Jellyfin.Api.Controllers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Subtitle controller.
|
||||||
|
/// </summary>
|
||||||
|
public class SubtitleController : BaseJellyfinApiController
|
||||||
|
{
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
private readonly ISubtitleManager _subtitleManager;
|
||||||
|
private readonly ISubtitleEncoder _subtitleEncoder;
|
||||||
|
private readonly IMediaSourceManager _mediaSourceManager;
|
||||||
|
private readonly IProviderManager _providerManager;
|
||||||
|
private readonly IFileSystem _fileSystem;
|
||||||
|
private readonly IAuthorizationContext _authContext;
|
||||||
|
private readonly ILogger<SubtitleController> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="SubtitleController"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param>
|
||||||
|
/// <param name="subtitleManager">Instance of <see cref="ISubtitleManager"/> interface.</param>
|
||||||
|
/// <param name="subtitleEncoder">Instance of <see cref="ISubtitleEncoder"/> interface.</param>
|
||||||
|
/// <param name="mediaSourceManager">Instance of <see cref="IMediaSourceManager"/> interface.</param>
|
||||||
|
/// <param name="providerManager">Instance of <see cref="IProviderManager"/> interface.</param>
|
||||||
|
/// <param name="fileSystem">Instance of <see cref="IFileSystem"/> interface.</param>
|
||||||
|
/// <param name="authContext">Instance of <see cref="IAuthorizationContext"/> interface.</param>
|
||||||
|
/// <param name="logger">Instance of <see cref="ILogger{SubtitleController}"/> interface.</param>
|
||||||
|
public SubtitleController(
|
||||||
|
ILibraryManager libraryManager,
|
||||||
|
ISubtitleManager subtitleManager,
|
||||||
|
ISubtitleEncoder subtitleEncoder,
|
||||||
|
IMediaSourceManager mediaSourceManager,
|
||||||
|
IProviderManager providerManager,
|
||||||
|
IFileSystem fileSystem,
|
||||||
|
IAuthorizationContext authContext,
|
||||||
|
ILogger<SubtitleController> logger)
|
||||||
|
{
|
||||||
|
_libraryManager = libraryManager;
|
||||||
|
_subtitleManager = subtitleManager;
|
||||||
|
_subtitleEncoder = subtitleEncoder;
|
||||||
|
_mediaSourceManager = mediaSourceManager;
|
||||||
|
_providerManager = providerManager;
|
||||||
|
_fileSystem = fileSystem;
|
||||||
|
_authContext = authContext;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes an external subtitle file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The item id.</param>
|
||||||
|
/// <param name="index">The index of the subtitle file.</param>
|
||||||
|
/// <response code="204">Subtitle deleted.</response>
|
||||||
|
/// <response code="404">Item not found.</response>
|
||||||
|
/// <returns>A <see cref="NoContentResult"/>.</returns>
|
||||||
|
[HttpDelete("/Videos/{id}/Subtitles/{index}")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public ActionResult<Task> DeleteSubtitle(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromRoute] int index)
|
||||||
|
{
|
||||||
|
var item = _libraryManager.GetItemById(id);
|
||||||
|
|
||||||
|
if (item == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
_subtitleManager.DeleteSubtitles(item, index);
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Search remote subtitles.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The item id.</param>
|
||||||
|
/// <param name="language">The language of the subtitles.</param>
|
||||||
|
/// <param name="isPerfectMatch">Optional. Only show subtitles which are a perfect match.</param>
|
||||||
|
/// <response code="200">Subtitles retrieved.</response>
|
||||||
|
/// <returns>An array of <see cref="RemoteSubtitleInfo"/>.</returns>
|
||||||
|
[HttpGet("/Items/{id}/RemoteSearch/Subtitles/{language}")]
|
||||||
|
[Authorize]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public async Task<ActionResult<IEnumerable<RemoteSubtitleInfo>>> SearchRemoteSubtitles(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromRoute] string language,
|
||||||
|
[FromQuery] bool? isPerfectMatch)
|
||||||
|
{
|
||||||
|
var video = (Video)_libraryManager.GetItemById(id);
|
||||||
|
|
||||||
|
return await _subtitleManager.SearchSubtitles(video, language, isPerfectMatch, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Downloads a remote subtitle.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The item id.</param>
|
||||||
|
/// <param name="subtitleId">The subtitle id.</param>
|
||||||
|
/// <response code="204">Subtitle downloaded.</response>
|
||||||
|
/// <returns>A <see cref="NoContentResult"/>.</returns>
|
||||||
|
[HttpPost("/Items/{id}/RemoteSearch/Subtitles/{subtitleId}")]
|
||||||
|
[Authorize]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
public async Task<ActionResult> DownloadRemoteSubtitles(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromRoute] string subtitleId)
|
||||||
|
{
|
||||||
|
var video = (Video)_libraryManager.GetItemById(id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _subtitleManager.DownloadSubtitles(video, subtitleId, CancellationToken.None)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
_providerManager.QueueRefresh(video.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error downloading subtitles");
|
||||||
|
}
|
||||||
|
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the remote subtitles.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The item id.</param>
|
||||||
|
/// <response code="200">File returned.</response>
|
||||||
|
/// <returns>A <see cref="FileStreamResult"/> with the subtitle file.</returns>
|
||||||
|
[HttpGet("/Providers/Subtitles/Subtitles/{id}")]
|
||||||
|
[Authorize]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[Produces(MediaTypeNames.Application.Octet)]
|
||||||
|
public async Task<ActionResult> GetRemoteSubtitles([FromRoute] string id)
|
||||||
|
{
|
||||||
|
var result = await _subtitleManager.GetRemoteSubtitles(id, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
|
||||||
|
return File(result.Stream, MimeTypes.GetMimeType("file." + result.Format));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets subtitles in a specified format.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The item id.</param>
|
||||||
|
/// <param name="mediaSourceId">The media source id.</param>
|
||||||
|
/// <param name="index">The subtitle stream index.</param>
|
||||||
|
/// <param name="format">The format of the returned subtitle.</param>
|
||||||
|
/// <param name="startPositionTicks">Optional. The start position of the subtitle in ticks.</param>
|
||||||
|
/// <param name="endPositionTicks">Optional. The end position of the subtitle in ticks.</param>
|
||||||
|
/// <param name="copyTimestamps">Optional. Whether to copy the timestamps.</param>
|
||||||
|
/// <param name="addVttTimeMap">Optional. Whether to add a VTT time map.</param>
|
||||||
|
/// <response code="200">File returned.</response>
|
||||||
|
/// <returns>A <see cref="FileContentResult"/> with the subtitle file.</returns>
|
||||||
|
[HttpGet("/Videos/{id}/{mediaSourceId}/Subtitles/{index}/Stream.{format}")]
|
||||||
|
[HttpGet("/Videos/{id}/{mediaSourceId}/Subtitles/{index}/{startPositionTicks}/Stream.{format}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public async Task<ActionResult> GetSubtitle(
|
||||||
|
[FromRoute, Required] Guid id,
|
||||||
|
[FromRoute, Required] string mediaSourceId,
|
||||||
|
[FromRoute, Required] int index,
|
||||||
|
[FromRoute, Required] string format,
|
||||||
|
[FromRoute] long startPositionTicks,
|
||||||
|
[FromQuery] long? endPositionTicks,
|
||||||
|
[FromQuery] bool copyTimestamps,
|
||||||
|
[FromQuery] bool addVttTimeMap)
|
||||||
|
{
|
||||||
|
if (string.Equals(format, "js", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
format = "json";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(format))
|
||||||
|
{
|
||||||
|
var item = (Video)_libraryManager.GetItemById(id);
|
||||||
|
|
||||||
|
var idString = id.ToString("N", CultureInfo.InvariantCulture);
|
||||||
|
var mediaSource = _mediaSourceManager.GetStaticMediaSources(item, false)
|
||||||
|
.First(i => string.Equals(i.Id, mediaSourceId ?? idString, StringComparison.Ordinal));
|
||||||
|
|
||||||
|
var subtitleStream = mediaSource.MediaStreams
|
||||||
|
.First(i => i.Type == MediaStreamType.Subtitle && i.Index == index);
|
||||||
|
|
||||||
|
FileStream stream = new FileStream(subtitleStream.Path, FileMode.Open, FileAccess.Read);
|
||||||
|
return File(stream, MimeTypes.GetMimeType(subtitleStream.Path));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.Equals(format, "vtt", StringComparison.OrdinalIgnoreCase) && addVttTimeMap)
|
||||||
|
{
|
||||||
|
await using Stream stream = await EncodeSubtitles(id, mediaSourceId, index, format, startPositionTicks, endPositionTicks, copyTimestamps).ConfigureAwait(false);
|
||||||
|
using var reader = new StreamReader(stream);
|
||||||
|
|
||||||
|
var text = await reader.ReadToEndAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
|
text = text.Replace("WEBVTT", "WEBVTT\nX-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000", StringComparison.Ordinal);
|
||||||
|
|
||||||
|
return File(Encoding.UTF8.GetBytes(text), MimeTypes.GetMimeType("file." + format));
|
||||||
|
}
|
||||||
|
|
||||||
|
return File(
|
||||||
|
await EncodeSubtitles(
|
||||||
|
id,
|
||||||
|
mediaSourceId,
|
||||||
|
index,
|
||||||
|
format,
|
||||||
|
startPositionTicks,
|
||||||
|
endPositionTicks,
|
||||||
|
copyTimestamps).ConfigureAwait(false),
|
||||||
|
MimeTypes.GetMimeType("file." + format));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an HLS subtitle playlist.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The item id.</param>
|
||||||
|
/// <param name="index">The subtitle stream index.</param>
|
||||||
|
/// <param name="mediaSourceId">The media source id.</param>
|
||||||
|
/// <param name="segmentLength">The subtitle segment length.</param>
|
||||||
|
/// <response code="200">Subtitle playlist retrieved.</response>
|
||||||
|
/// <returns>A <see cref="FileContentResult"/> with the HLS subtitle playlist.</returns>
|
||||||
|
[HttpGet("/Videos/{id}/{mediaSourceId}/Subtitles/{index}/subtitles.m3u8")]
|
||||||
|
[Authorize]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public async Task<ActionResult> GetSubtitlePlaylist(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
// TODO: 'int index' is never used: CA1801 is disabled
|
||||||
|
[FromRoute] int index,
|
||||||
|
[FromRoute] string mediaSourceId,
|
||||||
|
[FromQuery, Required] int segmentLength)
|
||||||
|
{
|
||||||
|
var item = (Video)_libraryManager.GetItemById(id);
|
||||||
|
|
||||||
|
var mediaSource = await _mediaSourceManager.GetMediaSource(item, mediaSourceId, null, false, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
|
||||||
|
var builder = new StringBuilder();
|
||||||
|
|
||||||
|
var runtime = mediaSource.RunTimeTicks ?? -1;
|
||||||
|
|
||||||
|
if (runtime <= 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("HLS Subtitles are not supported for this media.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var segmentLengthTicks = TimeSpan.FromSeconds(segmentLength).Ticks;
|
||||||
|
if (segmentLengthTicks <= 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("segmentLength was not given, or it was given incorrectly. (It should be bigger than 0)");
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.AppendLine("#EXTM3U");
|
||||||
|
builder.AppendLine("#EXT-X-TARGETDURATION:" + segmentLength.ToString(CultureInfo.InvariantCulture));
|
||||||
|
builder.AppendLine("#EXT-X-VERSION:3");
|
||||||
|
builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
|
||||||
|
builder.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD");
|
||||||
|
|
||||||
|
long positionTicks = 0;
|
||||||
|
|
||||||
|
var accessToken = _authContext.GetAuthorizationInfo(Request).Token;
|
||||||
|
|
||||||
|
while (positionTicks < runtime)
|
||||||
|
{
|
||||||
|
var remaining = runtime - positionTicks;
|
||||||
|
var lengthTicks = Math.Min(remaining, segmentLengthTicks);
|
||||||
|
|
||||||
|
builder.AppendLine("#EXTINF:" + TimeSpan.FromTicks(lengthTicks).TotalSeconds.ToString(CultureInfo.InvariantCulture) + ",");
|
||||||
|
|
||||||
|
var endPositionTicks = Math.Min(runtime, positionTicks + segmentLengthTicks);
|
||||||
|
|
||||||
|
var url = string.Format(
|
||||||
|
CultureInfo.CurrentCulture,
|
||||||
|
"stream.vtt?CopyTimestamps=true&AddVttTimeMap=true&StartPositionTicks={0}&EndPositionTicks={1}&api_key={2}",
|
||||||
|
positionTicks.ToString(CultureInfo.InvariantCulture),
|
||||||
|
endPositionTicks.ToString(CultureInfo.InvariantCulture),
|
||||||
|
accessToken);
|
||||||
|
|
||||||
|
builder.AppendLine(url);
|
||||||
|
|
||||||
|
positionTicks += segmentLengthTicks;
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.AppendLine("#EXT-X-ENDLIST");
|
||||||
|
return File(Encoding.UTF8.GetBytes(builder.ToString()), MimeTypes.GetMimeType("playlist.m3u8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encodes a subtitle in the specified format.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The media id.</param>
|
||||||
|
/// <param name="mediaSourceId">The source media id.</param>
|
||||||
|
/// <param name="index">The subtitle index.</param>
|
||||||
|
/// <param name="format">The format to convert to.</param>
|
||||||
|
/// <param name="startPositionTicks">The start position in ticks.</param>
|
||||||
|
/// <param name="endPositionTicks">The end position in ticks.</param>
|
||||||
|
/// <param name="copyTimestamps">Whether to copy the timestamps.</param>
|
||||||
|
/// <returns>A <see cref="Task{Stream}"/> with the new subtitle file.</returns>
|
||||||
|
private Task<Stream> EncodeSubtitles(
|
||||||
|
Guid id,
|
||||||
|
string mediaSourceId,
|
||||||
|
int index,
|
||||||
|
string format,
|
||||||
|
long startPositionTicks,
|
||||||
|
long? endPositionTicks,
|
||||||
|
bool copyTimestamps)
|
||||||
|
{
|
||||||
|
var item = _libraryManager.GetItemById(id);
|
||||||
|
|
||||||
|
return _subtitleEncoder.GetSubtitles(
|
||||||
|
item,
|
||||||
|
mediaSourceId,
|
||||||
|
index,
|
||||||
|
format,
|
||||||
|
startPositionTicks,
|
||||||
|
endPositionTicks ?? 0,
|
||||||
|
copyTimestamps,
|
||||||
|
CancellationToken.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,300 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using MediaBrowser.Controller.Configuration;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
|
||||||
using MediaBrowser.Controller.Library;
|
|
||||||
using MediaBrowser.Controller.MediaEncoding;
|
|
||||||
using MediaBrowser.Controller.Net;
|
|
||||||
using MediaBrowser.Controller.Providers;
|
|
||||||
using MediaBrowser.Controller.Subtitles;
|
|
||||||
using MediaBrowser.Model.Entities;
|
|
||||||
using MediaBrowser.Model.IO;
|
|
||||||
using MediaBrowser.Model.Providers;
|
|
||||||
using MediaBrowser.Model.Services;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using MimeTypes = MediaBrowser.Model.Net.MimeTypes;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Api.Subtitles
|
|
||||||
{
|
|
||||||
[Route("/Videos/{Id}/Subtitles/{Index}", "DELETE", Summary = "Deletes an external subtitle file")]
|
|
||||||
[Authenticated(Roles = "Admin")]
|
|
||||||
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 Guid 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")]
|
|
||||||
[Authenticated]
|
|
||||||
public class SearchRemoteSubtitles : IReturn<RemoteSubtitleInfo[]>
|
|
||||||
{
|
|
||||||
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
||||||
public Guid Id { get; set; }
|
|
||||||
|
|
||||||
[ApiMember(Name = "Language", Description = "Language", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
||||||
public string Language { get; set; }
|
|
||||||
|
|
||||||
public bool? IsPerfectMatch { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
[Route("/Items/{Id}/RemoteSearch/Subtitles/{SubtitleId}", "POST")]
|
|
||||||
[Authenticated]
|
|
||||||
public class DownloadRemoteSubtitles : IReturnVoid
|
|
||||||
{
|
|
||||||
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
||||||
public Guid 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")]
|
|
||||||
[Authenticated]
|
|
||||||
public class GetRemoteSubtitles : IReturnVoid
|
|
||||||
{
|
|
||||||
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
||||||
public string Id { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
[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.")]
|
|
||||||
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 Guid 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; }
|
|
||||||
|
|
||||||
[ApiMember(Name = "EndPositionTicks", Description = "EndPositionTicks", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
|
|
||||||
public long? EndPositionTicks { get; set; }
|
|
||||||
|
|
||||||
[ApiMember(Name = "CopyTimestamps", Description = "CopyTimestamps", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
|
|
||||||
public bool CopyTimestamps { get; set; }
|
|
||||||
public bool AddVttTimeMap { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
[Route("/Videos/{Id}/{MediaSourceId}/Subtitles/{Index}/subtitles.m3u8", "GET", Summary = "Gets an HLS subtitle playlist.")]
|
|
||||||
[Authenticated]
|
|
||||||
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 = "query", Verb = "GET")]
|
|
||||||
public int SegmentLength { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SubtitleService : BaseApiService
|
|
||||||
{
|
|
||||||
private readonly ILibraryManager _libraryManager;
|
|
||||||
private readonly ISubtitleManager _subtitleManager;
|
|
||||||
private readonly ISubtitleEncoder _subtitleEncoder;
|
|
||||||
private readonly IMediaSourceManager _mediaSourceManager;
|
|
||||||
private readonly IProviderManager _providerManager;
|
|
||||||
private readonly IFileSystem _fileSystem;
|
|
||||||
private readonly IAuthorizationContext _authContext;
|
|
||||||
|
|
||||||
public SubtitleService(
|
|
||||||
ILogger<SubtitleService> logger,
|
|
||||||
IServerConfigurationManager serverConfigurationManager,
|
|
||||||
IHttpResultFactory httpResultFactory,
|
|
||||||
ILibraryManager libraryManager,
|
|
||||||
ISubtitleManager subtitleManager,
|
|
||||||
ISubtitleEncoder subtitleEncoder,
|
|
||||||
IMediaSourceManager mediaSourceManager,
|
|
||||||
IProviderManager providerManager,
|
|
||||||
IFileSystem fileSystem,
|
|
||||||
IAuthorizationContext authContext)
|
|
||||||
: base(logger, serverConfigurationManager, httpResultFactory)
|
|
||||||
{
|
|
||||||
_libraryManager = libraryManager;
|
|
||||||
_subtitleManager = subtitleManager;
|
|
||||||
_subtitleEncoder = subtitleEncoder;
|
|
||||||
_mediaSourceManager = mediaSourceManager;
|
|
||||||
_providerManager = providerManager;
|
|
||||||
_fileSystem = fileSystem;
|
|
||||||
_authContext = authContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<object> Get(GetSubtitlePlaylist request)
|
|
||||||
{
|
|
||||||
var item = (Video)_libraryManager.GetItemById(new Guid(request.Id));
|
|
||||||
|
|
||||||
var mediaSource = await _mediaSourceManager.GetMediaSource(item, request.MediaSourceId, null, false, CancellationToken.None).ConfigureAwait(false);
|
|
||||||
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
|
|
||||||
var runtime = mediaSource.RunTimeTicks ?? -1;
|
|
||||||
|
|
||||||
if (runtime <= 0)
|
|
||||||
{
|
|
||||||
throw new ArgumentException("HLS Subtitles are not supported for this media.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var segmentLengthTicks = TimeSpan.FromSeconds(request.SegmentLength).Ticks;
|
|
||||||
if (segmentLengthTicks <= 0)
|
|
||||||
{
|
|
||||||
throw new ArgumentException("segmentLength was not given, or it was given incorrectly. (It should be bigger than 0)");
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
builder.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD");
|
|
||||||
|
|
||||||
long positionTicks = 0;
|
|
||||||
|
|
||||||
var accessToken = _authContext.GetAuthorizationInfo(Request).Token;
|
|
||||||
|
|
||||||
while (positionTicks < runtime)
|
|
||||||
{
|
|
||||||
var remaining = runtime - positionTicks;
|
|
||||||
var lengthTicks = Math.Min(remaining, segmentLengthTicks);
|
|
||||||
|
|
||||||
builder.AppendLine("#EXTINF:" + TimeSpan.FromTicks(lengthTicks).TotalSeconds.ToString(CultureInfo.InvariantCulture) + ",");
|
|
||||||
|
|
||||||
var endPositionTicks = Math.Min(runtime, positionTicks + segmentLengthTicks);
|
|
||||||
|
|
||||||
var url = string.Format("stream.vtt?CopyTimestamps=true&AddVttTimeMap=true&StartPositionTicks={0}&EndPositionTicks={1}&api_key={2}",
|
|
||||||
positionTicks.ToString(CultureInfo.InvariantCulture),
|
|
||||||
endPositionTicks.ToString(CultureInfo.InvariantCulture),
|
|
||||||
accessToken);
|
|
||||||
|
|
||||||
builder.AppendLine(url);
|
|
||||||
|
|
||||||
positionTicks += segmentLengthTicks;
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.AppendLine("#EXT-X-ENDLIST");
|
|
||||||
|
|
||||||
return ResultFactory.GetResult(Request, builder.ToString(), MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<object> Get(GetSubtitle request)
|
|
||||||
{
|
|
||||||
if (string.Equals(request.Format, "js", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
request.Format = "json";
|
|
||||||
}
|
|
||||||
if (string.IsNullOrEmpty(request.Format))
|
|
||||||
{
|
|
||||||
var item = (Video)_libraryManager.GetItemById(request.Id);
|
|
||||||
|
|
||||||
var idString = request.Id.ToString("N", CultureInfo.InvariantCulture);
|
|
||||||
var mediaSource = _mediaSourceManager.GetStaticMediaSources(item, false, null)
|
|
||||||
.First(i => string.Equals(i.Id, request.MediaSourceId ?? idString));
|
|
||||||
|
|
||||||
var subtitleStream = mediaSource.MediaStreams
|
|
||||||
.First(i => i.Type == MediaStreamType.Subtitle && i.Index == request.Index);
|
|
||||||
|
|
||||||
return await ResultFactory.GetStaticFileResult(Request, subtitleStream.Path).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.Equals(request.Format, "vtt", StringComparison.OrdinalIgnoreCase) && request.AddVttTimeMap)
|
|
||||||
{
|
|
||||||
using var stream = await GetSubtitles(request).ConfigureAwait(false);
|
|
||||||
using var reader = new StreamReader(stream);
|
|
||||||
|
|
||||||
var text = reader.ReadToEnd();
|
|
||||||
|
|
||||||
text = text.Replace("WEBVTT", "WEBVTT\nX-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000");
|
|
||||||
|
|
||||||
return ResultFactory.GetResult(Request, text, MimeTypes.GetMimeType("file." + request.Format));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResultFactory.GetResult(Request, await GetSubtitles(request).ConfigureAwait(false), MimeTypes.GetMimeType("file." + request.Format));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Task<Stream> GetSubtitles(GetSubtitle request)
|
|
||||||
{
|
|
||||||
var item = _libraryManager.GetItemById(request.Id);
|
|
||||||
|
|
||||||
return _subtitleEncoder.GetSubtitles(item,
|
|
||||||
request.MediaSourceId,
|
|
||||||
request.Index,
|
|
||||||
request.Format,
|
|
||||||
request.StartPositionTicks,
|
|
||||||
request.EndPositionTicks ?? 0,
|
|
||||||
request.CopyTimestamps,
|
|
||||||
CancellationToken.None);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<object> Get(SearchRemoteSubtitles request)
|
|
||||||
{
|
|
||||||
var video = (Video)_libraryManager.GetItemById(request.Id);
|
|
||||||
|
|
||||||
return await _subtitleManager.SearchSubtitles(video, request.Language, request.IsPerfectMatch, CancellationToken.None).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task Delete(DeleteSubtitle request)
|
|
||||||
{
|
|
||||||
var item = _libraryManager.GetItemById(request.Id);
|
|
||||||
return _subtitleManager.DeleteSubtitles(item, request.Index);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<object> Get(GetRemoteSubtitles request)
|
|
||||||
{
|
|
||||||
var result = await _subtitleManager.GetRemoteSubtitles(request.Id, CancellationToken.None).ConfigureAwait(false);
|
|
||||||
|
|
||||||
return ResultFactory.GetResult(Request, result.Stream, MimeTypes.GetMimeType("file." + result.Format));
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
_providerManager.QueueRefresh(video.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.LogError(ex, "Error downloading subtitles");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user