EncodingUtils.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using MediaBrowser.Model.MediaInfo;
  7. namespace MediaBrowser.MediaEncoding.Encoder
  8. {
  9. public static class EncodingUtils
  10. {
  11. public static string GetInputArgument(string inputPrefix, string inputFile, MediaProtocol protocol)
  12. {
  13. if (protocol != MediaProtocol.File)
  14. {
  15. return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", inputFile);
  16. }
  17. return GetFileInputArgument(inputFile, inputPrefix);
  18. }
  19. public static string GetInputArgument(string inputPrefix, IReadOnlyList<string> inputFiles, MediaProtocol protocol)
  20. {
  21. if (protocol != MediaProtocol.File)
  22. {
  23. return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", inputFiles[0]);
  24. }
  25. return GetConcatInputArgument(inputFiles, inputPrefix);
  26. }
  27. /// <summary>
  28. /// Gets the concat input argument.
  29. /// </summary>
  30. /// <param name="inputFiles">The input files.</param>
  31. /// <param name="inputPrefix">The input prefix.</param>
  32. /// <returns>System.String.</returns>
  33. private static string GetConcatInputArgument(IReadOnlyList<string> inputFiles, string inputPrefix)
  34. {
  35. // Get all streams
  36. // If there's more than one we'll need to use the concat command
  37. if (inputFiles.Count > 1)
  38. {
  39. var files = string.Join('|', inputFiles.Select(NormalizePath));
  40. return string.Format(CultureInfo.InvariantCulture, "concat:\"{0}\"", files);
  41. }
  42. // Determine the input path for video files
  43. return GetFileInputArgument(inputFiles[0], inputPrefix);
  44. }
  45. /// <summary>
  46. /// Gets the file input argument.
  47. /// </summary>
  48. /// <param name="path">The path.</param>
  49. /// <param name="inputPrefix">The path prefix.</param>
  50. /// <returns>System.String.</returns>
  51. private static string GetFileInputArgument(string path, string inputPrefix)
  52. {
  53. if (path.Contains("://", StringComparison.Ordinal))
  54. {
  55. return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", path);
  56. }
  57. // Quotes are valid path characters in linux and they need to be escaped here with a leading \
  58. path = NormalizePath(path);
  59. return string.Format(CultureInfo.InvariantCulture, "{1}:\"{0}\"", path, inputPrefix);
  60. }
  61. /// <summary>
  62. /// Normalizes the path.
  63. /// </summary>
  64. /// <param name="path">The path.</param>
  65. /// <returns>System.String.</returns>
  66. public static string NormalizePath(string path)
  67. {
  68. // Quotes are valid path characters in linux and they need to be escaped here with a leading \
  69. return path.Replace("\"", "\\\"", StringComparison.Ordinal);
  70. }
  71. }
  72. }