BaseStreamingService.cs 33 KB

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