EncodingUtils.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using MediaBrowser.Controller.MediaEncoding;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. namespace MediaBrowser.MediaEncoding.Encoder
  9. {
  10. public static class EncodingUtils
  11. {
  12. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  13. public static string GetInputArgument(List<string> inputFiles, bool isRemote)
  14. {
  15. if (isRemote)
  16. {
  17. return GetHttpInputArgument(inputFiles);
  18. }
  19. return GetConcatInputArgument(inputFiles);
  20. }
  21. /// <summary>
  22. /// Gets the concat input argument.
  23. /// </summary>
  24. /// <param name="inputFiles">The input files.</param>
  25. /// <returns>System.String.</returns>
  26. private static string GetConcatInputArgument(IReadOnlyList<string> inputFiles)
  27. {
  28. // Get all streams
  29. // If there's more than one we'll need to use the concat command
  30. if (inputFiles.Count > 1)
  31. {
  32. var files = string.Join("|", inputFiles);
  33. return string.Format("concat:\"{0}\"", files);
  34. }
  35. // Determine the input path for video files
  36. return GetFileInputArgument(inputFiles[0]);
  37. }
  38. /// <summary>
  39. /// Gets the file input argument.
  40. /// </summary>
  41. /// <param name="path">The path.</param>
  42. /// <returns>System.String.</returns>
  43. private static string GetFileInputArgument(string path)
  44. {
  45. return string.Format("file:\"{0}\"", path);
  46. }
  47. /// <summary>
  48. /// Gets the HTTP input argument.
  49. /// </summary>
  50. /// <param name="inputFiles">The input files.</param>
  51. /// <returns>System.String.</returns>
  52. private static string GetHttpInputArgument(IEnumerable<string> inputFiles)
  53. {
  54. var url = inputFiles.First();
  55. return string.Format("\"{0}\"", url);
  56. }
  57. private static string GetFastSeekValue(EncodingOptions options)
  58. {
  59. var time = options.StartTimeTicks;
  60. if (time.HasValue)
  61. {
  62. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds;
  63. if (seconds > 0)
  64. {
  65. return string.Format("-ss {0}", seconds.ToString(UsCulture));
  66. }
  67. }
  68. return string.Empty;
  69. }
  70. public static string GetProbeSizeArgument(bool isDvd)
  71. {
  72. return isDvd ? "-probesize 1G -analyzeduration 200M" : string.Empty;
  73. }
  74. /// <summary>
  75. /// Gets the number of audio channels to specify on the command line
  76. /// </summary>
  77. /// <param name="request">The request.</param>
  78. /// <param name="audioStream">The audio stream.</param>
  79. /// <returns>System.Nullable{System.Int32}.</returns>
  80. public static int? GetNumAudioChannelsParam(EncodingOptions request, MediaStream audioStream)
  81. {
  82. if (audioStream != null)
  83. {
  84. var codec = request.AudioCodec ?? string.Empty;
  85. if (audioStream.Channels > 2 && codec.IndexOf("wma", StringComparison.OrdinalIgnoreCase) != -1)
  86. {
  87. // wmav2 currently only supports two channel output
  88. return 2;
  89. }
  90. }
  91. if (request.MaxAudioChannels.HasValue)
  92. {
  93. if (audioStream != null && audioStream.Channels.HasValue)
  94. {
  95. return Math.Min(request.MaxAudioChannels.Value, audioStream.Channels.Value);
  96. }
  97. return request.MaxAudioChannels.Value;
  98. }
  99. return request.AudioChannels;
  100. }
  101. }
  102. }