FFMpegDownloader.cs 6.7 KB

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