mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
add image encoder based on ffmpeg
This commit is contained in:
parent
39ea2adbc5
commit
9705594845
@ -503,14 +503,13 @@ namespace MediaBrowser.Api.Playback
|
|||||||
return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
|
return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Max dimensions were supplied
|
// If Max dimensions were supplied, for width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size
|
||||||
//this makes my brain hurt. For width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size
|
|
||||||
if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
|
if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
|
||||||
{
|
{
|
||||||
var MaxwidthParam = request.MaxWidth.Value.ToString(UsCulture);
|
var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
|
||||||
var MaxheightParam = request.MaxHeight.Value.ToString(UsCulture);
|
var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
|
||||||
|
|
||||||
return string.Format("{4} -vf \"{0}scale=trunc(min(iw\\,{1})/2)*2:trunc(min((iw/dar)\\,{2})/2)*2{3}\"", yadifParam, MaxwidthParam, MaxheightParam, assSubtitleParam, copyTsParam);
|
return string.Format("{4} -vf \"{0}scale=trunc(min(iw\\,{1})/2)*2:trunc(min((iw/dar)\\,{2})/2)*2{3}\"", yadifParam, maxWidthParam, maxHeightParam, assSubtitleParam, copyTsParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
|
var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
|
||||||
|
@ -157,6 +157,7 @@
|
|||||||
<Compile Include="Localization\ILocalizationManager.cs" />
|
<Compile Include="Localization\ILocalizationManager.cs" />
|
||||||
<Compile Include="MediaEncoding\ChapterImageRefreshOptions.cs" />
|
<Compile Include="MediaEncoding\ChapterImageRefreshOptions.cs" />
|
||||||
<Compile Include="MediaEncoding\IEncodingManager.cs" />
|
<Compile Include="MediaEncoding\IEncodingManager.cs" />
|
||||||
|
<Compile Include="MediaEncoding\ImageEncodingOptions.cs" />
|
||||||
<Compile Include="MediaEncoding\IMediaEncoder.cs" />
|
<Compile Include="MediaEncoding\IMediaEncoder.cs" />
|
||||||
<Compile Include="MediaEncoding\InternalMediaInfoResult.cs" />
|
<Compile Include="MediaEncoding\InternalMediaInfoResult.cs" />
|
||||||
<Compile Include="Net\IHasResultFactory.cs" />
|
<Compile Include="Net\IHasResultFactory.cs" />
|
||||||
|
@ -88,6 +88,14 @@ namespace MediaBrowser.Controller.MediaEncoding
|
|||||||
/// <param name="type">The type.</param>
|
/// <param name="type">The type.</param>
|
||||||
/// <returns>System.String.</returns>
|
/// <returns>System.String.</returns>
|
||||||
string GetInputArgument(string[] inputFiles, InputType type);
|
string GetInputArgument(string[] inputFiles, InputType type);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encodes the image.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">The options.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns>Task{Stream}.</returns>
|
||||||
|
Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
namespace MediaBrowser.Controller.MediaEncoding
|
||||||
|
{
|
||||||
|
public class ImageEncodingOptions
|
||||||
|
{
|
||||||
|
public string InputPath { get; set; }
|
||||||
|
|
||||||
|
public int? Width { get; set; }
|
||||||
|
|
||||||
|
public int? Height { get; set; }
|
||||||
|
|
||||||
|
public int? MaxWidth { get; set; }
|
||||||
|
|
||||||
|
public int? MaxHeight { get; set; }
|
||||||
|
|
||||||
|
public string Format { get; set; }
|
||||||
|
}
|
||||||
|
}
|
158
MediaBrowser.MediaEncoding/Encoder/ImageEncoder.cs
Normal file
158
MediaBrowser.MediaEncoding/Encoder/ImageEncoder.cs
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
using MediaBrowser.Controller.MediaEncoding;
|
||||||
|
using MediaBrowser.Model.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
|
{
|
||||||
|
public class ImageEncoder
|
||||||
|
{
|
||||||
|
private readonly string _ffmpegPath;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
||||||
|
|
||||||
|
private static readonly SemaphoreSlim ResourcePool = new SemaphoreSlim(5, 5);
|
||||||
|
|
||||||
|
public ImageEncoder(string ffmpegPath, ILogger logger)
|
||||||
|
{
|
||||||
|
_ffmpegPath = ffmpegPath;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ValidateInput(options);
|
||||||
|
|
||||||
|
var process = new Process
|
||||||
|
{
|
||||||
|
StartInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
CreateNoWindow = true,
|
||||||
|
UseShellExecute = false,
|
||||||
|
FileName = _ffmpegPath,
|
||||||
|
Arguments = GetArguments(options),
|
||||||
|
WindowStyle = ProcessWindowStyle.Hidden,
|
||||||
|
ErrorDialog = false,
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
RedirectStandardError = true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await ResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
process.Start();
|
||||||
|
|
||||||
|
var memoryStream = new MemoryStream();
|
||||||
|
|
||||||
|
#pragma warning disable 4014
|
||||||
|
// Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
|
||||||
|
process.StandardOutput.BaseStream.CopyToAsync(memoryStream);
|
||||||
|
#pragma warning restore 4014
|
||||||
|
|
||||||
|
// MUST read both stdout and stderr asynchronously or a deadlock may occurr
|
||||||
|
process.BeginErrorReadLine();
|
||||||
|
|
||||||
|
var ranToCompletion = process.WaitForExit(5000);
|
||||||
|
|
||||||
|
if (!ranToCompletion)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.Info("Killing ffmpeg process");
|
||||||
|
|
||||||
|
process.Kill();
|
||||||
|
|
||||||
|
process.WaitForExit(1000);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.ErrorException("Error killing process", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourcePool.Release();
|
||||||
|
|
||||||
|
var exitCode = ranToCompletion ? process.ExitCode : -1;
|
||||||
|
|
||||||
|
process.Dispose();
|
||||||
|
|
||||||
|
if (exitCode == -1 || memoryStream.Length == 0)
|
||||||
|
{
|
||||||
|
memoryStream.Dispose();
|
||||||
|
|
||||||
|
var msg = string.Format("ffmpeg image encoding failed for {0}", options.InputPath);
|
||||||
|
|
||||||
|
_logger.Error(msg);
|
||||||
|
|
||||||
|
throw new ApplicationException(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
return memoryStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetArguments(ImageEncodingOptions options)
|
||||||
|
{
|
||||||
|
var vfScale = GetFilterGraph(options);
|
||||||
|
var outputFormat = GetOutputFormat(options);
|
||||||
|
|
||||||
|
return string.Format("-i file:\"{0}\" {1} -f {2}",
|
||||||
|
options.InputPath,
|
||||||
|
vfScale,
|
||||||
|
outputFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetFilterGraph(ImageEncodingOptions options)
|
||||||
|
{
|
||||||
|
if (!options.Width.HasValue &&
|
||||||
|
!options.Height.HasValue &&
|
||||||
|
!options.MaxHeight.HasValue &&
|
||||||
|
!options.MaxWidth.HasValue)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var widthScale = "-1";
|
||||||
|
var heightScale = "-1";
|
||||||
|
|
||||||
|
if (options.MaxWidth.HasValue)
|
||||||
|
{
|
||||||
|
widthScale = "min(iw," + options.MaxWidth.Value.ToString(_usCulture) + ")";
|
||||||
|
}
|
||||||
|
else if (options.Width.HasValue)
|
||||||
|
{
|
||||||
|
widthScale = options.Width.Value.ToString(_usCulture);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.MaxHeight.HasValue)
|
||||||
|
{
|
||||||
|
heightScale = "min(ih," + options.MaxHeight.Value.ToString(_usCulture) + ")";
|
||||||
|
}
|
||||||
|
else if (options.Height.HasValue)
|
||||||
|
{
|
||||||
|
heightScale = options.Height.Value.ToString(_usCulture);
|
||||||
|
}
|
||||||
|
|
||||||
|
var scaleMethod = "lanczos";
|
||||||
|
|
||||||
|
return string.Format("-vf scale=\"{0}:{1}\" -sws_flags {2}",
|
||||||
|
widthScale,
|
||||||
|
heightScale,
|
||||||
|
scaleMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetOutputFormat(ImageEncodingOptions options)
|
||||||
|
{
|
||||||
|
return options.Format;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ValidateInput(ImageEncodingOptions options)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -948,14 +948,9 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
|||||||
return GetFileInputArgument(playableStreamFiles[0]);
|
return GetFileInputArgument(playableStreamFiles[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
|
||||||
/// Gets the bluray input argument.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="blurayRoot">The bluray root.</param>
|
|
||||||
/// <returns>System.String.</returns>
|
|
||||||
private string GetBlurayInputArgument(string blurayRoot)
|
|
||||||
{
|
{
|
||||||
return string.Format("bluray:\"{0}\"", blurayRoot);
|
return new ImageEncoder(FFMpegPath, _logger).EncodeImage(options, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="BdInfo\BdInfoExaminer.cs" />
|
<Compile Include="BdInfo\BdInfoExaminer.cs" />
|
||||||
|
<Compile Include="Encoder\ImageEncoder.cs" />
|
||||||
<Compile Include="Encoder\MediaEncoder.cs" />
|
<Compile Include="Encoder\MediaEncoder.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user