From e98665a0917f93eba79970de44c68c9a88b78bb8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 12 Oct 2013 23:39:22 -0400 Subject: [PATCH] added ffmpeg downloading for mono --- .../Archiving/ZipClient.cs | 40 ++++++++++++++++ MediaBrowser.Model/IO/IZipClient.cs | 16 +++++++ MediaBrowser.Mono.userprefs | 23 ++++++++-- .../EntryPoints/UserDataChangeNotifier.cs | 2 +- .../FFMpeg/FFMpegDownloadInfo.cs | 20 ++++++++ .../FFMpeg/FFMpegDownloader.cs | 36 --------------- .../MediaBrowser.Server.Mono.csproj | 5 +- .../FFMpeg/FFMpegDownloadInfo.cs | 21 +++++++++ .../FFMpeg/FFMpegDownloader.cs | 46 ++++++++++--------- .../MediaBrowser.ServerApplication.csproj | 1 + 10 files changed, 148 insertions(+), 62 deletions(-) create mode 100644 MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloadInfo.cs delete mode 100644 MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloader.cs create mode 100644 MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloadInfo.cs diff --git a/MediaBrowser.Common.Implementations/Archiving/ZipClient.cs b/MediaBrowser.Common.Implementations/Archiving/ZipClient.cs index 39690eb07..2b66617af 100644 --- a/MediaBrowser.Common.Implementations/Archiving/ZipClient.cs +++ b/MediaBrowser.Common.Implementations/Archiving/ZipClient.cs @@ -1,5 +1,6 @@ using MediaBrowser.Model.IO; using SharpCompress.Archive.SevenZip; +using SharpCompress.Archive.Tar; using SharpCompress.Common; using SharpCompress.Reader; using System.IO; @@ -83,5 +84,44 @@ namespace MediaBrowser.Common.Implementations.Archiving } } } + + + /// + /// Extracts all from tar. + /// + /// The source file. + /// The target path. + /// if set to true [overwrite existing files]. + public void ExtractAllFromTar(string sourceFile, string targetPath, bool overwriteExistingFiles) + { + using (var fileStream = File.OpenRead(sourceFile)) + { + ExtractAllFromTar(fileStream, targetPath, overwriteExistingFiles); + } + } + + /// + /// Extracts all from tar. + /// + /// The source. + /// The target path. + /// if set to true [overwrite existing files]. + public void ExtractAllFromTar(Stream source, string targetPath, bool overwriteExistingFiles) + { + using (var archive = TarArchive.Open(source)) + { + using (var reader = archive.ExtractAllEntries()) + { + var options = ExtractOptions.ExtractFullPath; + + if (overwriteExistingFiles) + { + options = options | ExtractOptions.Overwrite; + } + + reader.WriteAllToDirectory(targetPath, options); + } + } + } } } diff --git a/MediaBrowser.Model/IO/IZipClient.cs b/MediaBrowser.Model/IO/IZipClient.cs index 694c393aa..1fa3e0271 100644 --- a/MediaBrowser.Model/IO/IZipClient.cs +++ b/MediaBrowser.Model/IO/IZipClient.cs @@ -38,5 +38,21 @@ namespace MediaBrowser.Model.IO /// The target path. /// if set to true [overwrite existing files]. void ExtractAllFrom7z(Stream source, string targetPath, bool overwriteExistingFiles); + + /// + /// Extracts all from tar. + /// + /// The source file. + /// The target path. + /// if set to true [overwrite existing files]. + void ExtractAllFromTar(string sourceFile, string targetPath, bool overwriteExistingFiles); + + /// + /// Extracts all from tar. + /// + /// The source. + /// The target path. + /// if set to true [overwrite existing files]. + void ExtractAllFromTar(Stream source, string targetPath, bool overwriteExistingFiles); } } diff --git a/MediaBrowser.Mono.userprefs b/MediaBrowser.Mono.userprefs index 3f004b5b0..3ebeb94d4 100644 --- a/MediaBrowser.Mono.userprefs +++ b/MediaBrowser.Mono.userprefs @@ -1,14 +1,31 @@  - + - - + + + + + + + + + + + + + + + + + + + diff --git a/MediaBrowser.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs b/MediaBrowser.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs index c3766b9fe..c495e424b 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs @@ -88,7 +88,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints } } - private async Task SendNotifications(List>> changes, CancellationToken cancellationToken) + private async Task SendNotifications(IEnumerable>> changes, CancellationToken cancellationToken) { foreach (var pair in changes) { diff --git a/MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloadInfo.cs b/MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloadInfo.cs new file mode 100644 index 000000000..3dc5293e0 --- /dev/null +++ b/MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloadInfo.cs @@ -0,0 +1,20 @@ + +namespace MediaBrowser.ServerApplication.FFMpeg +{ + public static class FFMpegDownloadInfo + { + public static string Version = "ffmpeg20130904"; + + public static string[] FfMpegUrls = new[] + { + "http://ffmpeg.gusari.org/static/32bit/ffmpeg.static.32bit.2013-09-04.tar.gz", + + "https://www.dropbox.com/s/y7f4nk96rxmbb30/ffmpeg.static.32bit.2013-09-04.tar.gz?dl=1" + }; + + public static string FFMpegFilename = "ffmpeg"; + public static string FFProbeFilename = "ffprobe"; + + public static string ArchiveType = "gz"; + } +} diff --git a/MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloader.cs b/MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloader.cs deleted file mode 100644 index cc268ef07..000000000 --- a/MediaBrowser.Server.Mono/FFMpeg/FFMpegDownloader.cs +++ /dev/null @@ -1,36 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.IO; -using MediaBrowser.Common.Net; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Net; -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace MediaBrowser.ServerApplication.FFMpeg -{ - public class FFMpegDownloader - { - private readonly IHttpClient _httpClient; - private readonly IApplicationPaths _appPaths; - private readonly ILogger _logger; - private readonly IZipClient _zipClient; - - public FFMpegDownloader(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IZipClient zipClient) - { - _logger = logger; - _appPaths = appPaths; - _httpClient = httpClient; - _zipClient = zipClient; - } - - public Task GetFFMpegInfo() - { - return Task.FromResult (new FFMpegInfo()); - } - } -} diff --git a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj index 641c30608..de3d2d0cb 100644 --- a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj +++ b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj @@ -74,7 +74,6 @@ - FFMpeg\FFMpegInfo.cs @@ -85,6 +84,10 @@ + + FFMpeg\FFMpegDownloader.cs + + diff --git a/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloadInfo.cs b/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloadInfo.cs new file mode 100644 index 000000000..9fb3391b9 --- /dev/null +++ b/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloadInfo.cs @@ -0,0 +1,21 @@ + +namespace MediaBrowser.ServerApplication.FFMpeg +{ + public static class FFMpegDownloadInfo + { + public static string Version = "ffmpeg20130904.1"; + + public static string[] FfMpegUrls = new[] + { + "https://github.com/MediaBrowser/MediaBrowser.Resources/raw/master/ffmpeg/windows/ffmpeg-20130904-git-f974289-win32-static.7z", + + "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 static string FFMpegFilename = "ffmpeg.exe"; + public static string FFProbeFilename = "ffprobe.exe"; + + public static string ArchiveType = "7z"; + } +} diff --git a/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs b/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs index cf79899c6..e9dee88c6 100644 --- a/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs +++ b/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs @@ -21,21 +21,11 @@ namespace MediaBrowser.ServerApplication.FFMpeg private readonly ILogger _logger; private readonly IZipClient _zipClient; - private const string Version = "ffmpeg20130904.1"; - private readonly string[] _fontUrls = new[] { "https://www.dropbox.com/s/pj847twf7riq0j7/ARIALUNI.7z?dl=1" }; - private readonly string[] _ffMpegUrls = new[] - { - "https://github.com/MediaBrowser/MediaBrowser.Resources/raw/master/ffmpeg/windows/ffmpeg-20130904-git-f974289-win32-static.7z", - - "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, IZipClient zipClient) { _logger = logger; @@ -46,13 +36,13 @@ namespace MediaBrowser.ServerApplication.FFMpeg public async Task GetFFMpegInfo() { - var versionedDirectoryPath = Path.Combine(GetMediaToolsPath(true), Version); + var versionedDirectoryPath = Path.Combine(GetMediaToolsPath(true), FFMpegDownloadInfo.Version); var info = new FFMpegInfo { - ProbePath = Path.Combine(versionedDirectoryPath, "ffprobe.exe"), - Path = Path.Combine(versionedDirectoryPath, "ffmpeg.exe"), - Version = Version + ProbePath = Path.Combine(versionedDirectoryPath, FFMpegDownloadInfo.FFProbeFilename), + Path = Path.Combine(versionedDirectoryPath, FFMpegDownloadInfo.FFMpegFilename), + Version = FFMpegDownloadInfo.Version }; Directory.CreateDirectory(versionedDirectoryPath); @@ -73,7 +63,7 @@ namespace MediaBrowser.ServerApplication.FFMpeg private async Task DownloadFFMpeg(FFMpegInfo info) { - foreach (var url in _ffMpegUrls) + foreach (var url in FFMpegDownloadInfo.FfMpegUrls) { try { @@ -115,11 +105,18 @@ namespace MediaBrowser.ServerApplication.FFMpeg try { - Extract7zArchive(tempFile, tempFolder); + ExtractArchive(tempFile, tempFolder); - var files = Directory.EnumerateFiles(tempFolder, "*.exe", SearchOption.AllDirectories).ToList(); + var files = Directory.EnumerateFiles(tempFolder, "*", SearchOption.AllDirectories).ToList(); - foreach (var file in files.Where(i => i.IndexOf("ffprobe.exe", StringComparison.OrdinalIgnoreCase) != -1 || i.IndexOf("ffmpeg.exe", StringComparison.OrdinalIgnoreCase) != -1)) + foreach (var file in files.Where(i => + { + var filename = Path.GetFileName(i); + + return + string.Equals(filename, FFMpegDownloadInfo.FFProbeFilename, StringComparison.OrdinalIgnoreCase) || + string.Equals(filename, FFMpegDownloadInfo.FFMpegFilename, StringComparison.OrdinalIgnoreCase); + })) { File.Copy(file, Path.Combine(targetFolder, Path.GetFileName(file)), true); } @@ -130,9 +127,16 @@ namespace MediaBrowser.ServerApplication.FFMpeg } } - private void Extract7zArchive(string archivePath, string targetPath) + private void ExtractArchive(string archivePath, string targetPath) { - _zipClient.ExtractAllFrom7z(archivePath, targetPath, true); + 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); + } } private void DeleteFile(string path) @@ -235,7 +239,7 @@ namespace MediaBrowser.ServerApplication.FFMpeg return; } - Extract7zArchive(tempFile, fontsDirectory); + ExtractArchive(tempFile, fontsDirectory); try { diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index f0ede70b2..dbf9109fd 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -186,6 +186,7 @@ Component +