jellyfin/MediaBrowser.ServerApplication/Implementations/FFMpegDownloader.cs

277 lines
9 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using SharpCompress.Archive.SevenZip;
using SharpCompress.Common;
using SharpCompress.Reader;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.ServerApplication.Implementations
{
public class FFMpegDownloader
{
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _appPaths;
private readonly ILogger _logger;
private const string Version = "ffmpeg20130904";
private const string FontUrl = "https://www.dropbox.com/s/pj847twf7riq0j7/ARIALUNI.7z?dl=1";
private readonly string[] _ffMpegUrls = new[]
{
"http://ffmpeg.zeranoe.com/builds/win32/static/ffmpeg-20130904-git-f974289-win32-static.7z",
"https://www.dropbox.com/s/a81cb2ob23fwcfs/ffmpeg-20130904-git-f974289-win32-static.7z?dl=1"
};
public FFMpegDownloader(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient)
{
_logger = logger;
_appPaths = appPaths;
_httpClient = httpClient;
}
public async Task<FFMpegInfo> GetFFMpegInfo()
{
var versionedDirectoryPath = Path.Combine(GetMediaToolsPath(true), Version);
var info = new FFMpegInfo
{
ProbePath = Path.Combine(versionedDirectoryPath, "ffprobe.exe"),
Path = Path.Combine(versionedDirectoryPath, "ffmpeg.exe"),
Version = Version
};
if (!Directory.Exists(versionedDirectoryPath))
{
Directory.CreateDirectory(versionedDirectoryPath);
}
if (!File.Exists(info.ProbePath) || !File.Exists(info.Path))
{
await DownloadFFMpeg(info).ConfigureAwait(false);
}
try
{
await DownloadFonts(versionedDirectoryPath).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error getting ffmpeg font files", ex);
}
return info;
}
private async Task DownloadFFMpeg(FFMpegInfo info)
{
foreach (var url in _ffMpegUrls)
{
try
{
var tempFile = await DownloadFFMpeg(info, url).ConfigureAwait(false);
ExtractFFMpeg(tempFile, Path.GetDirectoryName(info.Path));
return;
}
catch (HttpException ex)
{
}
}
}
private Task<string> DownloadFFMpeg(FFMpegInfo info, string url)
{
return _httpClient.GetTempFile(new HttpRequestOptions
{
Url = url,
CancellationToken = CancellationToken.None,
Progress = new Progress<double>(),
// Make it look like a browser
// Try to hide that we're direct linking
UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.47 Safari/537.36"
});
}
private void ExtractFFMpeg(string tempFile, string targetFolder)
{
_logger.Debug("Extracting ffmpeg from {0}", tempFile);
var tempFolder = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString());
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
try
{
Extract7zArchive(tempFile, tempFolder);
var files = Directory.EnumerateFiles(tempFolder, "*.exe", SearchOption.AllDirectories).ToList();
foreach (var file in files)
{
File.Copy(file, Path.Combine(targetFolder, Path.GetFileName(file)));
}
}
finally
{
DeleteFile(tempFile);
}
}
private void Extract7zArchive(string archivePath, string targetPath)
{
using (var archive = SevenZipArchive.Open(archivePath))
{
using (var reader = archive.ExtractAllEntries())
{
reader.WriteAllToDirectory(targetPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
}
}
private void DeleteFile(string path)
{
try
{
File.Delete(path);
}
catch (IOException ex)
{
_logger.ErrorException("Error deleting temp file {0}", ex, path);
}
}
/// <summary>
/// Extracts the fonts.
/// </summary>
/// <param name="targetPath">The target path.</param>
private async Task DownloadFonts(string targetPath)
{
var fontsDirectory = Path.Combine(targetPath, "fonts");
if (!Directory.Exists(fontsDirectory))
{
Directory.CreateDirectory(fontsDirectory);
}
const string fontFilename = "ARIALUNI.TTF";
var fontFile = Path.Combine(fontsDirectory, fontFilename);
if (!File.Exists(fontFile))
{
await DownloadFontFile(fontsDirectory, fontFilename).ConfigureAwait(false);
}
await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false);
}
/// <summary>
/// Downloads the font file.
/// </summary>
/// <param name="fontsDirectory">The fonts directory.</param>
/// <param name="fontFilename">The font filename.</param>
/// <returns>Task.</returns>
private async Task DownloadFontFile(string fontsDirectory, string fontFilename)
{
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);
}
}
var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
{
Url = FontUrl,
Progress = new Progress<double>()
});
Extract7zArchive(tempFile, fontsDirectory);
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);
using (var fileStream = new FileStream(fontConfigFile, FileMode.Create, FileAccess.Write,
FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize,
FileOptions.Asynchronous))
{
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");
if (create && !Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
}
public class FFMpegInfo
{
public string Path { get; set; }
public string ProbePath { get; set; }
public string Version { get; set; }
}
}