Skip to content
Permalink
Browse files

Changed FFMpegLib to be a lib and removed service

  • Loading branch information...
henso96 committed Oct 18, 2015
1 parent 44bc6a0 commit 07aca0828723f86ce21dc634601a732b58b32982
@@ -107,7 +107,7 @@ public MetadataContainer ParseFile(ILocalFsResourceAccessor lfsra)

ProcessExecutionResult executionResult;
lock (FFPROBE_THROTTLE_LOCK)
executionResult = ServiceRegistration.Get<IFFMpegLib>().FFProbeExecuteWithResourceAccessAsync(lfsra, arguments, ProcessPriorityClass.Idle, AnalyzerTimeout).Result;
executionResult = FFMpegBinary.FFProbeExecuteWithResourceAccessAsync(lfsra, arguments, ProcessPriorityClass.Idle, AnalyzerTimeout).Result;

// My guess (agree with dtb's comment): AFAIK ffmpeg uses stdout to pipe out binary data(multimedia, snapshots, etc.)
// and stderr is used for logging purposes. In your example you use stdout.
@@ -144,7 +144,7 @@ public MetadataContainer ParseStream(INetworkResourceAccessor streamLink)

ProcessExecutionResult executionResult;
lock (FFPROBE_THROTTLE_LOCK)
executionResult = ServiceRegistration.Get<IFFMpegLib>().FFProbeExecuteAsync(arguments, ProcessPriorityClass.Idle, AnalyzerTimeout).Result;
executionResult = FFMpegBinary.FFProbeExecuteAsync(arguments, ProcessPriorityClass.Idle, AnalyzerTimeout).Result;

if (executionResult != null && executionResult.Success && executionResult.ExitCode == 0 && !string.IsNullOrEmpty(executionResult.StandardError))
{
@@ -120,8 +120,7 @@ public static int HLSSegmentTimeInSeconds
static MediaConverter()
{
_logger = ServiceRegistration.Get<ILogger>();
FFMpegLib ffmpegLib = new FFMpegLib();
_transcoderBinPath = ffmpegLib.FFMpegBinaryPath;
_transcoderBinPath = FFMpegBinary.FFMpegPath;
string result;
using (Process process = new Process { StartInfo = new ProcessStartInfo(_transcoderBinPath, "") { UseShellExecute = false, CreateNoWindow = true, RedirectStandardError = true } })
{
@@ -33,7 +33,7 @@ namespace MediaPortal.Plugins.Transcoding.Service.Transcoders.FFMpeg
internal class FFMpegTranscodeData : TranscodeData
{

private static readonly string BIN_TRANSCODER = ServiceRegistration.Get<IFFMpegLib>().FFMpegBinaryPath;
private static readonly string BIN_TRANSCODER = FFMpegBinary.FFMpegPath;

public EncoderHandler Encoder { get; set; }

@@ -131,10 +131,9 @@ internal static void ParseH264Info(ref MetadataContainer info, Dictionary<float,
// TODO: Should be in the FFMpegLib
private static bool TryExecuteBinary(string arguments, out byte[] result, ILocalFsResourceAccessor lfsra, ProcessPriorityClass priorityClass = ProcessPriorityClass.Normal)
{
FFMpegLib ffmpeg = new FFMpegLib();
using (ServiceRegistration.Get<IImpersonationService>().CheckImpersonationFor(lfsra.CanonicalLocalResourcePath))
{
using (Process process = new Process { StartInfo = new ProcessStartInfo(ffmpeg.FFMpegBinaryPath, arguments) { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true } })
using (Process process = new Process { StartInfo = new ProcessStartInfo(FFMpegBinary.FFMpegPath, arguments) { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true } })
{
process.Start();
process.PriorityClass = priorityClass;
@@ -31,7 +31,7 @@

namespace MediaPortal.Extensions.MetadataExtractors.FFMpegLib
{
public class FFMpegLib : IFFMpegLib
public static class FFMpegBinary
{
#region Constants

@@ -49,56 +49,112 @@ public class FFMpegLib : IFFMpegLib

#region Variables

private readonly string _ffMpegBinPath;
private readonly string _ffProbeBinPath;
private static readonly string _ffMpegBinPath;
private static readonly string _ffProbeBinPath;

#endregion

public FFMpegLib()
/// <summary>
/// <see cref="FFMpegLib"/> provides access to the "ffmpeg.exe" and "ffprobe.exe" programs for processing video and its metadata.
/// </summary>
static FFMpegBinary()
{
_ffMpegBinPath = FileUtils.BuildAssemblyRelativePath(FFMPEG_EXECUTABLE);
_ffProbeBinPath = FileUtils.BuildAssemblyRelativePath(FFPROBE_EXECUTABLE);
}

#region IFFMpegLib implementation

Task<ProcessExecutionResult> IFFMpegLib.FFMpegExecuteWithResourceAccessAsync(ILocalFsResourceAccessor lfsra, string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
/// <summary>
/// Returns the absolute path to FFMpeg binary.
/// </summary>
public static string FFMpegPath
{
return lfsra.ExecuteWithResourceAccessAsync(_ffMpegBinPath, arguments, priorityClass, maxWaitMs);
get
{
return _ffMpegBinPath;
}
}

Task<ProcessExecutionResult> IFFMpegLib.FFProbeExecuteWithResourceAccessAsync(ILocalFsResourceAccessor lfsra, string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
/// <summary>
/// Returns the absolute path to FFProbe binary.
/// </summary>
public static string FFProbePath
{
return lfsra.ExecuteWithResourceAccessAsync(_ffProbeBinPath, arguments, priorityClass, maxWaitMs);
get
{
return _ffProbeBinPath;
}
}

Task<ProcessExecutionResult> IFFMpegLib.FFMpegExecuteAsync(string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
#region Async methods

/// <summary>
/// Executes FFMpeg and ensures that it has access to the respective resource
/// </summary>
/// <param name="lfsra"><see cref="ILocalFsResourceAccessor"/> to which FFMpeg needs access to</param>
/// <param name="arguments">Arguments for FFMpeg</param>
/// <param name="priorityClass">Process priority</param>
/// <param name="maxWaitMs">Maximum time to wait for completion</param>
/// <returns>A <see cref="Task"/> representing the result of executing FFMpeg</returns>
/// <remarks>
/// This is a convenience method that enables executing FFMpeg directly on the <see cref="ILocalFsResourceAccessor"/>
/// interface to which FFMpeg needs access. The purpose of an <see cref="ILocalFsResourceAccessor"/> is providing
/// access to a resource - not executing programs which is why this method is implemented as an extension method instead of
/// a method directly on the interface.
/// </remarks>
public static Task<ProcessExecutionResult> FFMpegExecuteWithResourceAccessAsync(ILocalFsResourceAccessor lfsra, string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
{
return ProcessUtils.ExecuteAsync(_ffMpegBinPath, arguments, priorityClass, maxWaitMs);
return lfsra.ExecuteWithResourceAccessAsync(_ffMpegBinPath, arguments, priorityClass, maxWaitMs);
}

Task<ProcessExecutionResult> IFFMpegLib.FFProbeExecuteAsync(string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
/// <summary>
/// Executes FFProbe and ensures that it has access to the respective resource
/// </summary>
/// <param name="lfsra"><see cref="ILocalFsResourceAccessor"/> to which FFProbe needs access to</param>
/// <param name="arguments">Arguments for FFProbe</param>
/// <param name="priorityClass">Process priority</param>
/// <param name="maxWaitMs">Maximum time to wait for completion</param>
/// <returns>A <see cref="Task"/> representing the result of executing FFProbe</returns>
/// <remarks>
/// This is a convenience method that enables executing FFProbe directly on the <see cref="ILocalFsResourceAccessor"/>
/// interface to which FFProbe needs access. The purpose of an <see cref="ILocalFsResourceAccessor"/> is providing
/// access to a resource - not executing programs which is why this method is implemented as an extension method instead of
/// a method directly on the interface.
/// </remarks>
public static Task<ProcessExecutionResult> FFProbeExecuteWithResourceAccessAsync(ILocalFsResourceAccessor lfsra, string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
{
return ProcessUtils.ExecuteAsync(_ffProbeBinPath, arguments, priorityClass, maxWaitMs);
return lfsra.ExecuteWithResourceAccessAsync(_ffProbeBinPath, arguments, priorityClass, maxWaitMs);
}

public string FFMpegBinaryPath
/// <summary>
/// Executes FFMpeg. This function doesn't check if impersionation is necessary
/// </summary>
/// <param name="arguments">Arguments for FFMpeg</param>
/// <param name="priorityClass">Process priority</param>
/// <param name="maxWaitMs">Maximum time to wait for completion</param>
/// <returns>A <see cref="Task"/> representing the result of executing FFMpeg</returns>
/// <remarks>
/// This is a convenience method that enables executing FFMpeg directly.
/// </remarks>
public static Task<ProcessExecutionResult> FFMpegExecuteAsync(string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
{
get
{
return _ffMpegBinPath;
}
return ProcessUtils.ExecuteAsync(_ffMpegBinPath, arguments, priorityClass, maxWaitMs);
}

public string FFProbeBinaryPath
/// <summary>
/// Executes FFProbe. This function doesn't check if impersionation is necessary
/// </summary>
/// <param name="arguments">Arguments for FFProbe</param>
/// <param name="priorityClass">Process priority</param>
/// <param name="maxWaitMs">Maximum time to wait for completion</param>
/// <returns>A <see cref="Task"/> representing the result of executing FFProbe</returns>
/// <remarks>
/// This is a convenience method that enables executing FFProbe directly.
/// </remarks>
public static Task<ProcessExecutionResult> FFProbeExecuteAsync(string arguments, ProcessPriorityClass priorityClass, int maxWaitMs)
{
get
{
return _ffProbeBinPath;
}
return ProcessUtils.ExecuteAsync(_ffProbeBinPath, arguments, priorityClass, maxWaitMs);
}

#endregion

}
}
@@ -67,7 +67,6 @@
<Link>Properties\VersionInfo.cs</Link>
</Compile>
<Compile Include="FFMpegLib.cs" />
<Compile Include="IFFMpegLib.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

This file was deleted.

@@ -17,10 +17,7 @@

<DependsOn>
<CoreDependency Name="MediaPortal.Common" CompatibleAPI="1" />
<CoreDependency Name="MediaPortal.Utilities" CompatibleAPI="1" />
</DependsOn>

<Register Location="/Services">
<Service Id="FFMpegLib" RegistrationClassName="MediaPortal.Extensions.MetadataExtractors.FFMpegLib.IFFMpegLib"
ServiceClassName="MediaPortal.Extensions.MetadataExtractors.FFMpegLib.FFMpegLib"/>
</Register>
</Plugin>
@@ -157,7 +157,7 @@ private bool ExtractThumbnail(ILocalFsResourceAccessor lfsra, IDictionary<Guid,
{
bool success;
lock (FFMPEG_THROTTLE_LOCK)
success = ServiceRegistration.Get<IFFMpegLib>().FFMpegExecuteWithResourceAccessAsync(lfsra, arguments, ProcessPriorityClass.Idle, PROCESS_TIMEOUT_MS).Result.Success;
success = FFMpegBinary.FFMpegExecuteWithResourceAccessAsync(lfsra, arguments, ProcessPriorityClass.Idle, PROCESS_TIMEOUT_MS).Result.Success;
if (success && File.Exists(tempFileName))
{
var binary = FileUtils.ReadFile(tempFileName);
@@ -191,4 +191,4 @@ private bool ExtractThumbnail(ILocalFsResourceAccessor lfsra, IDictionary<Guid,

#endregion
}
}
}

0 comments on commit 07aca08

Please sign in to comment.
You can’t perform that action at this time.