2020-06-11 06:29:56 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-06-10 12:20:54 -04:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-06-19 10:49:42 -04:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-06-10 12:20:54 -04:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-06-11 06:29:56 -04:00
|
|
|
using System.Net.Mime;
|
2020-06-10 12:20:54 -04:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-09-01 19:26:49 -04:00
|
|
|
using Jellyfin.Api.Attributes;
|
2020-06-10 12:20:54 -04:00
|
|
|
using Jellyfin.Api.Constants;
|
2020-09-11 18:08:49 -04:00
|
|
|
using Jellyfin.Api.Models.SubtitleDtos;
|
2020-08-19 05:30:22 -04:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2020-06-10 12:20:54 -04:00
|
|
|
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;
|
2020-08-19 05:30:22 -04:00
|
|
|
using MediaBrowser.Model.Subtitles;
|
2020-06-10 12:20:54 -04:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Subtitle controller.
|
|
|
|
/// </summary>
|
2020-08-04 14:48:53 -04:00
|
|
|
[Route("")]
|
2020-06-10 12:20:54 -04:00
|
|
|
public class SubtitleController : BaseJellyfinApiController
|
|
|
|
{
|
2020-08-19 05:30:22 -04:00
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
2020-06-10 12:20:54 -04:00
|
|
|
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>
|
2020-08-19 05:30:22 -04:00
|
|
|
/// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> interface.</param>
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <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(
|
2020-08-19 05:30:22 -04:00
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
2020-06-10 12:20:54 -04:00
|
|
|
ILibraryManager libraryManager,
|
|
|
|
ISubtitleManager subtitleManager,
|
|
|
|
ISubtitleEncoder subtitleEncoder,
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
IProviderManager providerManager,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IAuthorizationContext authContext,
|
|
|
|
ILogger<SubtitleController> logger)
|
|
|
|
{
|
2020-08-19 05:30:22 -04:00
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
2020-06-10 12:20:54 -04:00
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_subtitleManager = subtitleManager;
|
|
|
|
_subtitleEncoder = subtitleEncoder;
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
|
|
|
_providerManager = providerManager;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_authContext = authContext;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deletes an external subtitle file.
|
|
|
|
/// </summary>
|
2020-06-20 20:02:07 -04:00
|
|
|
/// <param name="itemId">The item id.</param>
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <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>
|
2020-08-04 14:48:53 -04:00
|
|
|
[HttpDelete("Videos/{itemId}/Subtitles/{index}")]
|
2020-06-10 12:20:54 -04:00
|
|
|
[Authorize(Policy = Policies.RequiresElevation)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
public ActionResult<Task> DeleteSubtitle(
|
2020-09-06 11:07:27 -04:00
|
|
|
[FromRoute, Required] Guid itemId,
|
|
|
|
[FromRoute, Required] int index)
|
2020-06-10 12:20:54 -04:00
|
|
|
{
|
2020-06-20 20:02:07 -04:00
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
2020-06-10 12:20:54 -04:00
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
_subtitleManager.DeleteSubtitles(item, index);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Search remote subtitles.
|
|
|
|
/// </summary>
|
2020-06-20 20:02:07 -04:00
|
|
|
/// <param name="itemId">The item id.</param>
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <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>
|
2020-08-04 14:48:53 -04:00
|
|
|
[HttpGet("Items/{itemId}/RemoteSearch/Subtitles/{language}")]
|
2020-06-16 16:15:58 -04:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-06-10 12:20:54 -04:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-06-11 06:29:56 -04:00
|
|
|
public async Task<ActionResult<IEnumerable<RemoteSubtitleInfo>>> SearchRemoteSubtitles(
|
2020-09-06 11:07:27 -04:00
|
|
|
[FromRoute, Required] Guid itemId,
|
2020-09-07 20:45:06 -04:00
|
|
|
[FromRoute, Required] string language,
|
2020-06-11 06:29:56 -04:00
|
|
|
[FromQuery] bool? isPerfectMatch)
|
2020-06-10 12:20:54 -04:00
|
|
|
{
|
2020-06-20 20:02:07 -04:00
|
|
|
var video = (Video)_libraryManager.GetItemById(itemId);
|
2020-06-10 12:20:54 -04:00
|
|
|
|
|
|
|
return await _subtitleManager.SearchSubtitles(video, language, isPerfectMatch, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Downloads a remote subtitle.
|
|
|
|
/// </summary>
|
2020-06-20 20:02:07 -04:00
|
|
|
/// <param name="itemId">The item id.</param>
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <param name="subtitleId">The subtitle id.</param>
|
|
|
|
/// <response code="204">Subtitle downloaded.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/>.</returns>
|
2020-08-04 14:48:53 -04:00
|
|
|
[HttpPost("Items/{itemId}/RemoteSearch/Subtitles/{subtitleId}")]
|
2020-06-16 16:15:58 -04:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-06-10 12:20:54 -04:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-06-11 06:29:56 -04:00
|
|
|
public async Task<ActionResult> DownloadRemoteSubtitles(
|
2020-09-06 11:07:27 -04:00
|
|
|
[FromRoute, Required] Guid itemId,
|
2020-09-07 20:45:06 -04:00
|
|
|
[FromRoute, Required] string subtitleId)
|
2020-06-10 12:20:54 -04:00
|
|
|
{
|
2020-06-20 20:02:07 -04:00
|
|
|
var video = (Video)_libraryManager.GetItemById(itemId);
|
2020-06-10 12:20:54 -04:00
|
|
|
|
2020-06-11 06:29:56 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await _subtitleManager.DownloadSubtitles(video, subtitleId, CancellationToken.None)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
_providerManager.QueueRefresh(video.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2020-06-10 12:20:54 -04:00
|
|
|
{
|
2020-06-11 06:29:56 -04:00
|
|
|
_logger.LogError(ex, "Error downloading subtitles");
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:20:54 -04:00
|
|
|
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>
|
2020-08-04 14:48:53 -04:00
|
|
|
[HttpGet("Providers/Subtitles/Subtitles/{id}")]
|
2020-06-16 16:15:58 -04:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-06-10 12:20:54 -04:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-06-11 06:29:56 -04:00
|
|
|
[Produces(MediaTypeNames.Application.Octet)]
|
2020-09-01 19:26:49 -04:00
|
|
|
[ProducesFile("text/*")]
|
2020-09-07 20:45:06 -04:00
|
|
|
public async Task<ActionResult> GetRemoteSubtitles([FromRoute, Required] string id)
|
2020-06-10 12:20:54 -04:00
|
|
|
{
|
|
|
|
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>
|
2021-03-26 09:07:45 -04:00
|
|
|
/// <param name="routeItemId">The (route) item id.</param>
|
|
|
|
/// <param name="routeMediaSourceId">The (route) media source id.</param>
|
|
|
|
/// <param name="routeIndex">The (route) subtitle stream index.</param>
|
|
|
|
/// <param name="routeFormat">The (route) format of the returned subtitle.</param>
|
2020-06-20 20:02:07 -04:00
|
|
|
/// <param name="itemId">The item id.</param>
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <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="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>
|
2021-03-26 09:07:45 -04:00
|
|
|
/// <param name="startPositionTicks">The start position of the subtitle in ticks.</param>
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <response code="200">File returned.</response>
|
|
|
|
/// <returns>A <see cref="FileContentResult"/> with the subtitle file.</returns>
|
2021-06-12 09:38:13 -04:00
|
|
|
[HttpGet("Videos/{routeItemId}/{routeMediaSourceId}/Subtitles/{routeIndex}/Stream.{routeFormat}")]
|
2020-06-10 12:20:54 -04:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-09-01 19:26:49 -04:00
|
|
|
[ProducesFile("text/*")]
|
2020-06-10 12:20:54 -04:00
|
|
|
public async Task<ActionResult> GetSubtitle(
|
2021-03-26 09:07:45 -04:00
|
|
|
[FromRoute, Required] Guid routeItemId,
|
|
|
|
[FromRoute, Required] string routeMediaSourceId,
|
|
|
|
[FromRoute, Required] int routeIndex,
|
2021-03-25 20:06:25 -04:00
|
|
|
[FromRoute, Required] string routeFormat,
|
2021-03-26 09:07:45 -04:00
|
|
|
[FromQuery, ParameterObsolete] Guid? itemId,
|
|
|
|
[FromQuery, ParameterObsolete] string? mediaSourceId,
|
|
|
|
[FromQuery, ParameterObsolete] int? index,
|
2021-03-25 22:30:15 -04:00
|
|
|
[FromQuery, ParameterObsolete] string? format,
|
2020-06-10 12:20:54 -04:00
|
|
|
[FromQuery] long? endPositionTicks,
|
2020-07-07 11:10:51 -04:00
|
|
|
[FromQuery] bool copyTimestamps = false,
|
|
|
|
[FromQuery] bool addVttTimeMap = false,
|
2020-11-15 12:58:39 -05:00
|
|
|
[FromQuery] long startPositionTicks = 0)
|
2020-06-10 12:20:54 -04:00
|
|
|
{
|
2021-03-26 09:07:45 -04:00
|
|
|
// Set parameters to route value if not provided via query.
|
|
|
|
itemId ??= routeItemId;
|
|
|
|
mediaSourceId ??= routeMediaSourceId;
|
|
|
|
index ??= routeIndex;
|
|
|
|
format ??= routeFormat;
|
2021-03-25 20:06:25 -04:00
|
|
|
|
2020-06-10 12:20:54 -04:00
|
|
|
if (string.Equals(format, "js", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
format = "json";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(format))
|
|
|
|
{
|
2021-03-26 09:07:45 -04:00
|
|
|
var item = (Video)_libraryManager.GetItemById(itemId.Value);
|
2020-06-10 12:20:54 -04:00
|
|
|
|
2021-03-26 09:07:45 -04:00
|
|
|
var idString = itemId.Value.ToString("N", CultureInfo.InvariantCulture);
|
2020-06-10 12:20:54 -04:00
|
|
|
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);
|
|
|
|
|
2020-09-01 21:27:57 -04:00
|
|
|
return PhysicalFile(subtitleStream.Path, MimeTypes.GetMimeType(subtitleStream.Path));
|
2020-06-10 12:20:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (string.Equals(format, "vtt", StringComparison.OrdinalIgnoreCase) && addVttTimeMap)
|
|
|
|
{
|
2021-03-26 09:07:45 -04:00
|
|
|
await using Stream stream = await EncodeSubtitles(itemId.Value, mediaSourceId, index.Value, format, startPositionTicks, endPositionTicks, copyTimestamps).ConfigureAwait(false);
|
2020-06-10 12:20:54 -04:00
|
|
|
using var reader = new StreamReader(stream);
|
|
|
|
|
2020-06-11 06:29:56 -04:00
|
|
|
var text = await reader.ReadToEndAsync().ConfigureAwait(false);
|
2020-06-10 12:20:54 -04:00
|
|
|
|
|
|
|
text = text.Replace("WEBVTT", "WEBVTT\nX-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000", StringComparison.Ordinal);
|
|
|
|
|
2020-06-11 06:29:56 -04:00
|
|
|
return File(Encoding.UTF8.GetBytes(text), MimeTypes.GetMimeType("file." + format));
|
2020-06-10 12:20:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return File(
|
|
|
|
await EncodeSubtitles(
|
2021-03-26 09:07:45 -04:00
|
|
|
itemId.Value,
|
2020-06-10 12:20:54 -04:00
|
|
|
mediaSourceId,
|
2021-03-26 09:07:45 -04:00
|
|
|
index.Value,
|
2020-06-10 12:20:54 -04:00
|
|
|
format,
|
|
|
|
startPositionTicks,
|
|
|
|
endPositionTicks,
|
|
|
|
copyTimestamps).ConfigureAwait(false),
|
|
|
|
MimeTypes.GetMimeType("file." + format));
|
|
|
|
}
|
|
|
|
|
2020-11-15 12:58:39 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets subtitles in a specified format.
|
|
|
|
/// </summary>
|
2021-03-26 09:07:45 -04:00
|
|
|
/// <param name="routeItemId">The (route) item id.</param>
|
|
|
|
/// <param name="routeMediaSourceId">The (route) media source id.</param>
|
|
|
|
/// <param name="routeIndex">The (route) subtitle stream index.</param>
|
|
|
|
/// <param name="routeStartPositionTicks">The (route) start position of the subtitle in ticks.</param>
|
|
|
|
/// <param name="routeFormat">The (route) format of the returned subtitle.</param>
|
2020-11-15 12:58:39 -05:00
|
|
|
/// <param name="itemId">The item id.</param>
|
|
|
|
/// <param name="mediaSourceId">The media source id.</param>
|
|
|
|
/// <param name="index">The subtitle stream index.</param>
|
2021-03-26 09:07:45 -04:00
|
|
|
/// <param name="startPositionTicks">The start position of the subtitle in ticks.</param>
|
2020-11-15 12:58:39 -05:00
|
|
|
/// <param name="format">The format of the returned subtitle.</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>
|
2021-03-26 09:07:45 -04:00
|
|
|
[HttpGet("Videos/{routeItemId}/{routeMediaSourceId}/Subtitles/{routeIndex}/{routeStartPositionTicks}/Stream.{routeFormat}")]
|
2020-11-15 12:58:39 -05:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesFile("text/*")]
|
|
|
|
public Task<ActionResult> GetSubtitleWithTicks(
|
2021-03-26 09:07:45 -04:00
|
|
|
[FromRoute, Required] Guid routeItemId,
|
|
|
|
[FromRoute, Required] string routeMediaSourceId,
|
|
|
|
[FromRoute, Required] int routeIndex,
|
|
|
|
[FromRoute, Required] long routeStartPositionTicks,
|
2021-03-25 20:06:25 -04:00
|
|
|
[FromRoute, Required] string routeFormat,
|
2021-03-26 09:07:45 -04:00
|
|
|
[FromQuery, ParameterObsolete] Guid? itemId,
|
|
|
|
[FromQuery, ParameterObsolete] string? mediaSourceId,
|
|
|
|
[FromQuery, ParameterObsolete] int? index,
|
|
|
|
[FromQuery, ParameterObsolete] long? startPositionTicks,
|
2021-03-25 22:30:15 -04:00
|
|
|
[FromQuery, ParameterObsolete] string? format,
|
2020-11-15 12:58:39 -05:00
|
|
|
[FromQuery] long? endPositionTicks,
|
|
|
|
[FromQuery] bool copyTimestamps = false,
|
|
|
|
[FromQuery] bool addVttTimeMap = false)
|
|
|
|
{
|
|
|
|
return GetSubtitle(
|
2021-03-26 09:07:45 -04:00
|
|
|
routeItemId,
|
|
|
|
routeMediaSourceId,
|
|
|
|
routeIndex,
|
|
|
|
routeFormat,
|
2020-11-15 12:58:39 -05:00
|
|
|
itemId,
|
|
|
|
mediaSourceId,
|
|
|
|
index,
|
|
|
|
format,
|
|
|
|
endPositionTicks,
|
|
|
|
copyTimestamps,
|
|
|
|
addVttTimeMap,
|
2021-03-26 09:07:45 -04:00
|
|
|
startPositionTicks ?? routeStartPositionTicks);
|
2020-11-15 12:58:39 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Gets an HLS subtitle playlist.
|
|
|
|
/// </summary>
|
2020-06-20 20:02:07 -04:00
|
|
|
/// <param name="itemId">The item id.</param>
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <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>
|
2020-08-04 14:48:53 -04:00
|
|
|
[HttpGet("Videos/{itemId}/{mediaSourceId}/Subtitles/{index}/subtitles.m3u8")]
|
2020-06-16 16:15:58 -04:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-06-10 12:20:54 -04:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-09-01 19:26:49 -04:00
|
|
|
[ProducesPlaylistFile]
|
2020-06-19 10:49:42 -04:00
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "index", Justification = "Imported from ServiceStack")]
|
2020-06-10 12:20:54 -04:00
|
|
|
public async Task<ActionResult> GetSubtitlePlaylist(
|
2020-09-06 11:07:27 -04:00
|
|
|
[FromRoute, Required] Guid itemId,
|
|
|
|
[FromRoute, Required] int index,
|
2020-09-07 20:45:06 -04:00
|
|
|
[FromRoute, Required] string mediaSourceId,
|
2020-06-10 12:20:54 -04:00
|
|
|
[FromQuery, Required] int segmentLength)
|
|
|
|
{
|
2020-06-20 20:02:07 -04:00
|
|
|
var item = (Video)_libraryManager.GetItemById(itemId);
|
2020-06-10 12:20:54 -04:00
|
|
|
|
|
|
|
var mediaSource = await _mediaSourceManager.GetMediaSource(item, mediaSourceId, null, false, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
|
|
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)");
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:30:45 -04:00
|
|
|
var builder = new StringBuilder();
|
2020-08-03 14:01:24 -04:00
|
|
|
builder.AppendLine("#EXTM3U")
|
|
|
|
.Append("#EXT-X-TARGETDURATION:")
|
2020-09-16 08:16:44 -04:00
|
|
|
.Append(segmentLength)
|
|
|
|
.AppendLine()
|
2020-08-03 14:01:24 -04:00
|
|
|
.AppendLine("#EXT-X-VERSION:3")
|
|
|
|
.AppendLine("#EXT-X-MEDIA-SEQUENCE:0")
|
|
|
|
.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD");
|
2020-06-10 12:20:54 -04:00
|
|
|
|
|
|
|
long positionTicks = 0;
|
|
|
|
|
|
|
|
var accessToken = _authContext.GetAuthorizationInfo(Request).Token;
|
|
|
|
|
|
|
|
while (positionTicks < runtime)
|
|
|
|
{
|
|
|
|
var remaining = runtime - positionTicks;
|
|
|
|
var lengthTicks = Math.Min(remaining, segmentLengthTicks);
|
|
|
|
|
2020-08-03 14:01:24 -04:00
|
|
|
builder.Append("#EXTINF:")
|
2020-09-16 08:16:44 -04:00
|
|
|
.Append(TimeSpan.FromTicks(lengthTicks).TotalSeconds)
|
|
|
|
.Append(',')
|
|
|
|
.AppendLine();
|
2020-06-10 12:20:54 -04:00
|
|
|
|
|
|
|
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");
|
2020-06-11 06:29:56 -04:00
|
|
|
return File(Encoding.UTF8.GetBytes(builder.ToString()), MimeTypes.GetMimeType("playlist.m3u8"));
|
2020-06-10 12:20:54 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:08:49 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Upload an external subtitle file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="itemId">The item the subtitle belongs to.</param>
|
|
|
|
/// <param name="body">The request body.</param>
|
|
|
|
/// <response code="204">Subtitle uploaded.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/>.</returns>
|
|
|
|
[HttpPost("Videos/{itemId}/Subtitles")]
|
2021-01-10 12:48:37 -05:00
|
|
|
[Authorize(Policy = Policies.RequiresElevation)]
|
2020-11-15 12:58:39 -05:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-09-11 18:08:49 -04:00
|
|
|
public async Task<ActionResult> UploadSubtitle(
|
|
|
|
[FromRoute, Required] Guid itemId,
|
|
|
|
[FromBody, Required] UploadSubtitleDto body)
|
|
|
|
{
|
|
|
|
var video = (Video)_libraryManager.GetItemById(itemId);
|
|
|
|
var data = Convert.FromBase64String(body.Data);
|
|
|
|
await using var memoryStream = new MemoryStream(data);
|
|
|
|
await _subtitleManager.UploadSubtitle(
|
|
|
|
video,
|
|
|
|
new SubtitleResponse
|
|
|
|
{
|
|
|
|
Format = body.Format,
|
|
|
|
Language = body.Language,
|
|
|
|
IsForced = body.IsForced,
|
|
|
|
Stream = memoryStream
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:20:54 -04:00
|
|
|
/// <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,
|
2020-06-27 12:50:44 -04:00
|
|
|
string? mediaSourceId,
|
2020-06-10 12:20:54 -04:00
|
|
|
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);
|
|
|
|
}
|
2020-08-19 05:30:22 -04:00
|
|
|
|
2020-08-19 12:04:55 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of available fallback font files.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="200">Information retrieved.</response>
|
|
|
|
/// <returns>An array of <see cref="FontFile"/> with the available font files.</returns>
|
|
|
|
[HttpGet("FallbackFont/Fonts")]
|
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public IEnumerable<FontFile> GetFallbackFontList()
|
2020-08-19 05:30:22 -04:00
|
|
|
{
|
2020-08-19 12:04:55 -04:00
|
|
|
var encodingOptions = _serverConfigurationManager.GetEncodingOptions();
|
2020-08-19 05:30:22 -04:00
|
|
|
var fallbackFontPath = encodingOptions.FallbackFontPath;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(fallbackFontPath))
|
|
|
|
{
|
2020-08-19 12:04:55 -04:00
|
|
|
var files = _fileSystem.GetFiles(fallbackFontPath, new[] { ".woff", ".woff2", ".ttf", ".otf" }, false, false);
|
|
|
|
var fontFiles = files
|
|
|
|
.Select(i => new FontFile
|
2020-08-19 05:30:22 -04:00
|
|
|
{
|
|
|
|
Name = i.Name,
|
|
|
|
Size = i.Length,
|
|
|
|
DateCreated = _fileSystem.GetCreationTimeUtc(i),
|
|
|
|
DateModified = _fileSystem.GetLastWriteTimeUtc(i)
|
2020-08-19 12:04:55 -04:00
|
|
|
})
|
|
|
|
.OrderBy(i => i.Size)
|
|
|
|
.ThenBy(i => i.Name)
|
|
|
|
.ThenByDescending(i => i.DateModified)
|
|
|
|
.ThenByDescending(i => i.DateCreated);
|
|
|
|
// max total size 20M
|
|
|
|
const int MaxSize = 20971520;
|
|
|
|
var sizeCounter = 0L;
|
|
|
|
foreach (var fontFile in fontFiles)
|
|
|
|
{
|
|
|
|
sizeCounter += fontFile.Size;
|
|
|
|
if (sizeCounter >= MaxSize)
|
2020-08-19 05:30:22 -04:00
|
|
|
{
|
2020-08-19 12:04:55 -04:00
|
|
|
_logger.LogWarning("Some fonts will not be sent due to size limitations");
|
|
|
|
yield break;
|
2020-08-19 05:30:22 -04:00
|
|
|
}
|
2020-08-20 03:17:42 -04:00
|
|
|
|
2020-08-19 12:04:55 -04:00
|
|
|
yield return fontFile;
|
2020-08-19 05:30:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogWarning("The path of fallback font folder has not been set");
|
|
|
|
encodingOptions.EnableFallbackFont = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a fallback font file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name of the fallback font file to get.</param>
|
|
|
|
/// <response code="200">Fallback font file retrieved.</response>
|
|
|
|
/// <returns>The fallback font file.</returns>
|
2020-08-19 12:04:55 -04:00
|
|
|
[HttpGet("FallbackFont/Fonts/{name}")]
|
2020-08-19 05:30:22 -04:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-11-15 12:58:39 -05:00
|
|
|
[ProducesFile("font/*")]
|
2020-08-19 12:04:55 -04:00
|
|
|
public ActionResult GetFallbackFont([FromRoute, Required] string name)
|
2020-08-19 05:30:22 -04:00
|
|
|
{
|
2020-08-19 12:04:55 -04:00
|
|
|
var encodingOptions = _serverConfigurationManager.GetEncodingOptions();
|
2020-08-19 05:30:22 -04:00
|
|
|
var fallbackFontPath = encodingOptions.FallbackFontPath;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(fallbackFontPath))
|
|
|
|
{
|
2020-08-19 12:08:44 -04:00
|
|
|
var fontFile = _fileSystem.GetFiles(fallbackFontPath)
|
|
|
|
.First(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
var fileSize = fontFile?.Length;
|
2020-08-19 05:30:22 -04:00
|
|
|
|
2020-08-19 12:08:44 -04:00
|
|
|
if (fontFile != null && fileSize != null && fileSize > 0)
|
|
|
|
{
|
2020-11-08 06:33:40 -05:00
|
|
|
_logger.LogDebug("Fallback font size is {fileSize} Bytes", fileSize);
|
2020-08-20 10:53:36 -04:00
|
|
|
return PhysicalFile(fontFile.FullName, MimeTypes.GetMimeType(fontFile.FullName));
|
2020-08-19 05:30:22 -04:00
|
|
|
}
|
2020-08-19 12:08:44 -04:00
|
|
|
else
|
2020-08-19 05:30:22 -04:00
|
|
|
{
|
2020-08-19 12:08:44 -04:00
|
|
|
_logger.LogWarning("The selected font is null or empty");
|
2020-08-19 05:30:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogWarning("The path of fallback font folder has not been set");
|
|
|
|
encodingOptions.EnableFallbackFont = false;
|
|
|
|
}
|
|
|
|
|
2020-08-19 12:08:44 -04:00
|
|
|
// returning HTTP 204 will break the SubtitlesOctopus
|
|
|
|
return Ok();
|
2020-08-19 05:30:22 -04:00
|
|
|
}
|
2020-06-10 12:20:54 -04:00
|
|
|
}
|
|
|
|
}
|