EncodingUtils.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(IReadOnlyList<string> inputFiles, MediaProtocol protocol)
  12. {
  13. if (protocol != MediaProtocol.File)
  14. {
  15. var url = inputFiles[0];
  16. return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", url);
  17. }
  18. return GetConcatInputArgument(inputFiles);
  19. }
  20. /// <summary>
  21. /// Gets the concat input argument.
  22. /// </summary>
  23. /// <param name="inputFiles">The input files.</param>
  24. /// <returns>System.String.</returns>
  25. private static string GetConcatInputArgument(IReadOnlyList<string> inputFiles)
  26. {
  27. // Get all streams
  28. // If there's more than one we'll need to use the concat command
  29. if (inputFiles.Count > 1)
  30. {
  31. var files = string.Join("|", inputFiles.Select(NormalizePath));
  32. return string.Format(CultureInfo.InvariantCulture, "concat:\"{0}\"", files);
  33. }
  34. // Determine the input path for video files
  35. return GetFileInputArgument(inputFiles[0]);
  36. }
  37. /// <summary>
  38. /// Gets the file input argument.
  39. /// </summary>
  40. /// <param name="path">The path.</param>
  41. /// <returns>System.String.</returns>
  42. private static string GetFileInputArgument(string path)
  43. {
  44. if (path.IndexOf("://", StringComparison.Ordinal) != -1)
  45. {
  46. return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", path);
  47. }
  48. // Quotes are valid path characters in linux and they need to be escaped here with a leading \
  49. path = NormalizePath(path);
  50. return string.Format(CultureInfo.InvariantCulture, "file:\"{0}\"", path);
  51. }
  52. /// <summary>
  53. /// Normalizes the path.
  54. /// </summary>
  55. /// <param name="path">The path.</param>
  56. /// <returns>System.String.</returns>
  57. private static string NormalizePath(string path)
  58. {
  59. // Quotes are valid path characters in linux and they need to be escaped here with a leading \
  60. return path.Replace("\"", "\\\"", StringComparison.Ordinal);
  61. }
  62. }
  63. }