FFMpegDownloader.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Model.IO;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.ServerApplication.Implementations
  13. {
  14. public class FFMpegDownloader
  15. {
  16. private readonly IZipClient _zipClient;
  17. private readonly IHttpClient _httpClient;
  18. private readonly IApplicationPaths _appPaths;
  19. private readonly ILogger _logger;
  20. public FFMpegDownloader(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IZipClient zipClient)
  21. {
  22. _logger = logger;
  23. _appPaths = appPaths;
  24. _httpClient = httpClient;
  25. _zipClient = zipClient;
  26. }
  27. public async Task<FFMpegInfo> GetFFMpegInfo()
  28. {
  29. var version = "ffmpeg20130904";
  30. var versionedDirectoryPath = Path.Combine(GetMediaToolsPath(true), version);
  31. var info = new FFMpegInfo
  32. {
  33. ProbePath = Path.Combine(versionedDirectoryPath, "ffprobe.exe"),
  34. Path = Path.Combine(versionedDirectoryPath, "ffmpeg.exe"),
  35. Version = version
  36. };
  37. if (!Directory.Exists(versionedDirectoryPath))
  38. {
  39. Directory.CreateDirectory(versionedDirectoryPath);
  40. }
  41. if (!File.Exists(info.ProbePath) || !File.Exists(info.Path))
  42. {
  43. ExtractTools(version, versionedDirectoryPath);
  44. }
  45. try
  46. {
  47. await DownloadFonts(versionedDirectoryPath).ConfigureAwait(false);
  48. }
  49. catch (Exception ex)
  50. {
  51. _logger.ErrorException("Error getting ffmpeg font files", ex);
  52. }
  53. return info;
  54. }
  55. /// <summary>
  56. /// Extracts the tools.
  57. /// </summary>
  58. /// <param name="assembly">The assembly.</param>
  59. /// <param name="zipFileResourcePath">The zip file resource path.</param>
  60. /// <param name="targetPath">The target path.</param>
  61. private void ExtractTools(string version, string targetPath)
  62. {
  63. var zipFileResourcePath = GetType().Namespace + "." + version + ".zip";
  64. using (var resourceStream = GetType().Assembly.GetManifestResourceStream(zipFileResourcePath))
  65. {
  66. _zipClient.ExtractAll(resourceStream, targetPath, false);
  67. }
  68. }
  69. private const string FontUrl = "https://www.dropbox.com/s/9nb76tybcsw5xrk/ARIALUNI.zip?dl=1";
  70. /// <summary>
  71. /// Extracts the fonts.
  72. /// </summary>
  73. /// <param name="targetPath">The target path.</param>
  74. private async Task DownloadFonts(string targetPath)
  75. {
  76. var fontsDirectory = Path.Combine(targetPath, "fonts");
  77. if (!Directory.Exists(fontsDirectory))
  78. {
  79. Directory.CreateDirectory(fontsDirectory);
  80. }
  81. const string fontFilename = "ARIALUNI.TTF";
  82. var fontFile = Path.Combine(fontsDirectory, fontFilename);
  83. if (!File.Exists(fontFile))
  84. {
  85. await DownloadFontFile(fontsDirectory, fontFilename).ConfigureAwait(false);
  86. }
  87. await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false);
  88. }
  89. /// <summary>
  90. /// Downloads the font file.
  91. /// </summary>
  92. /// <param name="fontsDirectory">The fonts directory.</param>
  93. /// <param name="fontFilename">The font filename.</param>
  94. /// <returns>Task.</returns>
  95. private async Task DownloadFontFile(string fontsDirectory, string fontFilename)
  96. {
  97. var existingFile = Directory
  98. .EnumerateFiles(_appPaths.ProgramDataPath, fontFilename, SearchOption.AllDirectories)
  99. .FirstOrDefault();
  100. if (existingFile != null)
  101. {
  102. try
  103. {
  104. File.Copy(existingFile, Path.Combine(fontsDirectory, fontFilename), true);
  105. return;
  106. }
  107. catch (IOException ex)
  108. {
  109. // Log this, but don't let it fail the operation
  110. _logger.ErrorException("Error copying file", ex);
  111. }
  112. }
  113. var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
  114. {
  115. Url = FontUrl,
  116. Progress = new Progress<double>()
  117. });
  118. _zipClient.ExtractAll(tempFile, fontsDirectory, true);
  119. try
  120. {
  121. File.Delete(tempFile);
  122. }
  123. catch (IOException ex)
  124. {
  125. // Log this, but don't let it fail the operation
  126. _logger.ErrorException("Error deleting temp file {0}", ex, tempFile);
  127. }
  128. }
  129. /// <summary>
  130. /// Writes the font config file.
  131. /// </summary>
  132. /// <param name="fontsDirectory">The fonts directory.</param>
  133. /// <returns>Task.</returns>
  134. private async Task WriteFontConfigFile(string fontsDirectory)
  135. {
  136. const string fontConfigFilename = "fonts.conf";
  137. var fontConfigFile = Path.Combine(fontsDirectory, fontConfigFilename);
  138. if (!File.Exists(fontConfigFile))
  139. {
  140. var contents = string.Format("<?xml version=\"1.0\"?><fontconfig><dir>{0}</dir><alias><family>Arial</family><prefer>Arial Unicode MS</prefer></alias></fontconfig>", fontsDirectory);
  141. var bytes = Encoding.UTF8.GetBytes(contents);
  142. using (var fileStream = new FileStream(fontConfigFile, FileMode.Create, FileAccess.Write,
  143. FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize,
  144. FileOptions.Asynchronous))
  145. {
  146. await fileStream.WriteAsync(bytes, 0, bytes.Length);
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// Gets the media tools path.
  152. /// </summary>
  153. /// <param name="create">if set to <c>true</c> [create].</param>
  154. /// <returns>System.String.</returns>
  155. private string GetMediaToolsPath(bool create)
  156. {
  157. var path = Path.Combine(_appPaths.ProgramDataPath, "ffmpeg");
  158. if (create && !Directory.Exists(path))
  159. {
  160. Directory.CreateDirectory(path);
  161. }
  162. return path;
  163. }
  164. }
  165. public class FFMpegInfo
  166. {
  167. public string Path { get; set; }
  168. public string ProbePath { get; set; }
  169. public string Version { get; set; }
  170. }
  171. }