2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2014-10-06 23:58:46 +00:00
|
|
|
using System.Collections.Generic;
|
2013-09-23 15:37:50 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-01-13 19:20:41 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
2016-11-18 21:06:00 +00:00
|
|
|
namespace Emby.Server.Implementations.FFMpeg
|
2013-09-23 15:37:50 +00:00
|
|
|
{
|
2016-04-02 04:29:48 +00:00
|
|
|
public class FFMpegLoader
|
2013-09-23 15:37:50 +00:00
|
|
|
{
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
2013-10-31 14:03:23 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-04-02 04:29:48 +00:00
|
|
|
private readonly FFMpegInstallInfo _ffmpegInstallInfo;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
2019-02-06 19:38:42 +00:00
|
|
|
public FFMpegLoader(IApplicationPaths appPaths, IFileSystem fileSystem, FFMpegInstallInfo ffmpegInstallInfo)
|
2013-09-23 15:37:50 +00:00
|
|
|
{
|
|
|
|
_appPaths = appPaths;
|
2013-10-31 14:03:23 +00:00
|
|
|
_fileSystem = fileSystem;
|
2016-04-02 04:29:48 +00:00
|
|
|
_ffmpegInstallInfo = ffmpegInstallInfo;
|
2013-09-23 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 20:58:47 +00:00
|
|
|
public FFMpegInfo GetFFMpegInfo(IStartupOptions options)
|
2013-09-23 15:37:50 +00:00
|
|
|
{
|
2019-01-28 20:58:47 +00:00
|
|
|
var customffMpegPath = options.FFmpegPath;
|
|
|
|
var customffProbePath = options.FFprobePath;
|
2014-09-14 15:26:33 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(customffMpegPath) && !string.IsNullOrWhiteSpace(customffProbePath))
|
|
|
|
{
|
|
|
|
return new FFMpegInfo
|
|
|
|
{
|
|
|
|
ProbePath = customffProbePath,
|
|
|
|
EncoderPath = customffMpegPath,
|
2016-06-23 17:04:18 +00:00
|
|
|
Version = "external"
|
2014-09-14 15:26:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-02 04:29:48 +00:00
|
|
|
var downloadInfo = _ffmpegInstallInfo;
|
2014-11-09 18:24:57 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var prebuiltFolder = _appPaths.ProgramSystemPath;
|
|
|
|
var prebuiltffmpeg = Path.Combine(prebuiltFolder, downloadInfo.FFMpegFilename);
|
|
|
|
var prebuiltffprobe = Path.Combine(prebuiltFolder, downloadInfo.FFProbeFilename);
|
2019-01-26 21:59:53 +00:00
|
|
|
if (File.Exists(prebuiltffmpeg) && File.Exists(prebuiltffprobe))
|
2014-09-18 01:26:23 +00:00
|
|
|
{
|
|
|
|
return new FFMpegInfo
|
|
|
|
{
|
2017-10-02 20:18:27 +00:00
|
|
|
ProbePath = prebuiltffprobe,
|
|
|
|
EncoderPath = prebuiltffmpeg,
|
|
|
|
Version = "external"
|
2014-09-18 01:26:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:18:27 +00:00
|
|
|
var version = downloadInfo.Version;
|
|
|
|
|
2016-06-30 01:19:38 +00:00
|
|
|
if (string.Equals(version, "0", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return new FFMpegInfo();
|
|
|
|
}
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
var rootEncoderPath = Path.Combine(_appPaths.ProgramDataPath, "ffmpeg");
|
2014-09-18 01:26:23 +00:00
|
|
|
var versionedDirectoryPath = Path.Combine(rootEncoderPath, version);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
2013-09-23 16:00:22 +00:00
|
|
|
var info = new FFMpegInfo
|
|
|
|
{
|
2014-11-09 18:24:57 +00:00
|
|
|
ProbePath = Path.Combine(versionedDirectoryPath, downloadInfo.FFProbeFilename),
|
|
|
|
EncoderPath = Path.Combine(versionedDirectoryPath, downloadInfo.FFMpegFilename),
|
2014-09-18 01:26:23 +00:00
|
|
|
Version = version
|
2013-09-23 16:00:22 +00:00
|
|
|
};
|
2013-09-23 15:37:50 +00:00
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(versionedDirectoryPath);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
2014-09-14 15:10:51 +00:00
|
|
|
var excludeFromDeletions = new List<string> { versionedDirectoryPath };
|
|
|
|
|
2019-01-26 21:59:53 +00:00
|
|
|
if (!File.Exists(info.ProbePath) || !File.Exists(info.EncoderPath))
|
2013-09-23 16:00:22 +00:00
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
// ffmpeg not present. See if there's an older version we can start with
|
|
|
|
var existingVersion = GetExistingVersion(info, rootEncoderPath);
|
2013-12-13 15:48:35 +00:00
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
// No older version. Need to download and block until complete
|
|
|
|
if (existingVersion == null)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return new FFMpegInfo();
|
2014-05-07 02:28:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info = existingVersion;
|
2019-01-26 20:47:11 +00:00
|
|
|
versionedDirectoryPath = Path.GetDirectoryName(info.EncoderPath);
|
2014-09-14 15:10:51 +00:00
|
|
|
excludeFromDeletions.Add(versionedDirectoryPath);
|
2014-05-07 02:28:19 +00:00
|
|
|
}
|
2013-09-23 16:00:22 +00:00
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
|
2015-12-21 16:10:06 +00:00
|
|
|
// Allow just one of these to be overridden, if desired.
|
|
|
|
if (!string.IsNullOrWhiteSpace(customffMpegPath))
|
|
|
|
{
|
|
|
|
info.EncoderPath = customffMpegPath;
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(customffProbePath))
|
|
|
|
{
|
2017-07-23 05:54:24 +00:00
|
|
|
info.ProbePath = customffProbePath;
|
2015-12-21 16:10:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
private FFMpegInfo GetExistingVersion(FFMpegInfo info, string rootEncoderPath)
|
|
|
|
{
|
|
|
|
var encoderFilename = Path.GetFileName(info.EncoderPath);
|
|
|
|
var probeFilename = Path.GetFileName(info.ProbePath);
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2019-02-06 19:38:42 +00:00
|
|
|
foreach (var directory in _fileSystem.GetDirectoryPaths(rootEncoderPath))
|
2013-12-13 15:48:35 +00:00
|
|
|
{
|
2016-11-18 21:06:00 +00:00
|
|
|
var allFiles = _fileSystem.GetFilePaths(directory, true).ToList();
|
2013-12-13 15:48:35 +00:00
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
var encoder = allFiles.FirstOrDefault(i => string.Equals(Path.GetFileName(i), encoderFilename, StringComparison.OrdinalIgnoreCase));
|
|
|
|
var probe = allFiles.FirstOrDefault(i => string.Equals(Path.GetFileName(i), probeFilename, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(encoder) &&
|
|
|
|
!string.IsNullOrWhiteSpace(probe))
|
2013-12-13 15:48:35 +00:00
|
|
|
{
|
2014-05-07 02:28:19 +00:00
|
|
|
return new FFMpegInfo
|
|
|
|
{
|
2015-02-11 20:23:07 +00:00
|
|
|
EncoderPath = encoder,
|
|
|
|
ProbePath = probe,
|
2019-01-26 20:47:11 +00:00
|
|
|
Version = Path.GetFileName(Path.GetDirectoryName(probe))
|
2014-05-07 02:28:19 +00:00
|
|
|
};
|
2013-12-13 15:48:35 +00:00
|
|
|
}
|
2014-05-07 02:28:19 +00:00
|
|
|
}
|
2013-09-23 17:27:38 +00:00
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
}
|
|
|
|
}
|