BaseStreamingService.cs 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Dlna;
  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.LiveTv;
  10. using MediaBrowser.Controller.MediaEncoding;
  11. using MediaBrowser.Controller.Persistence;
  12. using MediaBrowser.Model.Configuration;
  13. using MediaBrowser.Model.Dlna;
  14. using MediaBrowser.Model.Drawing;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Library;
  18. using MediaBrowser.Model.LiveTv;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Diagnostics;
  22. using System.Globalization;
  23. using System.IO;
  24. using System.Linq;
  25. using System.Text;
  26. using System.Threading;
  27. using System.Threading.Tasks;
  28. using MediaBrowser.Model.MediaInfo;
  29. namespace MediaBrowser.Api.Playback
  30. {
  31. /// <summary>
  32. /// Class BaseStreamingService
  33. /// </summary>
  34. public abstract class BaseStreamingService : BaseApiService
  35. {
  36. /// <summary>
  37. /// Gets or sets the application paths.
  38. /// </summary>
  39. /// <value>The application paths.</value>
  40. protected IServerConfigurationManager ServerConfigurationManager { get; private set; }
  41. /// <summary>
  42. /// Gets or sets the user manager.
  43. /// </summary>
  44. /// <value>The user manager.</value>
  45. protected IUserManager UserManager { get; private set; }
  46. /// <summary>
  47. /// Gets or sets the library manager.
  48. /// </summary>
  49. /// <value>The library manager.</value>
  50. protected ILibraryManager LibraryManager { get; private set; }
  51. /// <summary>
  52. /// Gets or sets the iso manager.
  53. /// </summary>
  54. /// <value>The iso manager.</value>
  55. protected IIsoManager IsoManager { get; private set; }
  56. /// <summary>
  57. /// Gets or sets the media encoder.
  58. /// </summary>
  59. /// <value>The media encoder.</value>
  60. protected IMediaEncoder MediaEncoder { get; private set; }
  61. protected IEncodingManager EncodingManager { get; private set; }
  62. protected IDtoService DtoService { get; private set; }
  63. protected IFileSystem FileSystem { get; private set; }
  64. protected IItemRepository ItemRepository { get; private set; }
  65. protected ILiveTvManager LiveTvManager { get; private set; }
  66. protected IDlnaManager DlnaManager { get; private set; }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  69. /// </summary>
  70. /// <param name="serverConfig">The server configuration.</param>
  71. /// <param name="userManager">The user manager.</param>
  72. /// <param name="libraryManager">The library manager.</param>
  73. /// <param name="isoManager">The iso manager.</param>
  74. /// <param name="mediaEncoder">The media encoder.</param>
  75. /// <param name="dtoService">The dto service.</param>
  76. /// <param name="fileSystem">The file system.</param>
  77. /// <param name="itemRepository">The item repository.</param>
  78. protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IEncodingManager encodingManager, IDlnaManager dlnaManager)
  79. {
  80. DlnaManager = dlnaManager;
  81. EncodingManager = encodingManager;
  82. LiveTvManager = liveTvManager;
  83. ItemRepository = itemRepository;
  84. FileSystem = fileSystem;
  85. DtoService = dtoService;
  86. ServerConfigurationManager = serverConfig;
  87. UserManager = userManager;
  88. LibraryManager = libraryManager;
  89. IsoManager = isoManager;
  90. MediaEncoder = mediaEncoder;
  91. }
  92. /// <summary>
  93. /// Gets the command line arguments.
  94. /// </summary>
  95. /// <param name="outputPath">The output path.</param>
  96. /// <param name="state">The state.</param>
  97. /// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
  98. /// <returns>System.String.</returns>
  99. protected abstract string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions);
  100. /// <summary>
  101. /// Gets the type of the transcoding job.
  102. /// </summary>
  103. /// <value>The type of the transcoding job.</value>
  104. protected abstract TranscodingJobType TranscodingJobType { get; }
  105. /// <summary>
  106. /// Gets the output file extension.
  107. /// </summary>
  108. /// <param name="state">The state.</param>
  109. /// <returns>System.String.</returns>
  110. protected virtual string GetOutputFileExtension(StreamState state)
  111. {
  112. return Path.GetExtension(state.RequestedUrl);
  113. }
  114. /// <summary>
  115. /// Gets the output file path.
  116. /// </summary>
  117. /// <param name="state">The state.</param>
  118. /// <returns>System.String.</returns>
  119. protected string GetOutputFilePath(StreamState state)
  120. {
  121. var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath;
  122. var outputFileExtension = GetOutputFileExtension(state);
  123. return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
  124. }
  125. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  126. /// <summary>
  127. /// Gets the fast seek command line parameter.
  128. /// </summary>
  129. /// <param name="request">The request.</param>
  130. /// <returns>System.String.</returns>
  131. /// <value>The fast seek command line parameter.</value>
  132. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  133. {
  134. var time = request.StartTimeTicks;
  135. if (time.HasValue)
  136. {
  137. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds;
  138. if (seconds > 0)
  139. {
  140. return string.Format("-ss {0}", seconds.ToString(UsCulture));
  141. }
  142. }
  143. return string.Empty;
  144. }
  145. /// <summary>
  146. /// Gets the map args.
  147. /// </summary>
  148. /// <param name="state">The state.</param>
  149. /// <returns>System.String.</returns>
  150. protected virtual string GetMapArgs(StreamState state)
  151. {
  152. var args = string.Empty;
  153. if (!state.HasMediaStreams)
  154. {
  155. return state.IsInputVideo ? "-sn" : string.Empty;
  156. }
  157. if (state.VideoStream != null)
  158. {
  159. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  160. }
  161. else
  162. {
  163. args += "-map -0:v";
  164. }
  165. if (state.AudioStream != null)
  166. {
  167. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  168. }
  169. else
  170. {
  171. args += " -map -0:a";
  172. }
  173. if (state.SubtitleStream == null)
  174. {
  175. args += " -map -0:s";
  176. }
  177. return args;
  178. }
  179. /// <summary>
  180. /// Determines which stream will be used for playback
  181. /// </summary>
  182. /// <param name="allStream">All stream.</param>
  183. /// <param name="desiredIndex">Index of the desired.</param>
  184. /// <param name="type">The type.</param>
  185. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  186. /// <returns>MediaStream.</returns>
  187. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  188. {
  189. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  190. if (desiredIndex.HasValue)
  191. {
  192. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  193. if (stream != null)
  194. {
  195. return stream;
  196. }
  197. }
  198. if (type == MediaStreamType.Video)
  199. {
  200. streams = streams.Where(i => !string.Equals(i.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)).ToList();
  201. }
  202. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  203. {
  204. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  205. streams.FirstOrDefault();
  206. }
  207. // Just return the first one
  208. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  209. }
  210. protected EncodingQuality GetQualitySetting()
  211. {
  212. var quality = ServerConfigurationManager.Configuration.MediaEncodingQuality;
  213. if (quality == EncodingQuality.Auto)
  214. {
  215. var cpuCount = Environment.ProcessorCount;
  216. if (cpuCount >= 4)
  217. {
  218. //return EncodingQuality.HighQuality;
  219. }
  220. return EncodingQuality.HighSpeed;
  221. }
  222. return quality;
  223. }
  224. /// <summary>
  225. /// Gets the number of threads.
  226. /// </summary>
  227. /// <returns>System.Int32.</returns>
  228. /// <exception cref="System.Exception">Unrecognized MediaEncodingQuality value.</exception>
  229. protected int GetNumberOfThreads(StreamState state, bool isWebm)
  230. {
  231. // Use more when this is true. -re will keep cpu usage under control
  232. if (state.ReadInputAtNativeFramerate)
  233. {
  234. if (isWebm)
  235. {
  236. return Math.Max(Environment.ProcessorCount - 1, 2);
  237. }
  238. return 0;
  239. }
  240. // Webm: http://www.webmproject.org/docs/encoder-parameters/
  241. // The decoder will usually automatically use an appropriate number of threads according to how many cores are available but it can only use multiple threads
  242. // for the coefficient data if the encoder selected --token-parts > 0 at encode time.
  243. switch (GetQualitySetting())
  244. {
  245. case EncodingQuality.HighSpeed:
  246. return 2;
  247. case EncodingQuality.HighQuality:
  248. return 2;
  249. case EncodingQuality.MaxQuality:
  250. return isWebm ? Math.Max(Environment.ProcessorCount - 1, 2) : 0;
  251. default:
  252. throw new Exception("Unrecognized MediaEncodingQuality value.");
  253. }
  254. }
  255. /// <summary>
  256. /// Gets the video bitrate to specify on the command line
  257. /// </summary>
  258. /// <param name="state">The state.</param>
  259. /// <param name="videoCodec">The video codec.</param>
  260. /// <param name="isHls">if set to <c>true</c> [is HLS].</param>
  261. /// <returns>System.String.</returns>
  262. protected string GetVideoQualityParam(StreamState state, string videoCodec, bool isHls)
  263. {
  264. var param = string.Empty;
  265. var isVc1 = state.VideoStream != null &&
  266. string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase);
  267. var qualitySetting = GetQualitySetting();
  268. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  269. {
  270. switch (qualitySetting)
  271. {
  272. case EncodingQuality.HighSpeed:
  273. param = "-preset ultrafast";
  274. break;
  275. case EncodingQuality.HighQuality:
  276. param = "-preset superfast";
  277. break;
  278. case EncodingQuality.MaxQuality:
  279. param = "-preset superfast";
  280. break;
  281. }
  282. switch (qualitySetting)
  283. {
  284. case EncodingQuality.HighSpeed:
  285. param += " -crf 23";
  286. break;
  287. case EncodingQuality.HighQuality:
  288. param += " -crf 20";
  289. break;
  290. case EncodingQuality.MaxQuality:
  291. param += " -crf 18";
  292. break;
  293. }
  294. }
  295. // webm
  296. else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  297. {
  298. // Values 0-3, 0 being highest quality but slower
  299. var profileScore = 0;
  300. string crf;
  301. switch (qualitySetting)
  302. {
  303. case EncodingQuality.HighSpeed:
  304. crf = "16";
  305. profileScore = 2;
  306. break;
  307. case EncodingQuality.HighQuality:
  308. crf = "10";
  309. profileScore = 1;
  310. break;
  311. case EncodingQuality.MaxQuality:
  312. crf = "4";
  313. break;
  314. default:
  315. throw new ArgumentException("Unrecognized quality setting");
  316. }
  317. if (isVc1)
  318. {
  319. profileScore++;
  320. // Max of 2
  321. profileScore = Math.Min(profileScore, 2);
  322. }
  323. // http://www.webmproject.org/docs/encoder-parameters/
  324. param = string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1}",
  325. profileScore.ToString(UsCulture),
  326. crf);
  327. }
  328. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  329. {
  330. param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  331. }
  332. // asf/wmv
  333. else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase))
  334. {
  335. param = "-qmin 2";
  336. }
  337. else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  338. {
  339. param = "-mbd 2";
  340. }
  341. param += GetVideoBitrateParam(state, videoCodec, isHls);
  342. var framerate = GetFramerateParam(state);
  343. if (framerate.HasValue)
  344. {
  345. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  346. }
  347. if (!string.IsNullOrEmpty(state.OutputVideoSync))
  348. {
  349. param += " -vsync " + state.OutputVideoSync;
  350. }
  351. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  352. {
  353. param += " -profile:v " + state.VideoRequest.Profile;
  354. }
  355. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  356. {
  357. param += " -level " + state.VideoRequest.Level;
  358. }
  359. return param;
  360. }
  361. protected string GetAudioFilterParam(StreamState state, bool isHls)
  362. {
  363. var volParam = string.Empty;
  364. var audioSampleRate = string.Empty;
  365. var channels = state.OutputAudioChannels;
  366. // Boost volume to 200% when downsampling from 6ch to 2ch
  367. if (channels.HasValue && channels.Value <= 2)
  368. {
  369. if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  370. {
  371. volParam = ",volume=" + ServerConfigurationManager.Configuration.DownMixAudioBoost.ToString(UsCulture);
  372. }
  373. }
  374. if (state.OutputAudioSampleRate.HasValue)
  375. {
  376. audioSampleRate = state.OutputAudioSampleRate.Value + ":";
  377. }
  378. var adelay = isHls ? "adelay=1," : string.Empty;
  379. var pts = string.Empty;
  380. if (state.SubtitleStream != null && !state.SubtitleStream.IsGraphicalSubtitleStream)
  381. {
  382. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  383. pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(UsCulture));
  384. }
  385. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  386. adelay,
  387. audioSampleRate,
  388. volParam,
  389. pts,
  390. state.OutputAudioSync);
  391. }
  392. /// <summary>
  393. /// If we're going to put a fixed size on the command line, this will calculate it
  394. /// </summary>
  395. /// <param name="state">The state.</param>
  396. /// <param name="outputVideoCodec">The output video codec.</param>
  397. /// <param name="performTextSubtitleConversion">if set to <c>true</c> [perform text subtitle conversion].</param>
  398. /// <returns>System.String.</returns>
  399. protected string GetOutputSizeParam(StreamState state, string outputVideoCodec, bool performTextSubtitleConversion)
  400. {
  401. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  402. var assSubtitleParam = string.Empty;
  403. var copyTsParam = string.Empty;
  404. var yadifParam = state.DeInterlace ? "yadif=0:-1:0," : string.Empty;
  405. var request = state.VideoRequest;
  406. if (state.SubtitleStream != null && !state.SubtitleStream.IsGraphicalSubtitleStream)
  407. {
  408. assSubtitleParam = GetTextSubtitleParam(state, performTextSubtitleConversion);
  409. copyTsParam = " -copyts";
  410. }
  411. // If fixed dimensions were supplied
  412. if (request.Width.HasValue && request.Height.HasValue)
  413. {
  414. var widthParam = request.Width.Value.ToString(UsCulture);
  415. var heightParam = request.Height.Value.ToString(UsCulture);
  416. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  417. }
  418. // If Max dimensions were supplied, for width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size
  419. if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
  420. {
  421. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  422. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  423. return string.Format("{4} -vf \"{0}scale=trunc(min(iw\\,{1})/2)*2:trunc(min((iw/dar)\\,{2})/2)*2{3}\"", yadifParam, maxWidthParam, maxHeightParam, assSubtitleParam, copyTsParam);
  424. }
  425. var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
  426. // If a fixed width was requested
  427. if (request.Width.HasValue)
  428. {
  429. var widthParam = request.Width.Value.ToString(UsCulture);
  430. return isH264Output ?
  431. string.Format("{3} -vf \"{0}scale={1}:trunc(ow/a/2)*2{2}\"", yadifParam, widthParam, assSubtitleParam, copyTsParam) :
  432. string.Format("{3} -vf \"{0}scale={1}:-1{2}\"", yadifParam, widthParam, assSubtitleParam, copyTsParam);
  433. }
  434. // If a fixed height was requested
  435. if (request.Height.HasValue)
  436. {
  437. var heightParam = request.Height.Value.ToString(UsCulture);
  438. return isH264Output ?
  439. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:{1}{2}\"", yadifParam, heightParam, assSubtitleParam, copyTsParam) :
  440. string.Format("{3} -vf \"{0}scale=-1:{1}{2}\"", yadifParam, heightParam, assSubtitleParam, copyTsParam);
  441. }
  442. // If a max width was requested
  443. if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null))
  444. {
  445. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  446. return isH264Output ?
  447. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):trunc(ow/dar/2)*2{2}\"", yadifParam, maxWidthParam, assSubtitleParam, copyTsParam) :
  448. string.Format("{3} -vf \"{0}scale=min(iw\\,{1}):-1{2}\"", yadifParam, maxWidthParam, assSubtitleParam, copyTsParam);
  449. }
  450. // If a max height was requested
  451. if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null))
  452. {
  453. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  454. return isH264Output ?
  455. string.Format("{3} -vf \"{0}scale=trunc(oh*a*2)/2:min(ih\\,{1}){2}\"", yadifParam, maxHeightParam, assSubtitleParam, copyTsParam) :
  456. string.Format("{3} -vf \"{0}scale=-1:min(ih\\,{1}){2}\"", yadifParam, maxHeightParam, assSubtitleParam, copyTsParam);
  457. }
  458. if (state.VideoStream == null)
  459. {
  460. // No way to figure this out
  461. return string.Empty;
  462. }
  463. // Need to perform calculations manually
  464. // Try to account for bad media info
  465. var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0;
  466. var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0;
  467. var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight);
  468. // If we're encoding with libx264, it can't handle odd numbered widths or heights, so we'll have to fix that
  469. if (isH264Output)
  470. {
  471. var widthParam = outputSize.Width.ToString(UsCulture);
  472. var heightParam = outputSize.Height.ToString(UsCulture);
  473. return string.Format("{4} -vf \"{0}scale=trunc({1}/2)*2:trunc({2}/2)*2{3}\"", yadifParam, widthParam, heightParam, assSubtitleParam, copyTsParam);
  474. }
  475. // Otherwise use -vf scale since ffmpeg will ensure internally that the aspect ratio is preserved
  476. return string.Format("{3} -vf \"{0}scale={1}:-1{2}\"", yadifParam, Convert.ToInt32(outputSize.Width), assSubtitleParam, copyTsParam);
  477. }
  478. /// <summary>
  479. /// Gets the text subtitle param.
  480. /// </summary>
  481. /// <param name="state">The state.</param>
  482. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  483. /// <returns>System.String.</returns>
  484. protected string GetTextSubtitleParam(StreamState state, bool performConversion)
  485. {
  486. var path = state.SubtitleStream.IsExternal ? GetConvertedAssPath(state.MediaPath, state.SubtitleStream, performConversion) :
  487. GetExtractedAssPath(state, performConversion);
  488. if (string.IsNullOrEmpty(path))
  489. {
  490. return string.Empty;
  491. }
  492. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  493. return string.Format(",ass='{0}',setpts=PTS -{1}/TB",
  494. path.Replace('\\', '/').Replace(":/", "\\:/"),
  495. Math.Round(seconds).ToString(UsCulture));
  496. }
  497. /// <summary>
  498. /// Gets the extracted ass path.
  499. /// </summary>
  500. /// <param name="state">The state.</param>
  501. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  502. /// <returns>System.String.</returns>
  503. private string GetExtractedAssPath(StreamState state, bool performConversion)
  504. {
  505. var path = EncodingManager.GetSubtitleCachePath(state.MediaPath, state.SubtitleStream.Index, ".ass");
  506. if (performConversion)
  507. {
  508. InputType type;
  509. var inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, null, state.PlayableStreamFileNames, out type);
  510. try
  511. {
  512. var parentPath = Path.GetDirectoryName(path);
  513. Directory.CreateDirectory(parentPath);
  514. // Don't re-encode ass/ssa to ass because ffmpeg ass encoder fails if there's more than one ass rectangle. Affect Anime mostly.
  515. // See https://lists.ffmpeg.org/pipermail/ffmpeg-cvslog/2013-April/063616.html
  516. var isAssSubtitle = string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase);
  517. var task = MediaEncoder.ExtractTextSubtitle(inputPath, type, state.SubtitleStream.Index, isAssSubtitle, path, CancellationToken.None);
  518. Task.WaitAll(task);
  519. }
  520. catch
  521. {
  522. return null;
  523. }
  524. }
  525. return path;
  526. }
  527. /// <summary>
  528. /// Gets the converted ass path.
  529. /// </summary>
  530. /// <param name="mediaPath">The media path.</param>
  531. /// <param name="subtitleStream">The subtitle stream.</param>
  532. /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param>
  533. /// <returns>System.String.</returns>
  534. private string GetConvertedAssPath(string mediaPath, MediaStream subtitleStream, bool performConversion)
  535. {
  536. var path = EncodingManager.GetSubtitleCachePath(subtitleStream.Path, ".ass");
  537. if (performConversion)
  538. {
  539. try
  540. {
  541. var parentPath = Path.GetDirectoryName(path);
  542. Directory.CreateDirectory(parentPath);
  543. var task = MediaEncoder.ConvertTextSubtitleToAss(subtitleStream.Path, path, subtitleStream.Language, CancellationToken.None);
  544. Task.WaitAll(task);
  545. }
  546. catch
  547. {
  548. return null;
  549. }
  550. }
  551. return path;
  552. }
  553. /// <summary>
  554. /// Gets the internal graphical subtitle param.
  555. /// </summary>
  556. /// <param name="state">The state.</param>
  557. /// <param name="outputVideoCodec">The output video codec.</param>
  558. /// <returns>System.String.</returns>
  559. protected string GetInternalGraphicalSubtitleParam(StreamState state, string outputVideoCodec)
  560. {
  561. var outputSizeParam = string.Empty;
  562. var request = state.VideoRequest;
  563. // Add resolution params, if specified
  564. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  565. {
  566. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec, false).TrimEnd('"');
  567. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  568. }
  569. var videoSizeParam = string.Empty;
  570. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  571. {
  572. videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  573. }
  574. return string.Format(" -filter_complex \"[0:{0}]format=yuva444p{3},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{1}] [sub] overlay{2}\"",
  575. state.SubtitleStream.Index,
  576. state.VideoStream.Index,
  577. outputSizeParam,
  578. videoSizeParam);
  579. }
  580. /// <summary>
  581. /// Gets the probe size argument.
  582. /// </summary>
  583. /// <param name="isVideo">if set to <c>true</c> [is video].</param>
  584. /// <param name="videoType">Type of the video.</param>
  585. /// <param name="isoType">Type of the iso.</param>
  586. /// <returns>System.String.</returns>
  587. private string GetProbeSizeArgument(bool isVideo, VideoType? videoType, IsoType? isoType)
  588. {
  589. var type = !isVideo ? MediaEncoderHelpers.GetInputType(null, null) :
  590. MediaEncoderHelpers.GetInputType(videoType, isoType);
  591. return MediaEncoder.GetProbeSizeArgument(type);
  592. }
  593. /// <summary>
  594. /// Gets the number of audio channels to specify on the command line
  595. /// </summary>
  596. /// <param name="request">The request.</param>
  597. /// <param name="audioStream">The audio stream.</param>
  598. /// <returns>System.Nullable{System.Int32}.</returns>
  599. private int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream)
  600. {
  601. if (audioStream != null)
  602. {
  603. var codec = request.AudioCodec ?? string.Empty;
  604. if (audioStream.Channels > 2 && codec.IndexOf("wma", StringComparison.OrdinalIgnoreCase) != -1)
  605. {
  606. // wmav2 currently only supports two channel output
  607. return 2;
  608. }
  609. }
  610. if (request.MaxAudioChannels.HasValue)
  611. {
  612. if (audioStream != null && audioStream.Channels.HasValue)
  613. {
  614. return Math.Min(request.MaxAudioChannels.Value, audioStream.Channels.Value);
  615. }
  616. return request.MaxAudioChannels.Value;
  617. }
  618. return request.AudioChannels;
  619. }
  620. /// <summary>
  621. /// Determines whether the specified stream is H264.
  622. /// </summary>
  623. /// <param name="stream">The stream.</param>
  624. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  625. protected bool IsH264(MediaStream stream)
  626. {
  627. return stream.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  628. stream.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  629. }
  630. /// <summary>
  631. /// Gets the name of the output audio codec
  632. /// </summary>
  633. /// <param name="request">The request.</param>
  634. /// <returns>System.String.</returns>
  635. protected string GetAudioCodec(StreamRequest request)
  636. {
  637. var codec = request.AudioCodec;
  638. if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase))
  639. {
  640. return "aac -strict experimental";
  641. }
  642. if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase))
  643. {
  644. return "libmp3lame";
  645. }
  646. if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase))
  647. {
  648. return "libvorbis";
  649. }
  650. if (string.Equals(codec, "wma", StringComparison.OrdinalIgnoreCase))
  651. {
  652. return "wmav2";
  653. }
  654. return codec.ToLower();
  655. }
  656. /// <summary>
  657. /// Gets the name of the output video codec
  658. /// </summary>
  659. /// <param name="request">The request.</param>
  660. /// <returns>System.String.</returns>
  661. protected string GetVideoCodec(VideoStreamRequest request)
  662. {
  663. var codec = request.VideoCodec;
  664. if (!string.IsNullOrEmpty(codec))
  665. {
  666. if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase))
  667. {
  668. return "libx264";
  669. }
  670. if (string.Equals(codec, "vpx", StringComparison.OrdinalIgnoreCase))
  671. {
  672. return "libvpx";
  673. }
  674. if (string.Equals(codec, "wmv", StringComparison.OrdinalIgnoreCase))
  675. {
  676. return "wmv2";
  677. }
  678. if (string.Equals(codec, "theora", StringComparison.OrdinalIgnoreCase))
  679. {
  680. return "libtheora";
  681. }
  682. return codec.ToLower();
  683. }
  684. return "copy";
  685. }
  686. /// <summary>
  687. /// Gets the input argument.
  688. /// </summary>
  689. /// <param name="state">The state.</param>
  690. /// <returns>System.String.</returns>
  691. protected string GetInputArgument(StreamState state)
  692. {
  693. var type = InputType.File;
  694. var inputPath = new[] { state.MediaPath };
  695. if (state.IsInputVideo)
  696. {
  697. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  698. {
  699. inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, state.IsoMount, state.PlayableStreamFileNames, out type);
  700. }
  701. }
  702. return MediaEncoder.GetInputArgument(inputPath, type);
  703. }
  704. /// <summary>
  705. /// Starts the FFMPEG.
  706. /// </summary>
  707. /// <param name="state">The state.</param>
  708. /// <param name="outputPath">The output path.</param>
  709. /// <returns>Task.</returns>
  710. protected async Task StartFfMpeg(StreamState state, string outputPath)
  711. {
  712. if (!File.Exists(MediaEncoder.EncoderPath))
  713. {
  714. throw new InvalidOperationException("ffmpeg was not found at " + MediaEncoder.EncoderPath);
  715. }
  716. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  717. if (state.IsInputVideo && state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  718. {
  719. state.IsoMount = await IsoManager.Mount(state.MediaPath, CancellationToken.None).ConfigureAwait(false);
  720. }
  721. var commandLineArgs = GetCommandLineArguments(outputPath, state, true);
  722. if (ServerConfigurationManager.Configuration.EnableDebugEncodingLogging)
  723. {
  724. commandLineArgs = "-loglevel debug " + commandLineArgs;
  725. }
  726. var process = new Process
  727. {
  728. StartInfo = new ProcessStartInfo
  729. {
  730. CreateNoWindow = true,
  731. UseShellExecute = false,
  732. // Must consume both stdout and stderr or deadlocks may occur
  733. RedirectStandardOutput = true,
  734. RedirectStandardError = true,
  735. FileName = MediaEncoder.EncoderPath,
  736. WorkingDirectory = Path.GetDirectoryName(MediaEncoder.EncoderPath),
  737. Arguments = commandLineArgs,
  738. WindowStyle = ProcessWindowStyle.Hidden,
  739. ErrorDialog = false
  740. },
  741. EnableRaisingEvents = true
  742. };
  743. ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, state.Request.StartTimeTicks, state.MediaPath, state.Request.DeviceId);
  744. var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;
  745. Logger.Info(commandLineLogMessage);
  746. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "transcode-" + Guid.NewGuid() + ".txt");
  747. Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
  748. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  749. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);
  750. var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(commandLineLogMessage + Environment.NewLine + Environment.NewLine);
  751. await state.LogFileStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length).ConfigureAwait(false);
  752. process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);
  753. try
  754. {
  755. process.Start();
  756. }
  757. catch (Exception ex)
  758. {
  759. Logger.ErrorException("Error starting ffmpeg", ex);
  760. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
  761. throw;
  762. }
  763. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  764. process.BeginOutputReadLine();
  765. #pragma warning disable 4014
  766. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  767. process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);
  768. #pragma warning restore 4014
  769. // Wait for the file to exist before proceeeding
  770. while (!File.Exists(outputPath))
  771. {
  772. await Task.Delay(100).ConfigureAwait(false);
  773. }
  774. // Allow a small amount of time to buffer a little
  775. if (state.IsInputVideo)
  776. {
  777. await Task.Delay(500).ConfigureAwait(false);
  778. }
  779. // This is arbitrary, but add a little buffer time when internet streaming
  780. if (state.IsRemote)
  781. {
  782. await Task.Delay(3000).ConfigureAwait(false);
  783. }
  784. }
  785. private int? GetVideoBitrateParamValue(VideoStreamRequest request, MediaStream videoStream)
  786. {
  787. var bitrate = request.VideoBitRate;
  788. if (videoStream != null)
  789. {
  790. var isUpscaling = request.Height.HasValue && videoStream.Height.HasValue &&
  791. request.Height.Value > videoStream.Height.Value;
  792. if (request.Width.HasValue && videoStream.Width.HasValue &&
  793. request.Width.Value > videoStream.Width.Value)
  794. {
  795. isUpscaling = true;
  796. }
  797. // Don't allow bitrate increases unless upscaling
  798. if (!isUpscaling)
  799. {
  800. if (bitrate.HasValue && videoStream.BitRate.HasValue)
  801. {
  802. bitrate = Math.Min(bitrate.Value, videoStream.BitRate.Value);
  803. }
  804. }
  805. }
  806. return bitrate;
  807. }
  808. protected string GetVideoBitrateParam(StreamState state, string videoCodec, bool isHls)
  809. {
  810. var bitrate = state.OutputVideoBitrate;
  811. if (bitrate.HasValue)
  812. {
  813. var hasFixedResolution = state.VideoRequest.HasFixedResolution;
  814. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  815. {
  816. if (hasFixedResolution)
  817. {
  818. return string.Format(" -minrate:v ({0}*.90) -maxrate:v ({0}*1.10) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture));
  819. }
  820. // With vpx when crf is used, b:v becomes a max rate
  821. // https://trac.ffmpeg.org/wiki/vpxEncodingGuide. But higher bitrate source files -b:v causes judder so limite the bitrate but dont allow it to "saturate" the bitrate. So dont contrain it down just up.
  822. return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture));
  823. }
  824. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  825. {
  826. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  827. }
  828. // H264
  829. if (hasFixedResolution)
  830. {
  831. if (isHls)
  832. {
  833. return string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture));
  834. }
  835. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  836. }
  837. return string.Format(" -maxrate {0} -bufsize {1}",
  838. bitrate.Value.ToString(UsCulture),
  839. (bitrate.Value * 2).ToString(UsCulture));
  840. }
  841. return string.Empty;
  842. }
  843. private int? GetAudioBitrateParam(StreamRequest request, MediaStream audioStream)
  844. {
  845. if (request.AudioBitRate.HasValue)
  846. {
  847. // Make sure we don't request a bitrate higher than the source
  848. var currentBitrate = audioStream == null ? request.AudioBitRate.Value : audioStream.BitRate ?? request.AudioBitRate.Value;
  849. return Math.Min(currentBitrate, request.AudioBitRate.Value);
  850. }
  851. return null;
  852. }
  853. /// <summary>
  854. /// Gets the user agent param.
  855. /// </summary>
  856. /// <param name="path">The path.</param>
  857. /// <returns>System.String.</returns>
  858. private string GetUserAgentParam(string path)
  859. {
  860. var useragent = GetUserAgent(path);
  861. if (!string.IsNullOrEmpty(useragent))
  862. {
  863. return "-user-agent \"" + useragent + "\"";
  864. }
  865. return string.Empty;
  866. }
  867. /// <summary>
  868. /// Gets the user agent.
  869. /// </summary>
  870. /// <param name="path">The path.</param>
  871. /// <returns>System.String.</returns>
  872. protected string GetUserAgent(string path)
  873. {
  874. if (string.IsNullOrEmpty(path))
  875. {
  876. throw new ArgumentNullException("path");
  877. }
  878. if (path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1)
  879. {
  880. return "QuickTime/7.7.4";
  881. }
  882. return string.Empty;
  883. }
  884. /// <summary>
  885. /// Processes the exited.
  886. /// </summary>
  887. /// <param name="process">The process.</param>
  888. /// <param name="state">The state.</param>
  889. protected void OnFfMpegProcessExited(Process process, StreamState state)
  890. {
  891. state.Dispose();
  892. try
  893. {
  894. Logger.Info("FFMpeg exited with code {0}", process.ExitCode);
  895. }
  896. catch
  897. {
  898. Logger.Info("FFMpeg exited with an error.");
  899. }
  900. }
  901. protected double? GetFramerateParam(StreamState state)
  902. {
  903. if (state.VideoRequest != null)
  904. {
  905. if (state.VideoRequest.Framerate.HasValue)
  906. {
  907. return state.VideoRequest.Framerate.Value;
  908. }
  909. var maxrate = state.VideoRequest.MaxFramerate ?? 23.97602;
  910. if (state.VideoStream != null)
  911. {
  912. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  913. if (contentRate.HasValue && contentRate.Value > maxrate)
  914. {
  915. return maxrate;
  916. }
  917. }
  918. }
  919. return null;
  920. }
  921. /// <summary>
  922. /// Parses the parameters.
  923. /// </summary>
  924. /// <param name="request">The request.</param>
  925. private void ParseParams(StreamRequest request)
  926. {
  927. var vals = request.Params.Split(';');
  928. var videoRequest = request as VideoStreamRequest;
  929. for (var i = 0; i < vals.Length; i++)
  930. {
  931. var val = vals[i];
  932. if (string.IsNullOrWhiteSpace(val))
  933. {
  934. continue;
  935. }
  936. if (i == 0)
  937. {
  938. request.DeviceProfileId = val;
  939. }
  940. else if (i == 1)
  941. {
  942. request.DeviceId = val;
  943. }
  944. else if (i == 2)
  945. {
  946. request.MediaSourceId = val;
  947. }
  948. else if (i == 3)
  949. {
  950. request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  951. }
  952. else if (i == 4)
  953. {
  954. if (videoRequest != null)
  955. {
  956. videoRequest.VideoCodec = val;
  957. }
  958. }
  959. else if (i == 5)
  960. {
  961. request.AudioCodec = val;
  962. }
  963. else if (i == 6)
  964. {
  965. if (videoRequest != null)
  966. {
  967. videoRequest.AudioStreamIndex = int.Parse(val, UsCulture);
  968. }
  969. }
  970. else if (i == 7)
  971. {
  972. if (videoRequest != null)
  973. {
  974. videoRequest.SubtitleStreamIndex = int.Parse(val, UsCulture);
  975. }
  976. }
  977. else if (i == 8)
  978. {
  979. if (videoRequest != null)
  980. {
  981. videoRequest.VideoBitRate = int.Parse(val, UsCulture);
  982. }
  983. }
  984. else if (i == 9)
  985. {
  986. request.AudioBitRate = int.Parse(val, UsCulture);
  987. }
  988. else if (i == 10)
  989. {
  990. request.MaxAudioChannels = int.Parse(val, UsCulture);
  991. }
  992. else if (i == 11)
  993. {
  994. if (videoRequest != null)
  995. {
  996. videoRequest.MaxFramerate = double.Parse(val, UsCulture);
  997. }
  998. }
  999. else if (i == 12)
  1000. {
  1001. if (videoRequest != null)
  1002. {
  1003. videoRequest.MaxWidth = int.Parse(val, UsCulture);
  1004. }
  1005. }
  1006. else if (i == 13)
  1007. {
  1008. if (videoRequest != null)
  1009. {
  1010. videoRequest.MaxHeight = int.Parse(val, UsCulture);
  1011. }
  1012. }
  1013. else if (i == 14)
  1014. {
  1015. request.StartTimeTicks = long.Parse(val, UsCulture);
  1016. }
  1017. else if (i == 15)
  1018. {
  1019. if (videoRequest != null)
  1020. {
  1021. videoRequest.Level = val;
  1022. }
  1023. }
  1024. }
  1025. }
  1026. /// <summary>
  1027. /// Parses the dlna headers.
  1028. /// </summary>
  1029. /// <param name="request">The request.</param>
  1030. private void ParseDlnaHeaders(StreamRequest request)
  1031. {
  1032. if (!request.StartTimeTicks.HasValue)
  1033. {
  1034. var timeSeek = GetHeader("TimeSeekRange.dlna.org");
  1035. request.StartTimeTicks = ParseTimeSeekHeader(timeSeek);
  1036. }
  1037. }
  1038. /// <summary>
  1039. /// Parses the time seek header.
  1040. /// </summary>
  1041. private long? ParseTimeSeekHeader(string value)
  1042. {
  1043. if (string.IsNullOrWhiteSpace(value))
  1044. {
  1045. return null;
  1046. }
  1047. if (value.IndexOf("npt=", StringComparison.OrdinalIgnoreCase) != 0)
  1048. {
  1049. throw new ArgumentException("Invalid timeseek header");
  1050. }
  1051. value = value.Substring(4).Split(new[] { '-' }, 2)[0];
  1052. if (value.IndexOf(':') == -1)
  1053. {
  1054. // Parses npt times in the format of '417.33'
  1055. double seconds;
  1056. if (double.TryParse(value, NumberStyles.Any, UsCulture, out seconds))
  1057. {
  1058. return TimeSpan.FromSeconds(seconds).Ticks;
  1059. }
  1060. throw new ArgumentException("Invalid timeseek header");
  1061. }
  1062. // Parses npt times in the format of '10:19:25.7'
  1063. var tokens = value.Split(new[] { ':' }, 3);
  1064. double secondsSum = 0;
  1065. var timeFactor = 3600;
  1066. foreach (var time in tokens)
  1067. {
  1068. double digit;
  1069. if (double.TryParse(time, NumberStyles.Any, UsCulture, out digit))
  1070. {
  1071. secondsSum += (digit * timeFactor);
  1072. }
  1073. else
  1074. {
  1075. throw new ArgumentException("Invalid timeseek header");
  1076. }
  1077. timeFactor /= 60;
  1078. }
  1079. return TimeSpan.FromSeconds(secondsSum).Ticks;
  1080. }
  1081. /// <summary>
  1082. /// Gets the state.
  1083. /// </summary>
  1084. /// <param name="request">The request.</param>
  1085. /// <param name="cancellationToken">The cancellation token.</param>
  1086. /// <returns>StreamState.</returns>
  1087. protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
  1088. {
  1089. ParseDlnaHeaders(request);
  1090. if (!string.IsNullOrWhiteSpace(request.Params))
  1091. {
  1092. ParseParams(request);
  1093. }
  1094. var user = AuthorizationRequestFilterAttribute.GetCurrentUser(Request, UserManager);
  1095. var url = Request.PathInfo;
  1096. if (string.IsNullOrEmpty(request.AudioCodec))
  1097. {
  1098. request.AudioCodec = InferAudioCodec(url);
  1099. }
  1100. var state = new StreamState(LiveTvManager, Logger)
  1101. {
  1102. Request = request,
  1103. RequestedUrl = url
  1104. };
  1105. if (!string.IsNullOrWhiteSpace(request.AudioCodec))
  1106. {
  1107. state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
  1108. state.Request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault();
  1109. }
  1110. var item = string.IsNullOrEmpty(request.MediaSourceId) ?
  1111. LibraryManager.GetItemById(request.Id) :
  1112. LibraryManager.GetItemById(request.MediaSourceId);
  1113. if (user != null && item.GetPlayAccess(user) != PlayAccess.Full)
  1114. {
  1115. throw new ArgumentException(string.Format("{0} is not allowed to play media.", user.Name));
  1116. }
  1117. if (item is ILiveTvRecording)
  1118. {
  1119. var recording = await LiveTvManager.GetInternalRecording(request.Id, cancellationToken).ConfigureAwait(false);
  1120. state.VideoType = VideoType.VideoFile;
  1121. state.IsInputVideo = string.Equals(recording.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1122. state.PlayableStreamFileNames = new List<string>();
  1123. var path = recording.RecordingInfo.Path;
  1124. var mediaUrl = recording.RecordingInfo.Url;
  1125. if (string.IsNullOrWhiteSpace(path) && string.IsNullOrWhiteSpace(mediaUrl))
  1126. {
  1127. var streamInfo = await LiveTvManager.GetRecordingStream(request.Id, cancellationToken).ConfigureAwait(false);
  1128. state.LiveTvStreamId = streamInfo.Id;
  1129. path = streamInfo.Path;
  1130. mediaUrl = streamInfo.Url;
  1131. }
  1132. if (!string.IsNullOrEmpty(path))
  1133. {
  1134. state.MediaPath = path;
  1135. state.IsRemote = false;
  1136. }
  1137. else if (!string.IsNullOrEmpty(mediaUrl))
  1138. {
  1139. state.MediaPath = mediaUrl;
  1140. state.IsRemote = true;
  1141. }
  1142. state.RunTimeTicks = recording.RunTimeTicks;
  1143. if (recording.RecordingInfo.Status == RecordingStatus.InProgress)
  1144. {
  1145. await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
  1146. }
  1147. state.ReadInputAtNativeFramerate = recording.RecordingInfo.Status == RecordingStatus.InProgress;
  1148. state.OutputAudioSync = "1000";
  1149. state.DeInterlace = true;
  1150. state.InputVideoSync = "-1";
  1151. state.InputAudioSync = "1";
  1152. state.InputContainer = recording.Container;
  1153. }
  1154. else if (item is LiveTvChannel)
  1155. {
  1156. var channel = LiveTvManager.GetInternalChannel(request.Id);
  1157. state.VideoType = VideoType.VideoFile;
  1158. state.IsInputVideo = string.Equals(channel.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1159. state.PlayableStreamFileNames = new List<string>();
  1160. var streamInfo = await LiveTvManager.GetChannelStream(request.Id, cancellationToken).ConfigureAwait(false);
  1161. state.LiveTvStreamId = streamInfo.Id;
  1162. if (!string.IsNullOrEmpty(streamInfo.Path))
  1163. {
  1164. state.MediaPath = streamInfo.Path;
  1165. state.IsRemote = false;
  1166. await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
  1167. }
  1168. else if (!string.IsNullOrEmpty(streamInfo.Url))
  1169. {
  1170. state.MediaPath = streamInfo.Url;
  1171. state.IsRemote = true;
  1172. }
  1173. state.ReadInputAtNativeFramerate = true;
  1174. state.OutputAudioSync = "1000";
  1175. state.DeInterlace = true;
  1176. state.InputVideoSync = "-1";
  1177. state.InputAudioSync = "1";
  1178. }
  1179. else
  1180. {
  1181. state.MediaPath = item.Path;
  1182. state.IsRemote = item.LocationType == LocationType.Remote;
  1183. var video = item as Video;
  1184. if (video != null)
  1185. {
  1186. state.IsInputVideo = true;
  1187. state.VideoType = video.VideoType;
  1188. state.IsoType = video.IsoType;
  1189. state.PlayableStreamFileNames = video.PlayableStreamFileNames == null
  1190. ? new List<string>()
  1191. : video.PlayableStreamFileNames.ToList();
  1192. state.DeInterlace = string.Equals(video.Container, "wtv", StringComparison.OrdinalIgnoreCase);
  1193. state.InputTimestamp = video.Timestamp ?? TransportStreamTimestamp.None;
  1194. state.InputContainer = video.Container;
  1195. }
  1196. var audio = item as Audio;
  1197. if (audio != null)
  1198. {
  1199. state.InputContainer = audio.Container;
  1200. }
  1201. state.RunTimeTicks = item.RunTimeTicks;
  1202. }
  1203. var videoRequest = request as VideoStreamRequest;
  1204. var mediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery
  1205. {
  1206. ItemId = item.Id
  1207. }).ToList();
  1208. if (videoRequest != null)
  1209. {
  1210. if (string.IsNullOrEmpty(videoRequest.VideoCodec))
  1211. {
  1212. videoRequest.VideoCodec = InferVideoCodec(url);
  1213. }
  1214. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  1215. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  1216. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  1217. if (state.VideoStream != null && state.VideoStream.IsInterlaced)
  1218. {
  1219. state.DeInterlace = true;
  1220. }
  1221. EnforceResolutionLimit(state, videoRequest);
  1222. }
  1223. else
  1224. {
  1225. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  1226. }
  1227. state.HasMediaStreams = mediaStreams.Count > 0;
  1228. state.SegmentLength = state.ReadInputAtNativeFramerate ? 5 : 10;
  1229. state.HlsListSize = state.ReadInputAtNativeFramerate ? 100 : 1440;
  1230. var container = Path.GetExtension(state.RequestedUrl);
  1231. if (string.IsNullOrEmpty(container))
  1232. {
  1233. container = request.Static ? state.InputContainer : Path.GetExtension(GetOutputFilePath(state));
  1234. }
  1235. state.OutputContainer = (container ?? string.Empty).TrimStart('.');
  1236. ApplyDeviceProfileSettings(state);
  1237. state.OutputAudioBitrate = GetAudioBitrateParam(state.Request, state.AudioStream);
  1238. state.OutputAudioSampleRate = request.AudioSampleRate;
  1239. state.OutputAudioChannels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  1240. if (videoRequest != null)
  1241. {
  1242. state.OutputVideoBitrate = GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream);
  1243. if (state.VideoStream != null && CanStreamCopyVideo(videoRequest, state.VideoStream))
  1244. {
  1245. videoRequest.VideoCodec = "copy";
  1246. }
  1247. if (state.AudioStream != null && CanStreamCopyAudio(request, state.AudioStream, state.SupportedAudioCodecs))
  1248. {
  1249. request.AudioCodec = "copy";
  1250. }
  1251. }
  1252. var headers = new Dictionary<string, string>();
  1253. foreach (var key in Request.Headers.AllKeys)
  1254. {
  1255. headers[key] = Request.Headers[key];
  1256. }
  1257. state.DeviceProfile = string.IsNullOrWhiteSpace(state.Request.DeviceProfileId) ?
  1258. DlnaManager.GetProfile(headers) :
  1259. DlnaManager.GetProfile(state.Request.DeviceProfileId);
  1260. return state;
  1261. }
  1262. private bool CanStreamCopyVideo(VideoStreamRequest request, MediaStream videoStream)
  1263. {
  1264. if (videoStream.IsInterlaced)
  1265. {
  1266. return false;
  1267. }
  1268. // Source and target codecs must match
  1269. if (!string.Equals(request.VideoCodec, videoStream.Codec, StringComparison.OrdinalIgnoreCase))
  1270. {
  1271. return false;
  1272. }
  1273. // If client is requesting a specific video profile, it must match the source
  1274. if (!string.IsNullOrEmpty(request.Profile) && !string.Equals(request.Profile, videoStream.Profile, StringComparison.OrdinalIgnoreCase))
  1275. {
  1276. return false;
  1277. }
  1278. // Video width must fall within requested value
  1279. if (request.MaxWidth.HasValue)
  1280. {
  1281. if (!videoStream.Width.HasValue || videoStream.Width.Value > request.MaxWidth.Value)
  1282. {
  1283. return false;
  1284. }
  1285. }
  1286. // Video height must fall within requested value
  1287. if (request.MaxHeight.HasValue)
  1288. {
  1289. if (!videoStream.Height.HasValue || videoStream.Height.Value > request.MaxHeight.Value)
  1290. {
  1291. return false;
  1292. }
  1293. }
  1294. // Video framerate must fall within requested value
  1295. var requestedFramerate = request.MaxFramerate ?? request.Framerate;
  1296. if (requestedFramerate.HasValue)
  1297. {
  1298. var videoFrameRate = videoStream.AverageFrameRate ?? videoStream.RealFrameRate;
  1299. if (!videoFrameRate.HasValue || videoFrameRate.Value > requestedFramerate.Value)
  1300. {
  1301. return false;
  1302. }
  1303. }
  1304. // Video bitrate must fall within requested value
  1305. if (request.VideoBitRate.HasValue)
  1306. {
  1307. if (!videoStream.BitRate.HasValue || videoStream.BitRate.Value > request.VideoBitRate.Value)
  1308. {
  1309. return false;
  1310. }
  1311. }
  1312. // If a specific level was requested, the source must match or be less than
  1313. if (!string.IsNullOrEmpty(request.Level))
  1314. {
  1315. double requestLevel;
  1316. if (double.TryParse(request.Level, NumberStyles.Any, UsCulture, out requestLevel))
  1317. {
  1318. if (!videoStream.Level.HasValue)
  1319. {
  1320. return false;
  1321. }
  1322. if (videoStream.Level.Value > requestLevel)
  1323. {
  1324. return false;
  1325. }
  1326. }
  1327. }
  1328. return request.EnableAutoStreamCopy;
  1329. }
  1330. private bool CanStreamCopyAudio(StreamRequest request, MediaStream audioStream, List<string> supportedAudioCodecs)
  1331. {
  1332. // Source and target codecs must match
  1333. if (string.IsNullOrEmpty(audioStream.Codec) || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparer.OrdinalIgnoreCase))
  1334. {
  1335. return false;
  1336. }
  1337. // Video bitrate must fall within requested value
  1338. if (request.AudioBitRate.HasValue)
  1339. {
  1340. if (!audioStream.BitRate.HasValue || audioStream.BitRate.Value > request.AudioBitRate.Value)
  1341. {
  1342. return false;
  1343. }
  1344. }
  1345. // Channels must fall within requested value
  1346. var channels = request.AudioChannels ?? request.MaxAudioChannels;
  1347. if (channels.HasValue)
  1348. {
  1349. if (!audioStream.Channels.HasValue || audioStream.Channels.Value > channels.Value)
  1350. {
  1351. return false;
  1352. }
  1353. }
  1354. // Sample rate must fall within requested value
  1355. if (request.AudioSampleRate.HasValue)
  1356. {
  1357. if (!audioStream.SampleRate.HasValue || audioStream.SampleRate.Value > request.AudioSampleRate.Value)
  1358. {
  1359. return false;
  1360. }
  1361. }
  1362. return true;
  1363. }
  1364. private void ApplyDeviceProfileSettings(StreamState state)
  1365. {
  1366. var profile = state.DeviceProfile;
  1367. if (profile == null)
  1368. {
  1369. // Don't use settings from the default profile.
  1370. // Only use a specific profile if it was requested.
  1371. return;
  1372. }
  1373. var audioCodec = state.Request.AudioCodec;
  1374. if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.AudioStream != null)
  1375. {
  1376. audioCodec = state.AudioStream.Codec;
  1377. }
  1378. var videoCodec = state.VideoRequest == null ? null : state.VideoRequest.VideoCodec;
  1379. if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.VideoStream != null)
  1380. {
  1381. videoCodec = state.VideoStream.Codec;
  1382. }
  1383. var mediaProfile = state.VideoRequest == null ?
  1384. profile.GetAudioMediaProfile(state.OutputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate) :
  1385. profile.GetVideoMediaProfile(state.OutputContainer,
  1386. audioCodec,
  1387. videoCodec,
  1388. state.OutputAudioBitrate,
  1389. state.OutputAudioChannels,
  1390. state.OutputWidth,
  1391. state.OutputHeight,
  1392. state.TargetVideoBitDepth,
  1393. state.OutputVideoBitrate,
  1394. state.TargetVideoProfile,
  1395. state.TargetVideoLevel,
  1396. state.TargetFramerate,
  1397. state.TargetPacketLength,
  1398. state.TargetTimestamp);
  1399. if (mediaProfile != null)
  1400. {
  1401. state.MimeType = mediaProfile.MimeType;
  1402. }
  1403. var transcodingProfile = state.VideoRequest == null ?
  1404. profile.GetAudioTranscodingProfile(state.OutputContainer, audioCodec) :
  1405. profile.GetVideoTranscodingProfile(state.OutputContainer, audioCodec, videoCodec);
  1406. if (transcodingProfile != null)
  1407. {
  1408. state.EstimateContentLength = transcodingProfile.EstimateContentLength;
  1409. state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
  1410. state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  1411. if (state.VideoRequest != null && string.IsNullOrWhiteSpace(state.VideoRequest.Profile))
  1412. {
  1413. state.VideoRequest.Profile = transcodingProfile.VideoProfile;
  1414. }
  1415. }
  1416. }
  1417. /// <summary>
  1418. /// Adds the dlna headers.
  1419. /// </summary>
  1420. /// <param name="state">The state.</param>
  1421. /// <param name="responseHeaders">The response headers.</param>
  1422. /// <param name="isStaticallyStreamed">if set to <c>true</c> [is statically streamed].</param>
  1423. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  1424. protected void AddDlnaHeaders(StreamState state, IDictionary<string, string> responseHeaders, bool isStaticallyStreamed)
  1425. {
  1426. var profile = state.DeviceProfile;
  1427. var transferMode = GetHeader("transferMode.dlna.org");
  1428. responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode;
  1429. responseHeaders["realTimeInfo.dlna.org"] = "DLNA.ORG_TLAG=*";
  1430. if (state.RunTimeTicks.HasValue && !isStaticallyStreamed && profile != null)
  1431. {
  1432. AddTimeSeekResponseHeaders(state, responseHeaders);
  1433. }
  1434. if (profile == null)
  1435. {
  1436. profile = DlnaManager.GetDefaultProfile();
  1437. }
  1438. var audioCodec = state.Request.AudioCodec;
  1439. if (state.VideoRequest == null)
  1440. {
  1441. responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile)
  1442. .BuildAudioHeader(
  1443. state.OutputContainer,
  1444. audioCodec,
  1445. state.OutputAudioBitrate,
  1446. state.OutputAudioSampleRate,
  1447. state.OutputAudioChannels,
  1448. isStaticallyStreamed,
  1449. state.RunTimeTicks,
  1450. state.TranscodeSeekInfo
  1451. );
  1452. }
  1453. else
  1454. {
  1455. if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.AudioStream != null)
  1456. {
  1457. audioCodec = state.AudioStream.Codec;
  1458. }
  1459. var videoCodec = state.VideoRequest == null ? null : state.VideoRequest.VideoCodec;
  1460. if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.VideoStream != null)
  1461. {
  1462. videoCodec = state.VideoStream.Codec;
  1463. }
  1464. responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile)
  1465. .BuildVideoHeader(
  1466. state.OutputContainer,
  1467. videoCodec,
  1468. audioCodec,
  1469. state.OutputWidth,
  1470. state.OutputHeight,
  1471. state.TargetVideoBitDepth,
  1472. state.OutputVideoBitrate,
  1473. state.OutputAudioBitrate,
  1474. state.OutputAudioChannels,
  1475. state.TargetTimestamp,
  1476. isStaticallyStreamed,
  1477. state.RunTimeTicks,
  1478. state.TargetVideoProfile,
  1479. state.TargetVideoLevel,
  1480. state.TargetFramerate,
  1481. state.TargetPacketLength,
  1482. state.TranscodeSeekInfo
  1483. );
  1484. }
  1485. foreach (var item in responseHeaders)
  1486. {
  1487. Request.Response.AddHeader(item.Key, item.Value);
  1488. }
  1489. }
  1490. private void AddTimeSeekResponseHeaders(StreamState state, IDictionary<string, string> responseHeaders)
  1491. {
  1492. var runtimeSeconds = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds.ToString(UsCulture);
  1493. var startSeconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds.ToString(UsCulture);
  1494. responseHeaders["TimeSeekRange.dlna.org"] = string.Format("npt={0}-{1}/{1}", startSeconds, runtimeSeconds);
  1495. responseHeaders["X-AvailableSeekRange"] = string.Format("1 npt={0}-{1}", startSeconds, runtimeSeconds);
  1496. }
  1497. /// <summary>
  1498. /// Enforces the resolution limit.
  1499. /// </summary>
  1500. /// <param name="state">The state.</param>
  1501. /// <param name="videoRequest">The video request.</param>
  1502. private void EnforceResolutionLimit(StreamState state, VideoStreamRequest videoRequest)
  1503. {
  1504. // If enabled, allow whatever the client asks for
  1505. if (ServerConfigurationManager.Configuration.AllowVideoUpscaling)
  1506. {
  1507. return;
  1508. }
  1509. // Switch the incoming params to be ceilings rather than fixed values
  1510. videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width;
  1511. videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height;
  1512. videoRequest.Width = null;
  1513. videoRequest.Height = null;
  1514. }
  1515. protected string GetInputModifier(StreamState state)
  1516. {
  1517. var inputModifier = string.Empty;
  1518. var probeSize = GetProbeSizeArgument(state.IsInputVideo, state.VideoType, state.IsoType);
  1519. inputModifier += " " + probeSize;
  1520. inputModifier = inputModifier.Trim();
  1521. inputModifier += " " + GetUserAgentParam(state.MediaPath);
  1522. inputModifier = inputModifier.Trim();
  1523. inputModifier += " " + GetFastSeekCommandLineParameter(state.Request);
  1524. inputModifier = inputModifier.Trim();
  1525. if (state.VideoRequest != null)
  1526. {
  1527. inputModifier += " -fflags genpts";
  1528. }
  1529. if (!string.IsNullOrEmpty(state.InputFormat))
  1530. {
  1531. inputModifier += " -f " + state.InputFormat;
  1532. }
  1533. if (!string.IsNullOrEmpty(state.InputVideoCodec))
  1534. {
  1535. inputModifier += " -vcodec " + state.InputVideoCodec;
  1536. }
  1537. if (!string.IsNullOrEmpty(state.InputAudioCodec))
  1538. {
  1539. inputModifier += " -acodec " + state.InputAudioCodec;
  1540. }
  1541. if (!string.IsNullOrEmpty(state.InputAudioSync))
  1542. {
  1543. inputModifier += " -async " + state.InputAudioSync;
  1544. }
  1545. if (!string.IsNullOrEmpty(state.InputVideoSync))
  1546. {
  1547. inputModifier += " -vsync " + state.InputVideoSync;
  1548. }
  1549. if (state.ReadInputAtNativeFramerate)
  1550. {
  1551. inputModifier += " -re";
  1552. }
  1553. return inputModifier;
  1554. }
  1555. /// <summary>
  1556. /// Infers the audio codec based on the url
  1557. /// </summary>
  1558. /// <param name="url">The URL.</param>
  1559. /// <returns>System.Nullable{AudioCodecs}.</returns>
  1560. private string InferAudioCodec(string url)
  1561. {
  1562. var ext = Path.GetExtension(url);
  1563. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  1564. {
  1565. return "mp3";
  1566. }
  1567. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  1568. {
  1569. return "aac";
  1570. }
  1571. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  1572. {
  1573. return "wma";
  1574. }
  1575. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  1576. {
  1577. return "vorbis";
  1578. }
  1579. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  1580. {
  1581. return "vorbis";
  1582. }
  1583. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1584. {
  1585. return "vorbis";
  1586. }
  1587. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1588. {
  1589. return "vorbis";
  1590. }
  1591. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  1592. {
  1593. return "vorbis";
  1594. }
  1595. return "copy";
  1596. }
  1597. /// <summary>
  1598. /// Infers the video codec.
  1599. /// </summary>
  1600. /// <param name="url">The URL.</param>
  1601. /// <returns>System.Nullable{VideoCodecs}.</returns>
  1602. private string InferVideoCodec(string url)
  1603. {
  1604. var ext = Path.GetExtension(url);
  1605. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  1606. {
  1607. return "wmv";
  1608. }
  1609. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  1610. {
  1611. return "vpx";
  1612. }
  1613. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  1614. {
  1615. return "theora";
  1616. }
  1617. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  1618. {
  1619. return "h264";
  1620. }
  1621. return "copy";
  1622. }
  1623. }
  1624. }