EncodingUtils.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using MediaBrowser.Model.MediaInfo;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace MediaBrowser.MediaEncoding.Encoder
  5. {
  6. public static class EncodingUtils
  7. {
  8. public static string GetInputArgument(List<string> inputFiles, MediaProtocol protocol)
  9. {
  10. if (protocol == MediaProtocol.Http)
  11. {
  12. var url = inputFiles.First();
  13. return string.Format("\"{0}\"", url);
  14. }
  15. if (protocol == MediaProtocol.Rtmp)
  16. {
  17. var url = inputFiles.First();
  18. return string.Format("\"{0}\"", url);
  19. }
  20. if (protocol == MediaProtocol.Rtsp)
  21. {
  22. var url = inputFiles.First();
  23. return string.Format("\"{0}\"", url);
  24. }
  25. if (protocol == MediaProtocol.Udp)
  26. {
  27. var url = inputFiles.First();
  28. return string.Format("\"{0}\"", url);
  29. }
  30. return GetConcatInputArgument(inputFiles);
  31. }
  32. /// <summary>
  33. /// Gets the concat input argument.
  34. /// </summary>
  35. /// <param name="inputFiles">The input files.</param>
  36. /// <returns>System.String.</returns>
  37. private static string GetConcatInputArgument(IReadOnlyList<string> inputFiles)
  38. {
  39. // Get all streams
  40. // If there's more than one we'll need to use the concat command
  41. if (inputFiles.Count > 1)
  42. {
  43. var files = string.Join("|", inputFiles.Select(NormalizePath).ToArray());
  44. return string.Format("concat:\"{0}\"", files);
  45. }
  46. // Determine the input path for video files
  47. return GetFileInputArgument(inputFiles[0]);
  48. }
  49. /// <summary>
  50. /// Gets the file input argument.
  51. /// </summary>
  52. /// <param name="path">The path.</param>
  53. /// <returns>System.String.</returns>
  54. private static string GetFileInputArgument(string path)
  55. {
  56. // Quotes are valid path characters in linux and they need to be escaped here with a leading \
  57. path = NormalizePath(path);
  58. return string.Format("file:\"{0}\"", path);
  59. }
  60. /// <summary>
  61. /// Normalizes the path.
  62. /// </summary>
  63. /// <param name="path">The path.</param>
  64. /// <returns>System.String.</returns>
  65. private static string NormalizePath(string path)
  66. {
  67. // Quotes are valid path characters in linux and they need to be escaped here with a leading \
  68. return path.Replace("\"", "\\\"");
  69. }
  70. public static string GetProbeSizeArgument(int numInputFiles)
  71. {
  72. return numInputFiles > 1 ? "-probesize 1G" : "";
  73. }
  74. public static string GetAnalyzeDurationArgument(int numInputFiles)
  75. {
  76. return numInputFiles > 1 ? "-analyzeduration 200M" : "";
  77. }
  78. }
  79. }