2013-09-23 15:37:50 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-12-13 15:48:35 +00:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2013-09-25 00:54:51 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-09-23 17:14:17 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
using System;
|
2013-09-23 17:27:38 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2013-09-23 17:14:17 +00:00
|
|
|
|
using System.Threading;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2013-12-25 19:26:49 +00:00
|
|
|
|
#if __MonoCS__
|
|
|
|
|
using Mono.Unix.Native;
|
|
|
|
|
#endif
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-25 00:54:51 +00:00
|
|
|
|
namespace MediaBrowser.ServerApplication.FFMpeg
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
|
|
|
|
public class FFMpegDownloader
|
|
|
|
|
{
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly ILogger _logger;
|
2013-09-25 00:54:51 +00:00
|
|
|
|
private readonly IZipClient _zipClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-23 18:14:07 +00:00
|
|
|
|
private readonly string[] _fontUrls = new[]
|
|
|
|
|
{
|
|
|
|
|
"https://www.dropbox.com/s/pj847twf7riq0j7/ARIALUNI.7z?dl=1"
|
|
|
|
|
};
|
2013-09-23 17:14:17 +00:00
|
|
|
|
|
2013-10-31 14:03:23 +00:00
|
|
|
|
public FFMpegDownloader(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IZipClient zipClient, IFileSystem fileSystem)
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_httpClient = httpClient;
|
2013-09-25 00:54:51 +00:00
|
|
|
|
_zipClient = zipClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-13 15:48:35 +00:00
|
|
|
|
public async Task<FFMpegInfo> GetFFMpegInfo(IProgress<double> progress)
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
2013-10-13 03:39:22 +00:00
|
|
|
|
var versionedDirectoryPath = Path.Combine(GetMediaToolsPath(true), FFMpegDownloadInfo.Version);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-23 16:00:22 +00:00
|
|
|
|
var info = new FFMpegInfo
|
|
|
|
|
{
|
2013-10-13 03:39:22 +00:00
|
|
|
|
ProbePath = Path.Combine(versionedDirectoryPath, FFMpegDownloadInfo.FFProbeFilename),
|
|
|
|
|
Path = Path.Combine(versionedDirectoryPath, FFMpegDownloadInfo.FFMpegFilename),
|
|
|
|
|
Version = FFMpegDownloadInfo.Version
|
2013-09-23 16:00:22 +00:00
|
|
|
|
};
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
Directory.CreateDirectory(versionedDirectoryPath);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-23 17:27:38 +00:00
|
|
|
|
var tasks = new List<Task>();
|
|
|
|
|
|
2013-12-13 15:48:35 +00:00
|
|
|
|
double ffmpegPercent = 0;
|
|
|
|
|
double fontPercent = 0;
|
|
|
|
|
var syncLock = new object();
|
|
|
|
|
|
2013-09-23 16:00:22 +00:00
|
|
|
|
if (!File.Exists(info.ProbePath) || !File.Exists(info.Path))
|
|
|
|
|
{
|
2013-12-13 15:48:35 +00:00
|
|
|
|
var ffmpegProgress = new ActionableProgress<double>();
|
|
|
|
|
ffmpegProgress.RegisterAction(p =>
|
|
|
|
|
{
|
|
|
|
|
ffmpegPercent = p;
|
|
|
|
|
|
|
|
|
|
lock (syncLock)
|
|
|
|
|
{
|
|
|
|
|
progress.Report((ffmpegPercent / 2) + (fontPercent / 2));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tasks.Add(DownloadFFMpeg(info, ffmpegProgress));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffmpegPercent = 100;
|
|
|
|
|
progress.Report(50);
|
2013-09-23 16:00:22 +00:00
|
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-12-13 15:48:35 +00:00
|
|
|
|
var fontProgress = new ActionableProgress<double>();
|
|
|
|
|
fontProgress.RegisterAction(p =>
|
|
|
|
|
{
|
|
|
|
|
fontPercent = p;
|
|
|
|
|
|
|
|
|
|
lock (syncLock)
|
|
|
|
|
{
|
|
|
|
|
progress.Report((ffmpegPercent / 2) + (fontPercent / 2));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tasks.Add(DownloadFonts(versionedDirectoryPath, fontProgress));
|
2013-09-23 17:27:38 +00:00
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
2013-09-23 16:00:22 +00:00
|
|
|
|
|
|
|
|
|
return info;
|
2013-09-23 15:37:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-13 15:48:35 +00:00
|
|
|
|
private async Task DownloadFFMpeg(FFMpegInfo info, IProgress<double> progress)
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
2013-10-13 03:39:22 +00:00
|
|
|
|
foreach (var url in FFMpegDownloadInfo.FfMpegUrls)
|
2013-09-23 17:14:17 +00:00
|
|
|
|
{
|
2013-12-13 15:48:35 +00:00
|
|
|
|
progress.Report(0);
|
|
|
|
|
|
2013-09-23 17:14:17 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-12-13 15:48:35 +00:00
|
|
|
|
var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = CancellationToken.None,
|
|
|
|
|
Progress = progress
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-23 17:14:17 +00:00
|
|
|
|
ExtractFFMpeg(tempFile, Path.GetDirectoryName(info.Path));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-01-21 06:10:58 +00:00
|
|
|
|
catch (HttpException ex)
|
2013-09-23 17:14:17 +00:00
|
|
|
|
{
|
2014-01-21 06:10:58 +00:00
|
|
|
|
_logger.ErrorException("Error downloading {0}", ex, url);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error unpacking {0}", ex, url);
|
2013-09-23 17:14:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-23 17:27:38 +00:00
|
|
|
|
|
|
|
|
|
throw new ApplicationException("Unable to download required components. Please try again later.");
|
2013-09-23 17:14:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExtractFFMpeg(string tempFile, string targetFolder)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Extracting ffmpeg from {0}", tempFile);
|
|
|
|
|
|
|
|
|
|
var tempFolder = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString());
|
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
Directory.CreateDirectory(tempFolder);
|
2013-09-23 17:14:17 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-10-13 03:39:22 +00:00
|
|
|
|
ExtractArchive(tempFile, tempFolder);
|
|
|
|
|
|
|
|
|
|
var files = Directory.EnumerateFiles(tempFolder, "*", SearchOption.AllDirectories).ToList();
|
2013-09-23 17:14:17 +00:00
|
|
|
|
|
2013-10-13 03:39:22 +00:00
|
|
|
|
foreach (var file in files.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileName(i);
|
2013-09-23 17:14:17 +00:00
|
|
|
|
|
2013-10-13 03:39:22 +00:00
|
|
|
|
return
|
|
|
|
|
string.Equals(filename, FFMpegDownloadInfo.FFProbeFilename, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals(filename, FFMpegDownloadInfo.FFMpegFilename, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}))
|
2013-09-23 17:14:17 +00:00
|
|
|
|
{
|
2013-10-04 15:22:03 +00:00
|
|
|
|
File.Copy(file, Path.Combine(targetFolder, Path.GetFileName(file)), true);
|
2013-12-25 19:26:49 +00:00
|
|
|
|
#if __MonoCS__
|
|
|
|
|
//Linux: File permission to 666, and user's execute bit
|
|
|
|
|
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
|
|
|
|
|
{
|
|
|
|
|
Syscall.chmod(Path.Combine(targetFolder, Path.GetFileName(file)), FilePermissions.DEFFILEMODE | FilePermissions.S_IXUSR);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-09-23 17:14:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
DeleteFile(tempFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 03:39:22 +00:00
|
|
|
|
private void ExtractArchive(string archivePath, string targetPath)
|
2013-09-23 17:14:17 +00:00
|
|
|
|
{
|
2013-10-13 03:39:22 +00:00
|
|
|
|
if (string.Equals(FFMpegDownloadInfo.ArchiveType, "7z", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_zipClient.ExtractAllFrom7z(archivePath, targetPath, true);
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(FFMpegDownloadInfo.ArchiveType, "gz", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_zipClient.ExtractAllFromTar(archivePath, targetPath, true);
|
|
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
|
}
|
2013-10-13 19:56:54 +00:00
|
|
|
|
private void Extract7zArchive(string archivePath, string targetPath)
|
|
|
|
|
{
|
|
|
|
|
_zipClient.ExtractAllFrom7z(archivePath, targetPath, true);
|
|
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-23 17:14:17 +00:00
|
|
|
|
private void DeleteFile(string path)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error deleting temp file {0}", ex, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extracts the fonts.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="targetPath">The target path.</param>
|
2013-12-13 15:48:35 +00:00
|
|
|
|
private async Task DownloadFonts(string targetPath, IProgress<double> progress)
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
2013-09-23 17:27:38 +00:00
|
|
|
|
try
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
2013-09-23 17:27:38 +00:00
|
|
|
|
var fontsDirectory = Path.Combine(targetPath, "fonts");
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
Directory.CreateDirectory(fontsDirectory);
|
2013-09-23 17:27:38 +00:00
|
|
|
|
|
|
|
|
|
const string fontFilename = "ARIALUNI.TTF";
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-23 17:27:38 +00:00
|
|
|
|
var fontFile = Path.Combine(fontsDirectory, fontFilename);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(fontFile))
|
|
|
|
|
{
|
2013-12-13 15:48:35 +00:00
|
|
|
|
await DownloadFontFile(fontsDirectory, fontFilename, progress).ConfigureAwait(false);
|
2013-09-23 17:27:38 +00:00
|
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-09-23 17:27:38 +00:00
|
|
|
|
await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
2013-09-23 17:27:38 +00:00
|
|
|
|
// Don't let the server crash because of this
|
|
|
|
|
_logger.ErrorException("Error downloading ffmpeg font files", ex);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Don't let the server crash because of this
|
|
|
|
|
_logger.ErrorException("Error writing ffmpeg font files", ex);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
}
|
2013-12-13 15:48:35 +00:00
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Downloads the font file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fontsDirectory">The fonts directory.</param>
|
|
|
|
|
/// <param name="fontFilename">The font filename.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-12-13 15:48:35 +00:00
|
|
|
|
private async Task DownloadFontFile(string fontsDirectory, string fontFilename, IProgress<double> progress)
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
|
|
|
|
var existingFile = Directory
|
|
|
|
|
.EnumerateFiles(_appPaths.ProgramDataPath, fontFilename, SearchOption.AllDirectories)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (existingFile != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Copy(existingFile, Path.Combine(fontsDirectory, fontFilename), true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
// Log this, but don't let it fail the operation
|
|
|
|
|
_logger.ErrorException("Error copying file", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-23 18:14:07 +00:00
|
|
|
|
string tempFile = null;
|
|
|
|
|
|
|
|
|
|
foreach (var url in _fontUrls)
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
2013-12-13 15:48:35 +00:00
|
|
|
|
progress.Report(0);
|
|
|
|
|
|
2013-09-23 18:14:07 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
2013-12-13 15:48:35 +00:00
|
|
|
|
Progress = progress
|
2013-09-23 18:14:07 +00:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// The core can function without the font file, so handle this
|
|
|
|
|
_logger.ErrorException("Failed to download ffmpeg font file from {0}", ex, url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(tempFile))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
2013-10-13 19:56:54 +00:00
|
|
|
|
Extract7zArchive(tempFile, fontsDirectory);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Delete(tempFile);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
// Log this, but don't let it fail the operation
|
|
|
|
|
_logger.ErrorException("Error deleting temp file {0}", ex, tempFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes the font config file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fontsDirectory">The fonts directory.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task WriteFontConfigFile(string fontsDirectory)
|
|
|
|
|
{
|
|
|
|
|
const string fontConfigFilename = "fonts.conf";
|
|
|
|
|
var fontConfigFile = Path.Combine(fontsDirectory, fontConfigFilename);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(fontConfigFile))
|
|
|
|
|
{
|
|
|
|
|
var contents = string.Format("<?xml version=\"1.0\"?><fontconfig><dir>{0}</dir><alias><family>Arial</family><prefer>Arial Unicode MS</prefer></alias></fontconfig>", fontsDirectory);
|
|
|
|
|
|
|
|
|
|
var bytes = Encoding.UTF8.GetBytes(contents);
|
|
|
|
|
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(fontConfigFile, FileMode.Create, FileAccess.Write,
|
|
|
|
|
FileShare.Read, true))
|
2013-09-23 15:37:50 +00:00
|
|
|
|
{
|
|
|
|
|
await fileStream.WriteAsync(bytes, 0, bytes.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the media tools path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="create">if set to <c>true</c> [create].</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string GetMediaToolsPath(bool create)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(_appPaths.ProgramDataPath, "ffmpeg");
|
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
Directory.CreateDirectory(path);
|
2013-09-23 15:37:50 +00:00
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|