BaseStreamingService.cs 33 KB

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