jellyfin/MediaBrowser.Server.Startup.Common/FFMpeg/FFMpegLoader.cs

251 lines
9.4 KiB
C#
Raw Normal View History

2014-10-06 16:58:46 -07:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
2013-09-24 17:54:51 -07:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
2014-10-06 16:58:46 -07:00
using Mono.Unix.Native;
using System;
2014-10-06 16:58:46 -07:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Server.Startup.Common.FFMpeg
{
2016-04-01 21:29:48 -07:00
public class FFMpegLoader
{
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _appPaths;
private readonly ILogger _logger;
2013-09-24 17:54:51 -07:00
private readonly IZipClient _zipClient;
private readonly IFileSystem _fileSystem;
2014-11-23 16:10:41 -07:00
private readonly NativeEnvironment _environment;
2016-04-01 21:29:48 -07:00
private readonly FFMpegInstallInfo _ffmpegInstallInfo;
2016-06-20 18:59:26 -07:00
public FFMpegLoader(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IZipClient zipClient, IFileSystem fileSystem, NativeEnvironment environment, FFMpegInstallInfo ffmpegInstallInfo)
{
_logger = logger;
_appPaths = appPaths;
_httpClient = httpClient;
2013-09-24 17:54:51 -07:00
_zipClient = zipClient;
_fileSystem = fileSystem;
2014-11-23 16:10:41 -07:00
_environment = environment;
2016-04-01 21:29:48 -07:00
_ffmpegInstallInfo = ffmpegInstallInfo;
}
public async Task<FFMpegInfo> GetFFMpegInfo(NativeEnvironment environment, StartupOptions options, IProgress<double> progress)
{
2014-09-14 08:26:33 -07:00
var customffMpegPath = options.GetOption("-ffmpeg");
var customffProbePath = options.GetOption("-ffprobe");
if (!string.IsNullOrWhiteSpace(customffMpegPath) && !string.IsNullOrWhiteSpace(customffProbePath))
{
return new FFMpegInfo
{
ProbePath = customffProbePath,
EncoderPath = customffMpegPath,
2016-06-23 10:04:18 -07:00
Version = "external"
2014-09-14 08:26:33 -07:00
};
}
2016-04-01 21:29:48 -07:00
var downloadInfo = _ffmpegInstallInfo;
var version = downloadInfo.Version;
if (string.Equals(version, "path", StringComparison.OrdinalIgnoreCase))
{
return new FFMpegInfo
{
ProbePath = downloadInfo.FFProbeFilename,
EncoderPath = downloadInfo.FFMpegFilename,
Version = version
};
}
2016-06-29 18:19:38 -07:00
if (string.Equals(version, "0", StringComparison.OrdinalIgnoreCase))
{
return new FFMpegInfo();
}
2014-05-06 19:28:19 -07:00
var rootEncoderPath = Path.Combine(_appPaths.ProgramDataPath, "ffmpeg");
var versionedDirectoryPath = Path.Combine(rootEncoderPath, version);
var info = new FFMpegInfo
{
ProbePath = Path.Combine(versionedDirectoryPath, downloadInfo.FFProbeFilename),
EncoderPath = Path.Combine(versionedDirectoryPath, downloadInfo.FFMpegFilename),
Version = version
};
2016-04-01 21:16:18 -07:00
_fileSystem.CreateDirectory(versionedDirectoryPath);
2014-09-14 08:10:51 -07:00
var excludeFromDeletions = new List<string> { versionedDirectoryPath };
2016-04-01 21:16:18 -07:00
if (!_fileSystem.FileExists(info.ProbePath) || !_fileSystem.FileExists(info.EncoderPath))
{
2014-05-06 19:28:19 -07:00
// ffmpeg not present. See if there's an older version we can start with
var existingVersion = GetExistingVersion(info, rootEncoderPath);
2014-05-06 19:28:19 -07:00
// No older version. Need to download and block until complete
if (existingVersion == null)
{
2016-06-29 18:19:38 -07:00
var success = await DownloadFFMpeg(downloadInfo, versionedDirectoryPath, progress).ConfigureAwait(false);
if (!success)
{
return new FFMpegInfo();
}
2014-05-06 19:28:19 -07:00
}
else
{
info = existingVersion;
versionedDirectoryPath = Path.GetDirectoryName(info.EncoderPath);
2014-09-14 08:10:51 -07:00
excludeFromDeletions.Add(versionedDirectoryPath);
2014-05-06 19:28:19 -07:00
}
}
// Allow just one of these to be overridden, if desired.
if (!string.IsNullOrWhiteSpace(customffMpegPath))
{
info.EncoderPath = customffMpegPath;
}
if (!string.IsNullOrWhiteSpace(customffProbePath))
{
info.EncoderPath = customffProbePath;
}
2014-05-06 19:28:19 -07:00
return info;
}
private FFMpegInfo GetExistingVersion(FFMpegInfo info, string rootEncoderPath)
{
var encoderFilename = Path.GetFileName(info.EncoderPath);
var probeFilename = Path.GetFileName(info.ProbePath);
foreach (var directory in Directory.EnumerateDirectories(rootEncoderPath, "*", SearchOption.TopDirectoryOnly)
.ToList())
{
2014-05-06 19:28:19 -07:00
var allFiles = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories).ToList();
2014-05-06 19:28:19 -07: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))
{
2014-05-06 19:28:19 -07:00
return new FFMpegInfo
{
2015-02-11 13:23:07 -07:00
EncoderPath = encoder,
ProbePath = probe,
Version = Path.GetFileName(Path.GetDirectoryName(probe))
2014-05-06 19:28:19 -07:00
};
}
2014-05-06 19:28:19 -07:00
}
2013-09-23 10:27:38 -07:00
2014-05-06 19:28:19 -07:00
return null;
}
2016-06-29 18:19:38 -07:00
private async Task<bool> DownloadFFMpeg(FFMpegInstallInfo downloadinfo, string directory, IProgress<double> progress)
{
foreach (var url in downloadinfo.DownloadUrls)
{
progress.Report(0);
try
{
var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
{
Url = url,
CancellationToken = CancellationToken.None,
Progress = progress
}).ConfigureAwait(false);
ExtractFFMpeg(downloadinfo, tempFile, directory);
2016-06-29 18:19:38 -07:00
return true;
}
2014-01-20 23:10:58 -07:00
catch (Exception ex)
{
2014-07-29 20:31:35 -07:00
_logger.ErrorException("Error downloading {0}", ex, url);
}
}
2016-06-29 18:19:38 -07:00
return false;
}
2016-04-01 21:29:48 -07:00
private void ExtractFFMpeg(FFMpegInstallInfo downloadinfo, string tempFile, string targetFolder)
{
2014-05-06 19:28:19 -07:00
_logger.Info("Extracting ffmpeg from {0}", tempFile);
var tempFolder = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString());
2016-04-01 21:16:18 -07:00
_fileSystem.CreateDirectory(tempFolder);
try
{
ExtractArchive(downloadinfo, tempFile, tempFolder);
2013-10-12 20:39:22 -07:00
2015-02-11 13:23:07 -07:00
var files = Directory.EnumerateFiles(tempFolder, "*", SearchOption.AllDirectories)
.ToList();
2013-10-12 20:39:22 -07:00
foreach (var file in files.Where(i =>
{
var filename = Path.GetFileName(i);
2013-10-12 20:39:22 -07:00
return
string.Equals(filename, downloadinfo.FFProbeFilename, StringComparison.OrdinalIgnoreCase) ||
string.Equals(filename, downloadinfo.FFMpegFilename, StringComparison.OrdinalIgnoreCase);
2013-10-12 20:39:22 -07:00
}))
{
2015-01-25 14:04:29 -07:00
var targetFile = Path.Combine(targetFolder, Path.GetFileName(file));
2016-04-01 21:16:18 -07:00
_fileSystem.CopyFile(file, targetFile, true);
2015-01-25 14:04:29 -07:00
SetFilePermissions(targetFile);
}
}
finally
{
DeleteFile(tempFile);
}
}
2015-01-25 14:04:29 -07:00
private void SetFilePermissions(string path)
2014-10-06 16:58:46 -07:00
{
// Linux: File permission to 666, and user's execute bit
2014-11-23 16:10:41 -07:00
if (_environment.OperatingSystem == OperatingSystem.Bsd || _environment.OperatingSystem == OperatingSystem.Linux || _environment.OperatingSystem == OperatingSystem.Osx)
2014-10-06 16:58:46 -07:00
{
2015-02-11 13:23:07 -07:00
_logger.Info("Syscall.chmod {0} FilePermissions.DEFFILEMODE | FilePermissions.S_IRWXU | FilePermissions.S_IXGRP | FilePermissions.S_IXOTH", path);
2015-01-27 21:17:35 -07:00
Syscall.chmod(path, FilePermissions.DEFFILEMODE | FilePermissions.S_IRWXU | FilePermissions.S_IXGRP | FilePermissions.S_IXOTH);
2014-10-06 16:58:46 -07:00
}
}
2016-04-01 21:29:48 -07:00
private void ExtractArchive(FFMpegInstallInfo downloadinfo, string archivePath, string targetPath)
{
2014-05-06 19:28:19 -07:00
_logger.Info("Extracting {0} to {1}", archivePath, targetPath);
if (string.Equals(downloadinfo.ArchiveType, "7z", StringComparison.OrdinalIgnoreCase))
2013-10-12 20:39:22 -07:00
{
_zipClient.ExtractAllFrom7z(archivePath, targetPath, true);
}
else if (string.Equals(downloadinfo.ArchiveType, "gz", StringComparison.OrdinalIgnoreCase))
2013-10-12 20:39:22 -07:00
{
_zipClient.ExtractAllFromTar(archivePath, targetPath, true);
}
}
private void DeleteFile(string path)
{
try
{
_fileSystem.DeleteFile(path);
}
catch (IOException ex)
{
_logger.ErrorException("Error deleting temp file {0}", ex, path);
}
}
}
}