BaseStreamingService.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.MediaInfo;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Dto;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Entities.Audio;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.MediaInfo;
  11. using MediaBrowser.Controller.Persistence;
  12. using MediaBrowser.Model.Configuration;
  13. using MediaBrowser.Model.Drawing;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.IO;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Diagnostics;
  20. using System.Globalization;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. namespace MediaBrowser.Api.Playback
  26. {
  27. /// <summary>
  28. /// Class BaseStreamingService
  29. /// </summary>
  30. public abstract class BaseStreamingService : BaseApiService
  31. {
  32. /// <summary>
  33. /// Gets or sets the application paths.
  34. /// </summary>
  35. /// <value>The application paths.</value>
  36. protected IServerConfigurationManager ServerConfigurationManager { get; private set; }
  37. /// <summary>
  38. /// Gets or sets the user manager.
  39. /// </summary>
  40. /// <value>The user manager.</value>
  41. protected IUserManager UserManager { get; private set; }
  42. /// <summary>
  43. /// Gets or sets the library manager.
  44. /// </summary>
  45. /// <value>The library manager.</value>
  46. protected ILibraryManager LibraryManager { get; private set; }
  47. /// <summary>
  48. /// Gets or sets the iso manager.
  49. /// </summary>
  50. /// <value>The iso manager.</value>
  51. protected IIsoManager IsoManager { get; private set; }
  52. /// <summary>
  53. /// Gets or sets the media encoder.
  54. /// </summary>
  55. /// <value>The media encoder.</value>
  56. protected IMediaEncoder MediaEncoder { get; private set; }
  57. protected IDtoService DtoService { get; private set; }
  58. protected IFileSystem FileSystem { get; private set; }
  59. protected IItemRepository ItemRepository { get; private set; }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  62. /// </summary>
  63. /// <param name="serverConfig">The server configuration.</param>
  64. /// <param name="userManager">The user manager.</param>
  65. /// <param name="libraryManager">The library manager.</param>
  66. /// <param name="isoManager">The iso manager.</param>
  67. /// <param name="mediaEncoder">The media encoder.</param>
  68. /// <param name="dtoService">The dto service.</param>
  69. /// <param name="fileSystem">The file system.</param>
  70. /// <param name="itemRepository">The item repository.</param>
  71. protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository)
  72. {
  73. ItemRepository = itemRepository;
  74. FileSystem = fileSystem;
  75. DtoService = dtoService;
  76. ServerConfigurationManager = serverConfig;
  77. UserManager = userManager;
  78. LibraryManager = libraryManager;
  79. IsoManager = isoManager;
  80. MediaEncoder = mediaEncoder;
  81. }
  82. /// <summary>
  83. /// Gets the command line arguments.
  84. /// </summary>
  85. /// <param name="outputPath">The output path.</param>
  86. /// <param name="state">The state.</param>
  87. /// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
  88. /// <returns>System.String.</returns>
  89. protected abstract string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions);
  90. /// <summary>
  91. /// Gets the type of the transcoding job.
  92. /// </summary>
  93. /// <value>The type of the transcoding job.</value>
  94. protected abstract TranscodingJobType TranscodingJobType { get; }
  95. /// <summary>
  96. /// Gets the output file extension.
  97. /// </summary>
  98. /// <param name="state">The state.</param>
  99. /// <returns>System.String.</returns>
  100. protected virtual string GetOutputFileExtension(StreamState state)
  101. {
  102. return Path.GetExtension(state.Url);
  103. }
  104. /// <summary>
  105. /// Gets the output file path.
  106. /// </summary>
  107. /// <param name="state">The state.</param>
  108. /// <returns>System.String.</returns>
  109. protected virtual string GetOutputFilePath(StreamState state)
  110. {
  111. var folder = ServerConfigurationManager.ApplicationPaths.EncodedMediaCachePath;
  112. var outputFileExtension = GetOutputFileExtension(state);
  113. return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
  114. }
  115. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  116. /// <summary>
  117. /// The fast seek offset seconds
  118. /// </summary>
  119. private const int FastSeekOffsetSeconds = 1;
  120. /// <summary>
  121. /// Gets the fast seek command line parameter.
  122. /// </summary>
  123. /// <param name="request">The request.</param>
  124. /// <returns>System.String.</returns>
  125. /// <value>The fast seek command line parameter.</value>
  126. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  127. {
  128. var time = request.StartTimeTicks;
  129. if (time.HasValue)
  130. {
  131. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds;
  132. if (seconds > 0)
  133. {
  134. return string.Format("-ss {0}", seconds.ToString(UsCulture));
  135. }
  136. }
  137. return string.Empty;
  138. }
  139. /// <summary>
  140. /// Gets the slow seek command line parameter.
  141. /// </summary>
  142. /// <param name="request">The request.</param>
  143. /// <returns>System.String.</returns>
  144. /// <value>The slow seek command line parameter.</value>
  145. protected string GetSlowSeekCommandLineParameter(StreamRequest request)
  146. {
  147. var time = request.StartTimeTicks;
  148. if (time.HasValue)
  149. {
  150. if (TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds > 0)
  151. {
  152. return string.Format(" -ss {0}", FastSeekOffsetSeconds.ToString(UsCulture));
  153. }
  154. }
  155. return string.Empty;
  156. }
  157. /// <summary>
  158. /// Gets the map args.
  159. /// </summary>
  160. /// <param name="state">The state.</param>
  161. /// <returns>System.String.</returns>
  162. protected virtual string GetMapArgs(StreamState state)
  163. {
  164. var args = string.Empty;
  165. if (state.Item.LocationType == LocationType.Remote)
  166. {
  167. return string.Empty;
  168. }
  169. if (state.VideoStream != null)
  170. {
  171. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  172. }
  173. else
  174. {
  175. args += "-map -0:v";
  176. }
  177. if (state.AudioStream != null)
  178. {
  179. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  180. }
  181. else
  182. {
  183. args += " -map -0:a";
  184. }
  185. if (state.SubtitleStream == null)
  186. {
  187. args += " -map -0:s";
  188. }
  189. return args;
  190. }
  191. /// <summary>
  192. /// Determines which stream will be used for playback
  193. /// </summary>
  194. /// <param name="allStream">All stream.</param>
  195. /// <param name="desiredIndex">Index of the desired.</param>
  196. /// <param name="type">The type.</param>
  197. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  198. /// <returns>MediaStream.</returns>
  199. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  200. {
  201. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  202. if (desiredIndex.HasValue)
  203. {
  204. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  205. if (stream != null)
  206. {
  207. return stream;
  208. }
  209. }
  210. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  211. {
  212. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  213. streams.FirstOrDefault();
  214. }
  215. // Just return the first one
  216. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  217. }
  218. /// <summary>
  219. /// Gets the number of threads.
  220. /// </summary>
  221. /// <returns>System.Int32.</returns>
  222. /// <exception cref="System.Exception">Unrecognized EncodingQuality value.</exception>
  223. protected int GetNumberOfThreads()
  224. {
  225. var quality = ServerConfigurationManager.Configuration.EncodingQuality;
  226. if (quality == EncodingQuality.Auto)
  227. {
  228. var cpuCount = Environment.ProcessorCount;
  229. if (cpuCount >= 4)
  230. {
  231. return 0;
  232. }
  233. return cpuCount;
  234. }
  235. switch (quality)
  236. {
  237. case EncodingQuality.HighSpeed:
  238. return 2;
  239. case EncodingQuality.HighQuality:
  240. return 2;
  241. case EncodingQuality.MaxQuality:
  242. return 0;
  243. default:
  244. throw new Exception("Unrecognized EncodingQuality value.");
  245. }
  246. }
  247. /// <summary>
  248. /// Gets the video bitrate to specify on the command line
  249. /// </summary>
  250. /// <param name="state">The state.</param>
  251. /// <param name="videoCodec">The video codec.</param>
  252. /// <returns>System.String.</returns>
  253. protected string GetVideoQualityParam(StreamState state, string videoCodec)
  254. {
  255. var args = string.Empty;
  256. // webm
  257. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  258. {
  259. args = "-speed 16 -quality good -profile:v 0 -slices 8";
  260. }
  261. // asf/wmv
  262. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  263. {
  264. args = "-g 100 -qmax 15";
  265. }
  266. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  267. {
  268. args = "-preset superfast";
  269. }
  270. else if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase))
  271. {
  272. args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  273. }
  274. return args.Trim();
  275. }
  276. /// <summary>
  277. /// If we're going to put a fixed size on the command line, this will calculate it
  278. /// </summary>
  279. /// <param name="state">The state.</param>
  280. /// <param name="outputVideoCodec">The output video codec.</param>
  281. /// <param name="performTextSubtitleConversion">if set to <c>true</c> [perform text subtitle conversion].</param>
  282. /// <returns>System.String.</returns>
  283. protected string GetOutputSizeParam(StreamState state, string outputVideoCodec, bool performTextSubtitleConversion)
  284. {
  285. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  286. var assSubtitleParam = string.Empty;
  287. var request = state.VideoRequest;
  288. if (state.SubtitleStream != null)
  289. {
  290. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 ||
  291. state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1 ||
  292. string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  293. string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase))
  294. {
  295. assSubtitleParam = GetTextSubtitleParam((Video)state.Item, state.SubtitleStream, request.StartTimeTicks, performTextSubtitleConversion);
  296. }
  297. }
  298. // If fixed dimensions were supplied
  299. if (request.Width.HasValue && request.Height.HasValue)
  300. {
  301. return string.Format(" -vf \"scale=trunc({0}/2)*2:trunc({1}/2)*2{2}\"", request.Width.Value, request.Height.Value, assSubtitleParam);
  302. }
  303. var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
  304. // If a fixed width was requested
  305. if (request.Width.HasValue)
  306. {
  307. return isH264Output ?
  308. string.Format(" -vf \"scale={0}:trunc(ow/a/2)*2{1}\"", request.Width.Value, assSubtitleParam) :
  309. string.Format(" -vf \"scale={0}:-1{1}\"", request.Width.Value, assSubtitleParam);
  310. }
  311. // If a fixed height was requested
  312. if (request.Height.HasValue)
  313. {
  314. return isH264Output ?
  315. string.Format(" -vf \"scale=trunc(oh*a*2)/2:{0}{1}\"", request.Height.Value, assSubtitleParam) :
  316. string.Format(" -vf \"scale=-1:{0}{1}\"", request.Height.Value, assSubtitleParam);
  317. }
  318. // If a max width was requested
  319. if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null))
  320. {
  321. return isH264Output ?
  322. string.Format(" -vf \"scale=min(iw\\,{0}):trunc(ow/a/2)*2{1}\"", request.MaxWidth.Value, assSubtitleParam) :
  323. string.Format(" -vf \"scale=min(iw\\,{0}):-1{1}\"", request.MaxWidth.Value, assSubtitleParam);
  324. }
  325. // If a max height was requested
  326. if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null))
  327. {
  328. return isH264Output ?
  329. string.Format(" -vf \"scale=trunc(oh*a*2)/2:min(ih\\,{0}){1}\"", request.MaxHeight.Value, assSubtitleParam) :
  330. string.Format(" -vf \"scale=-1:min(ih\\,{0}){1}\"", request.MaxHeight.Value, assSubtitleParam);
  331. }
  332. if (state.VideoStream == null)
  333. {
  334. // No way to figure this out
  335. return string.Empty;
  336. }
  337. // Need to perform calculations manually
  338. // Try to account for bad media info
  339. var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0;
  340. var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0;
  341. var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight);
  342. // If we're encoding with libx264, it can't handle odd numbered widths or heights, so we'll have to fix that
  343. if (isH264Output)
  344. {
  345. return string.Format(" -vf \"scale=trunc({0}/2)*2:trunc({1}/2)*2{2}\"", outputSize.Width, outputSize.Height, assSubtitleParam);
  346. }
  347. // Otherwise use -vf scale since ffmpeg will ensure internally that the aspect ratio is preserved
  348. return string.Format(" -vf \"scale={0}:-1{1}\"", Convert.ToInt32(outputSize.Width), assSubtitleParam);
  349. }
  350. /// <summary>
  351. /// Gets the text subtitle param.
  352. /// </summary>
  353. /// <param name="video">The video.</param>
  354. /// <param name="subtitleStream">The subtitle stream.</param>
  355. /// <param name="startTimeTicks">The start time ticks.</param>
  356. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  357. /// <returns>System.String.</returns>
  358. protected string GetTextSubtitleParam(Video video, MediaStream subtitleStream, long? startTimeTicks, bool performConversion)
  359. {
  360. var path = subtitleStream.IsExternal ? GetConvertedAssPath(video, subtitleStream, startTimeTicks, performConversion) : GetExtractedAssPath(video, subtitleStream, startTimeTicks, performConversion);
  361. if (string.IsNullOrEmpty(path))
  362. {
  363. return string.Empty;
  364. }
  365. return string.Format(",ass='{0}'", path.Replace('\\', '/').Replace(":/", "\\:/"));
  366. }
  367. /// <summary>
  368. /// Gets the extracted ass path.
  369. /// </summary>
  370. /// <param name="video">The video.</param>
  371. /// <param name="subtitleStream">The subtitle stream.</param>
  372. /// <param name="startTimeTicks">The start time ticks.</param>
  373. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  374. /// <returns>System.String.</returns>
  375. private string GetExtractedAssPath(Video video, MediaStream subtitleStream, long? startTimeTicks, bool performConversion)
  376. {
  377. var offset = TimeSpan.FromTicks(startTimeTicks ?? 0);
  378. var path = FFMpegManager.Instance.GetSubtitleCachePath(video, subtitleStream.Index, offset, ".ass");
  379. if (performConversion)
  380. {
  381. InputType type;
  382. var inputPath = MediaEncoderHelpers.GetInputArgument(video, null, out type);
  383. try
  384. {
  385. var parentPath = Path.GetDirectoryName(path);
  386. Directory.CreateDirectory(parentPath);
  387. var task = MediaEncoder.ExtractTextSubtitle(inputPath, type, subtitleStream.Index, offset, path, CancellationToken.None);
  388. Task.WaitAll(task);
  389. }
  390. catch
  391. {
  392. return null;
  393. }
  394. }
  395. return path;
  396. }
  397. /// <summary>
  398. /// Gets the converted ass path.
  399. /// </summary>
  400. /// <param name="video">The video.</param>
  401. /// <param name="subtitleStream">The subtitle stream.</param>
  402. /// <param name="startTimeTicks">The start time ticks.</param>
  403. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  404. /// <returns>System.String.</returns>
  405. private string GetConvertedAssPath(Video video, MediaStream subtitleStream, long? startTimeTicks, bool performConversion)
  406. {
  407. // If it's already ass, no conversion neccessary
  408. //if (string.Equals(Path.GetExtension(subtitleStream.Path), ".ass", StringComparison.OrdinalIgnoreCase))
  409. //{
  410. // return subtitleStream.Path;
  411. //}
  412. var offset = TimeSpan.FromTicks(startTimeTicks ?? 0);
  413. var path = FFMpegManager.Instance.GetSubtitleCachePath(video, subtitleStream.Index, offset, ".ass");
  414. if (performConversion)
  415. {
  416. try
  417. {
  418. var parentPath = Path.GetDirectoryName(path);
  419. Directory.CreateDirectory(parentPath);
  420. var task = MediaEncoder.ConvertTextSubtitleToAss(subtitleStream.Path, path, subtitleStream.Language, offset, CancellationToken.None);
  421. Task.WaitAll(task);
  422. }
  423. catch
  424. {
  425. return null;
  426. }
  427. }
  428. return path;
  429. }
  430. /// <summary>
  431. /// Gets the internal graphical subtitle param.
  432. /// </summary>
  433. /// <param name="state">The state.</param>
  434. /// <param name="outputVideoCodec">The output video codec.</param>
  435. /// <returns>System.String.</returns>
  436. protected string GetInternalGraphicalSubtitleParam(StreamState state, string outputVideoCodec)
  437. {
  438. var outputSizeParam = string.Empty;
  439. var request = state.VideoRequest;
  440. // Add resolution params, if specified
  441. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  442. {
  443. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec, false).TrimEnd('"');
  444. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  445. }
  446. return string.Format(" -filter_complex \"[0:{0}]format=yuva444p,lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{1}] [sub] overlay{2}\"", state.SubtitleStream.Index, state.VideoStream.Index, outputSizeParam);
  447. }
  448. /// <summary>
  449. /// Gets the probe size argument.
  450. /// </summary>
  451. /// <param name="item">The item.</param>
  452. /// <returns>System.String.</returns>
  453. protected string GetProbeSizeArgument(BaseItem item)
  454. {
  455. var type = InputType.AudioFile;
  456. if (item is Audio)
  457. {
  458. type = MediaEncoderHelpers.GetInputType(item.Path, null, null);
  459. }
  460. else
  461. {
  462. var video = item as Video;
  463. if (video != null)
  464. {
  465. type = MediaEncoderHelpers.GetInputType(item.Path, video.VideoType, video.IsoType);
  466. }
  467. }
  468. return MediaEncoder.GetProbeSizeArgument(type);
  469. }
  470. /// <summary>
  471. /// Gets the number of audio channels to specify on the command line
  472. /// </summary>
  473. /// <param name="request">The request.</param>
  474. /// <param name="audioStream">The audio stream.</param>
  475. /// <returns>System.Nullable{System.Int32}.</returns>
  476. protected int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream)
  477. {
  478. if (audioStream != null)
  479. {
  480. if (audioStream.Channels > 2 && request.AudioCodec.HasValue)
  481. {
  482. if (request.AudioCodec.Value == AudioCodecs.Wma)
  483. {
  484. // wmav2 currently only supports two channel output
  485. return 2;
  486. }
  487. }
  488. }
  489. return request.AudioChannels;
  490. }
  491. /// <summary>
  492. /// Determines whether the specified stream is H264.
  493. /// </summary>
  494. /// <param name="stream">The stream.</param>
  495. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  496. protected bool IsH264(MediaStream stream)
  497. {
  498. return stream.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  499. stream.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  500. }
  501. /// <summary>
  502. /// Gets the name of the output audio codec
  503. /// </summary>
  504. /// <param name="request">The request.</param>
  505. /// <returns>System.String.</returns>
  506. protected string GetAudioCodec(StreamRequest request)
  507. {
  508. var codec = request.AudioCodec;
  509. if (codec.HasValue)
  510. {
  511. if (codec == AudioCodecs.Aac)
  512. {
  513. return "aac -strict experimental";
  514. }
  515. if (codec == AudioCodecs.Mp3)
  516. {
  517. return "libmp3lame";
  518. }
  519. if (codec == AudioCodecs.Vorbis)
  520. {
  521. return "libvorbis";
  522. }
  523. if (codec == AudioCodecs.Wma)
  524. {
  525. return "wmav2";
  526. }
  527. return codec.ToString().ToLower();
  528. }
  529. return "copy";
  530. }
  531. /// <summary>
  532. /// Gets the name of the output video codec
  533. /// </summary>
  534. /// <param name="request">The request.</param>
  535. /// <returns>System.String.</returns>
  536. protected string GetVideoCodec(VideoStreamRequest request)
  537. {
  538. var codec = request.VideoCodec;
  539. if (codec.HasValue)
  540. {
  541. if (codec == VideoCodecs.H264)
  542. {
  543. return "libx264";
  544. }
  545. if (codec == VideoCodecs.Vpx)
  546. {
  547. return "libvpx";
  548. }
  549. if (codec == VideoCodecs.Wmv)
  550. {
  551. return "wmv2";
  552. }
  553. if (codec == VideoCodecs.Theora)
  554. {
  555. return "libtheora";
  556. }
  557. return codec.ToString().ToLower();
  558. }
  559. return "copy";
  560. }
  561. /// <summary>
  562. /// Gets the input argument.
  563. /// </summary>
  564. /// <param name="item">The item.</param>
  565. /// <param name="isoMount">The iso mount.</param>
  566. /// <returns>System.String.</returns>
  567. protected string GetInputArgument(BaseItem item, IIsoMount isoMount)
  568. {
  569. var type = InputType.AudioFile;
  570. var inputPath = new[] { item.Path };
  571. var video = item as Video;
  572. if (video != null)
  573. {
  574. if (!(video.VideoType == VideoType.Iso && isoMount == null))
  575. {
  576. inputPath = MediaEncoderHelpers.GetInputArgument(video, isoMount, out type);
  577. }
  578. }
  579. return MediaEncoder.GetInputArgument(inputPath, type);
  580. }
  581. /// <summary>
  582. /// Starts the FFMPEG.
  583. /// </summary>
  584. /// <param name="state">The state.</param>
  585. /// <param name="outputPath">The output path.</param>
  586. /// <returns>Task.</returns>
  587. protected async Task StartFfMpeg(StreamState state, string outputPath)
  588. {
  589. var parentPath = Path.GetDirectoryName(outputPath);
  590. Directory.CreateDirectory(parentPath);
  591. var video = state.Item as Video;
  592. if (video != null && video.VideoType == VideoType.Iso && video.IsoType.HasValue && IsoManager.CanMount(video.Path))
  593. {
  594. state.IsoMount = await IsoManager.Mount(video.Path, CancellationToken.None).ConfigureAwait(false);
  595. }
  596. var process = new Process
  597. {
  598. StartInfo = new ProcessStartInfo
  599. {
  600. CreateNoWindow = true,
  601. UseShellExecute = false,
  602. // Must consume both stdout and stderr or deadlocks may occur
  603. RedirectStandardOutput = true,
  604. RedirectStandardError = true,
  605. FileName = MediaEncoder.EncoderPath,
  606. WorkingDirectory = Path.GetDirectoryName(MediaEncoder.EncoderPath),
  607. Arguments = GetCommandLineArguments(outputPath, state, true),
  608. WindowStyle = ProcessWindowStyle.Hidden,
  609. ErrorDialog = false
  610. },
  611. EnableRaisingEvents = true
  612. };
  613. ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, video != null, state.Request.StartTimeTicks, state.Item.Path, state.Request.DeviceId);
  614. Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
  615. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");
  616. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  617. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);
  618. process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);
  619. try
  620. {
  621. process.Start();
  622. }
  623. catch (Exception ex)
  624. {
  625. Logger.ErrorException("Error starting ffmpeg", ex);
  626. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
  627. state.LogFileStream.Dispose();
  628. throw;
  629. }
  630. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  631. process.BeginOutputReadLine();
  632. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  633. process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);
  634. // Wait for the file to exist before proceeeding
  635. while (!File.Exists(outputPath))
  636. {
  637. await Task.Delay(100).ConfigureAwait(false);
  638. }
  639. // Allow a small amount of time to buffer a little
  640. if (state.Item is Video)
  641. {
  642. await Task.Delay(500).ConfigureAwait(false);
  643. }
  644. // This is arbitrary, but add a little buffer time when internet streaming
  645. if (state.Item.LocationType == LocationType.Remote)
  646. {
  647. await Task.Delay(4000).ConfigureAwait(false);
  648. }
  649. }
  650. protected int? GetVideoBitrateParam(StreamState state)
  651. {
  652. return state.VideoRequest.VideoBitRate;
  653. }
  654. protected int? GetAudioBitrateParam(StreamState state)
  655. {
  656. if (state.Request.AudioBitRate.HasValue)
  657. {
  658. // Make sure we don't request a bitrate higher than the source
  659. var currentBitrate = state.AudioStream == null ? state.Request.AudioBitRate.Value : state.AudioStream.BitRate ?? state.Request.AudioBitRate.Value;
  660. return Math.Min(currentBitrate, state.Request.AudioBitRate.Value);
  661. }
  662. return null;
  663. }
  664. /// <summary>
  665. /// Gets the user agent param.
  666. /// </summary>
  667. /// <param name="item">The item.</param>
  668. /// <returns>System.String.</returns>
  669. protected string GetUserAgentParam(BaseItem item)
  670. {
  671. var useragent = GetUserAgent(item);
  672. if (!string.IsNullOrEmpty(useragent))
  673. {
  674. return "-user-agent \"" + useragent + "\"";
  675. }
  676. return string.Empty;
  677. }
  678. /// <summary>
  679. /// Gets the user agent.
  680. /// </summary>
  681. /// <param name="item">The item.</param>
  682. /// <returns>System.String.</returns>
  683. protected string GetUserAgent(BaseItem item)
  684. {
  685. if (item.Path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1)
  686. {
  687. return "QuickTime/7.7.4";
  688. }
  689. return string.Empty;
  690. }
  691. /// <summary>
  692. /// Processes the exited.
  693. /// </summary>
  694. /// <param name="process">The process.</param>
  695. /// <param name="state">The state.</param>
  696. protected void OnFfMpegProcessExited(Process process, StreamState state)
  697. {
  698. if (state.IsoMount != null)
  699. {
  700. state.IsoMount.Dispose();
  701. state.IsoMount = null;
  702. }
  703. var outputFilePath = GetOutputFilePath(state);
  704. state.LogFileStream.Dispose();
  705. try
  706. {
  707. Logger.Info("FFMpeg exited with code {0} for {1}", process.ExitCode, outputFilePath);
  708. }
  709. catch
  710. {
  711. Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
  712. }
  713. }
  714. /// <summary>
  715. /// Gets the state.
  716. /// </summary>
  717. /// <param name="request">The request.</param>
  718. /// <returns>StreamState.</returns>
  719. protected StreamState GetState(StreamRequest request)
  720. {
  721. var item = DtoService.GetItemByDtoId(request.Id);
  722. var media = (IHasMediaStreams)item;
  723. var url = Request.PathInfo;
  724. if (!request.AudioCodec.HasValue)
  725. {
  726. request.AudioCodec = InferAudioCodec(url);
  727. }
  728. var state = new StreamState
  729. {
  730. Item = item,
  731. Request = request,
  732. Url = url
  733. };
  734. var videoRequest = request as VideoStreamRequest;
  735. var mediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery
  736. {
  737. ItemId = item.Id
  738. }).ToList();
  739. if (videoRequest != null)
  740. {
  741. if (!videoRequest.VideoCodec.HasValue)
  742. {
  743. videoRequest.VideoCodec = InferVideoCodec(url);
  744. }
  745. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  746. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  747. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  748. }
  749. else
  750. {
  751. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  752. }
  753. return state;
  754. }
  755. /// <summary>
  756. /// Infers the audio codec based on the url
  757. /// </summary>
  758. /// <param name="url">The URL.</param>
  759. /// <returns>System.Nullable{AudioCodecs}.</returns>
  760. private AudioCodecs? InferAudioCodec(string url)
  761. {
  762. var ext = Path.GetExtension(url);
  763. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  764. {
  765. return AudioCodecs.Mp3;
  766. }
  767. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  768. {
  769. return AudioCodecs.Aac;
  770. }
  771. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  772. {
  773. return AudioCodecs.Wma;
  774. }
  775. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  776. {
  777. return AudioCodecs.Vorbis;
  778. }
  779. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  780. {
  781. return AudioCodecs.Vorbis;
  782. }
  783. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  784. {
  785. return AudioCodecs.Vorbis;
  786. }
  787. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  788. {
  789. return AudioCodecs.Vorbis;
  790. }
  791. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  792. {
  793. return AudioCodecs.Vorbis;
  794. }
  795. return null;
  796. }
  797. /// <summary>
  798. /// Infers the video codec.
  799. /// </summary>
  800. /// <param name="url">The URL.</param>
  801. /// <returns>System.Nullable{VideoCodecs}.</returns>
  802. private VideoCodecs? InferVideoCodec(string url)
  803. {
  804. var ext = Path.GetExtension(url);
  805. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  806. {
  807. return VideoCodecs.Wmv;
  808. }
  809. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  810. {
  811. return VideoCodecs.Vpx;
  812. }
  813. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  814. {
  815. return VideoCodecs.Theora;
  816. }
  817. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  818. {
  819. return VideoCodecs.H264;
  820. }
  821. return VideoCodecs.Copy;
  822. }
  823. }
  824. }