mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
fixed cleanup of encoded media
This commit is contained in:
parent
8329710cf5
commit
4ab6a339c6
@ -1,4 +1,5 @@
|
|||||||
using MediaBrowser.Controller.Plugins;
|
using System.IO;
|
||||||
|
using MediaBrowser.Controller.Plugins;
|
||||||
using MediaBrowser.Model.Logging;
|
using MediaBrowser.Model.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -187,37 +188,11 @@ namespace MediaBrowser.Api
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when [transcoding finished].
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The path.</param>
|
|
||||||
/// <param name="type">The type.</param>
|
|
||||||
public void OnTranscodingFinished(string path, TranscodingJobType type)
|
|
||||||
{
|
|
||||||
lock (_activeTranscodingJobs)
|
|
||||||
{
|
|
||||||
var job = _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
|
|
||||||
|
|
||||||
if (job == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_activeTranscodingJobs.Remove(job);
|
|
||||||
|
|
||||||
if (job.KillTimer != null)
|
|
||||||
{
|
|
||||||
job.KillTimer.Dispose();
|
|
||||||
job.KillTimer = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called when [transcode kill timer stopped].
|
/// Called when [transcode kill timer stopped].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="state">The state.</param>
|
/// <param name="state">The state.</param>
|
||||||
private void OnTranscodeKillTimerStopped(object state)
|
private async void OnTranscodeKillTimerStopped(object state)
|
||||||
{
|
{
|
||||||
var job = (TranscodingJob)state;
|
var job = (TranscodingJob)state;
|
||||||
|
|
||||||
@ -253,16 +228,16 @@ namespace MediaBrowser.Api
|
|||||||
Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
|
Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasExited)
|
if (!hasExited)
|
||||||
{
|
{
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Logger.Info("Killing ffmpeg process for {0}", job.Path);
|
Logger.Info("Killing ffmpeg process for {0}", job.Path);
|
||||||
|
|
||||||
process.Kill();
|
process.Kill();
|
||||||
|
|
||||||
|
// Need to wait because killing is asynchronous
|
||||||
|
process.WaitForExit(5000);
|
||||||
}
|
}
|
||||||
catch (Win32Exception ex)
|
catch (Win32Exception ex)
|
||||||
{
|
{
|
||||||
@ -278,6 +253,85 @@ namespace MediaBrowser.Api
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine if it exited successfully
|
||||||
|
var hasExitedSuccessfully = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
hasExitedSuccessfully = process.ExitCode == 0;
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (NotSupportedException)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dispose the process
|
||||||
|
process.Dispose();
|
||||||
|
|
||||||
|
// If it didn't complete successfully cleanup the partial files
|
||||||
|
if (!hasExitedSuccessfully)
|
||||||
|
{
|
||||||
|
Logger.Info("Deleting partial stream file(s) {0}", job.Path);
|
||||||
|
|
||||||
|
await Task.Delay(1000).ConfigureAwait(false);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (job.Type == TranscodingJobType.Progressive)
|
||||||
|
{
|
||||||
|
DeleteProgressivePartialStreamFiles(job.Path);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DeleteHlsPartialStreamFiles(job.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, job.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the progressive partial stream files.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="outputFilePath">The output file path.</param>
|
||||||
|
private void DeleteProgressivePartialStreamFiles(string outputFilePath)
|
||||||
|
{
|
||||||
|
File.Delete(outputFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes the HLS partial stream files.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="outputFilePath">The output file path.</param>
|
||||||
|
private void DeleteHlsPartialStreamFiles(string outputFilePath)
|
||||||
|
{
|
||||||
|
var directory = Path.GetDirectoryName(outputFilePath);
|
||||||
|
var name = Path.GetFileNameWithoutExtension(outputFilePath);
|
||||||
|
|
||||||
|
var filesToDelete = Directory.EnumerateFiles(directory)
|
||||||
|
.Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
foreach (var file in filesToDelete)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Logger.Info("Deleting HLS file {0}", file);
|
||||||
|
File.Delete(file);
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
Logger.ErrorException("Error deleting HLS file {0}", ex, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -674,7 +674,7 @@ namespace MediaBrowser.Api.Playback
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="process">The process.</param>
|
/// <param name="process">The process.</param>
|
||||||
/// <param name="state">The state.</param>
|
/// <param name="state">The state.</param>
|
||||||
protected async void OnFfMpegProcessExited(Process process, StreamState state)
|
protected void OnFfMpegProcessExited(Process process, StreamState state)
|
||||||
{
|
{
|
||||||
if (state.IsoMount != null)
|
if (state.IsoMount != null)
|
||||||
{
|
{
|
||||||
@ -686,48 +686,15 @@ namespace MediaBrowser.Api.Playback
|
|||||||
|
|
||||||
state.LogFileStream.Dispose();
|
state.LogFileStream.Dispose();
|
||||||
|
|
||||||
int? exitCode = null;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
exitCode = process.ExitCode;
|
Logger.Info("FFMpeg exited with code {0} for {1}", process.ExitCode, outputFilePath);
|
||||||
Logger.Info("FFMpeg exited with code {0} for {1}", exitCode.Value, outputFilePath);
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
|
Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
process.Dispose();
|
|
||||||
|
|
||||||
ApiEntryPoint.Instance.OnTranscodingFinished(outputFilePath, TranscodingJobType);
|
|
||||||
|
|
||||||
if (!exitCode.HasValue || exitCode.Value != 0)
|
|
||||||
{
|
|
||||||
Logger.Info("Deleting partial stream file(s) {0}", outputFilePath);
|
|
||||||
|
|
||||||
await Task.Delay(1000).ConfigureAwait(false);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
DeletePartialStreamFiles(outputFilePath);
|
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, outputFilePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Logger.Info("FFMpeg completed and exited normally for {0}", outputFilePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes the partial stream files.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="outputFilePath">The output file path.</param>
|
|
||||||
protected abstract void DeletePartialStreamFiles(string outputFilePath);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the state.
|
/// Gets the state.
|
||||||
|
@ -207,32 +207,5 @@ namespace MediaBrowser.Api.Playback.Hls
|
|||||||
segmentOutputPath
|
segmentOutputPath
|
||||||
).Trim();
|
).Trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes the partial stream files.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="outputFilePath">The output file path.</param>
|
|
||||||
protected override void DeletePartialStreamFiles(string outputFilePath)
|
|
||||||
{
|
|
||||||
var directory = Path.GetDirectoryName(outputFilePath);
|
|
||||||
var name = Path.GetFileNameWithoutExtension(outputFilePath);
|
|
||||||
|
|
||||||
var filesToDelete = Directory.EnumerateFiles(directory)
|
|
||||||
.Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
foreach (var file in filesToDelete)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Logger.Info("Deleting HLS file {0}", file);
|
|
||||||
File.Delete(file);
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
Logger.ErrorException("Error deleting HLS file {0}", ex, file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -357,14 +357,5 @@ namespace MediaBrowser.Api.Playback.Progressive
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes the partial stream files.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="outputFilePath">The output file path.</param>
|
|
||||||
protected override void DeletePartialStreamFiles(string outputFilePath)
|
|
||||||
{
|
|
||||||
File.Delete(outputFilePath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user