aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication/Implementations/FFMpegDownloader.cs
blob: 861ca7f3b0204fb000ecfb093461c29eb55ff48f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MediaBrowser.ServerApplication.Implementations
{
    public class FFMpegDownloader
    {
        private readonly IZipClient _zipClient;
        private readonly IHttpClient _httpClient;
        private readonly IApplicationPaths _appPaths;
        private readonly ILogger _logger;

        public FFMpegDownloader(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IZipClient zipClient)
        {
            _logger = logger;
            _appPaths = appPaths;
            _httpClient = httpClient;
            _zipClient = zipClient;
        }

        public async Task<FFMpegInfo> GetFFMpegInfo()
        {
            var version = "ffmpeg20130904";

            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))
            {
                ExtractTools(version, versionedDirectoryPath);
            }

            try
            {
                await DownloadFonts(versionedDirectoryPath).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error getting ffmpeg font files", ex);
            }

            return info;
        }

        /// <summary>
        /// Extracts the tools.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="zipFileResourcePath">The zip file resource path.</param>
        /// <param name="targetPath">The target path.</param>
        private void ExtractTools(string version, string targetPath)
        {
            var zipFileResourcePath = GetType().Namespace + "." + version + ".zip";

            using (var resourceStream = GetType().Assembly.GetManifestResourceStream(zipFileResourcePath))
            {
                _zipClient.ExtractAll(resourceStream, targetPath, false);
            }
        }

        private const string FontUrl = "https://www.dropbox.com/s/9nb76tybcsw5xrk/ARIALUNI.zip?dl=1";

        /// <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>()
            });

            _zipClient.ExtractAll(tempFile, fontsDirectory, true);

            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; }
    }
}