aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding/Encoder/ImageEncoder.cs
blob: e0ca86c41aad8e6823f81c333c803bf6fc88cf60 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Logging;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.MediaEncoding.Encoder
{
    public class ImageEncoder
    {
        private readonly string _ffmpegPath;
        private readonly ILogger _logger;
        private readonly IFileSystem _fileSystem;
        private readonly IApplicationPaths _appPaths;

        private readonly CultureInfo _usCulture = new CultureInfo("en-US");

        private static readonly SemaphoreSlim ResourcePool = new SemaphoreSlim(10, 10);

        public ImageEncoder(string ffmpegPath, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths)
        {
            _ffmpegPath = ffmpegPath;
            _logger = logger;
            _fileSystem = fileSystem;
            _appPaths = appPaths;
        }

        public async Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
        {
            ValidateInput(options);

            await ResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                return await EncodeImageInternal(options, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                ResourcePool.Release();
            }
        }

        private async Task<Stream> EncodeImageInternal(ImageEncodingOptions options, CancellationToken cancellationToken)
        {
            ValidateInput(options);

            var inputPath = options.InputPath;
            var filename = Path.GetFileName(inputPath);

            if (HasDiacritics(filename))
            {
                inputPath = GetTempFile(inputPath);
                filename = Path.GetFileName(inputPath);
            }

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    FileName = _ffmpegPath,
                    Arguments = GetArguments(options, filename),
                    WindowStyle = ProcessWindowStyle.Hidden,
                    ErrorDialog = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    WorkingDirectory = Path.GetDirectoryName(inputPath)
                }
            };

            _logger.Debug("ffmpeg " + process.StartInfo.Arguments);

            process.Start();

            var memoryStream = new MemoryStream();

#pragma warning disable 4014
            // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
            process.StandardOutput.BaseStream.CopyToAsync(memoryStream);
#pragma warning restore 4014

            // MUST read both stdout and stderr asynchronously or a deadlock may occurr
            process.BeginErrorReadLine();

            var ranToCompletion = process.WaitForExit(5000);

            if (!ranToCompletion)
            {
                try
                {
                    _logger.Info("Killing ffmpeg process");

                    process.Kill();

                    process.WaitForExit(1000);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error killing process", ex);
                }
            }

            var exitCode = ranToCompletion ? process.ExitCode : -1;

            process.Dispose();

            if (exitCode == -1 || memoryStream.Length == 0)
            {
                memoryStream.Dispose();

                var msg = string.Format("ffmpeg image encoding failed for {0}", options.InputPath);

                _logger.Error(msg);

                throw new ApplicationException(msg);
            }

            memoryStream.Position = 0;
            return memoryStream;
        }

        private string GetTempFile(string path)
        {
            var extension = Path.GetExtension(path) ?? string.Empty;

            var tempPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N") + extension);

            File.Copy(path, tempPath);

            return tempPath;
        }
        
        private string GetArguments(ImageEncodingOptions options, string inputFilename)
        {
            var vfScale = GetFilterGraph(options);
            var outputFormat = GetOutputFormat(options.Format);

            var quality = (options.Quality ?? 100) * .3;
            quality = 31 - quality;
            var qualityValue = Convert.ToInt32(Math.Max(quality, 1));

            return string.Format("-f image2 -i file:\"{3}\" -q:v {0} {1} -f image2pipe -vcodec {2} -",
                qualityValue.ToString(_usCulture),
                vfScale,
                outputFormat,
                inputFilename);
        }

        private string GetFilterGraph(ImageEncodingOptions options)
        {
            if (!options.Width.HasValue &&
                !options.Height.HasValue &&
                !options.MaxHeight.HasValue &&
                !options.MaxWidth.HasValue)
            {
                return string.Empty;
            }

            var widthScale = "-1";
            var heightScale = "-1";

            if (options.MaxWidth.HasValue)
            {
                widthScale = "min(iw\\," + options.MaxWidth.Value.ToString(_usCulture) + ")";
            }
            else if (options.Width.HasValue)
            {
                widthScale = options.Width.Value.ToString(_usCulture);
            }

            if (options.MaxHeight.HasValue)
            {
                heightScale = "min(ih\\," + options.MaxHeight.Value.ToString(_usCulture) + ")";
            }
            else if (options.Height.HasValue)
            {
                heightScale = options.Height.Value.ToString(_usCulture);
            }

            var scaleMethod = "lanczos";

            return string.Format("-vf scale=\"{0}:{1}\"",
                widthScale,
                heightScale);
        }

        private string GetOutputFormat(string format)
        {
            if (string.Equals(format, "jpeg", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(format, "jpg", StringComparison.OrdinalIgnoreCase))
            {
                return "mjpeg";
            }
            return format;
        }

        private void ValidateInput(ImageEncodingOptions options)
        {

        }

        /// <summary>
        /// Determines whether the specified text has diacritics.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns><c>true</c> if the specified text has diacritics; otherwise, <c>false</c>.</returns>
        private bool HasDiacritics(string text)
        {
            return !String.Equals(text, RemoveDiacritics(text), StringComparison.Ordinal);
        }

        /// <summary>
        /// Removes the diacritics.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns>System.String.</returns>
        private string RemoveDiacritics(string text)
        {
            return String.Concat(
                text.Normalize(NormalizationForm.FormD)
                .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
                                              UnicodeCategory.NonSpacingMark)
              ).Normalize(NormalizationForm.FormC);
        }
    }
}