EncodingHelper.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.MediaEncoding;
  13. using MediaBrowser.Model.Configuration;
  14. using MediaBrowser.Model.Dlna;
  15. using MediaBrowser.Model.Dto;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Logging;
  19. using MediaBrowser.Model.MediaInfo;
  20. namespace MediaBrowser.MediaEncoding.Encoder
  21. {
  22. public class EncodingHelper
  23. {
  24. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  25. private readonly IMediaEncoder _mediaEncoder;
  26. private readonly IServerConfigurationManager _config;
  27. private readonly IFileSystem _fileSystem;
  28. private readonly ISubtitleEncoder _subtitleEncoder;
  29. public EncodingHelper(IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem, ISubtitleEncoder subtitleEncoder)
  30. {
  31. _mediaEncoder = mediaEncoder;
  32. _config = config;
  33. _fileSystem = fileSystem;
  34. _subtitleEncoder = subtitleEncoder;
  35. }
  36. public string GetH264Encoder(EncodingJobInfo state, EncodingOptions encodingOptions)
  37. {
  38. var defaultEncoder = "libx264";
  39. // Only use alternative encoders for video files.
  40. // When using concat with folder rips, if the mfx session fails to initialize, ffmpeg will be stuck retrying and will not exit gracefully
  41. // Since transcoding of folder rips is expiremental anyway, it's not worth adding additional variables such as this.
  42. if (state.VideoType == VideoType.VideoFile)
  43. {
  44. var hwType = encodingOptions.HardwareAccelerationType;
  45. if (string.Equals(hwType, "qsv", StringComparison.OrdinalIgnoreCase) ||
  46. string.Equals(hwType, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  47. {
  48. return GetAvailableEncoder("h264_qsv", defaultEncoder);
  49. }
  50. if (string.Equals(hwType, "nvenc", StringComparison.OrdinalIgnoreCase))
  51. {
  52. return GetAvailableEncoder("h264_nvenc", defaultEncoder);
  53. }
  54. if (string.Equals(hwType, "h264_omx", StringComparison.OrdinalIgnoreCase))
  55. {
  56. return GetAvailableEncoder("h264_omx", defaultEncoder);
  57. }
  58. if (string.Equals(hwType, "vaapi", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(encodingOptions.VaapiDevice))
  59. {
  60. if (IsVaapiSupported(state))
  61. {
  62. return GetAvailableEncoder("h264_vaapi", defaultEncoder);
  63. }
  64. }
  65. }
  66. return defaultEncoder;
  67. }
  68. private string GetAvailableEncoder(string preferredEncoder, string defaultEncoder)
  69. {
  70. if (_mediaEncoder.SupportsEncoder(preferredEncoder))
  71. {
  72. return preferredEncoder;
  73. }
  74. return defaultEncoder;
  75. }
  76. private bool IsVaapiSupported(EncodingJobInfo state)
  77. {
  78. var videoStream = state.VideoStream;
  79. if (videoStream != null)
  80. {
  81. // vaapi will throw an error with this input
  82. // [vaapi @ 0x7faed8000960] No VAAPI support for codec mpeg4 profile -99.
  83. if (string.Equals(videoStream.Codec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  84. {
  85. if (videoStream.Level == -99 || videoStream.Level == 15)
  86. {
  87. return false;
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93. /// <summary>
  94. /// Gets the name of the output video codec
  95. /// </summary>
  96. public string GetVideoEncoder(EncodingJobInfo state, EncodingOptions encodingOptions)
  97. {
  98. var codec = state.OutputVideoCodec;
  99. if (!string.IsNullOrEmpty(codec))
  100. {
  101. if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase))
  102. {
  103. return GetH264Encoder(state, encodingOptions);
  104. }
  105. if (string.Equals(codec, "vpx", StringComparison.OrdinalIgnoreCase))
  106. {
  107. return "libvpx";
  108. }
  109. if (string.Equals(codec, "wmv", StringComparison.OrdinalIgnoreCase))
  110. {
  111. return "wmv2";
  112. }
  113. if (string.Equals(codec, "theora", StringComparison.OrdinalIgnoreCase))
  114. {
  115. return "libtheora";
  116. }
  117. return codec.ToLower();
  118. }
  119. return "copy";
  120. }
  121. /// <summary>
  122. /// Gets the user agent param.
  123. /// </summary>
  124. /// <param name="state">The state.</param>
  125. /// <returns>System.String.</returns>
  126. public string GetUserAgentParam(EncodingJobInfo state)
  127. {
  128. string useragent = null;
  129. state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
  130. if (!string.IsNullOrWhiteSpace(useragent))
  131. {
  132. return "-user-agent \"" + useragent + "\"";
  133. }
  134. return string.Empty;
  135. }
  136. public string GetInputFormat(string container)
  137. {
  138. if (string.Equals(container, "mkv", StringComparison.OrdinalIgnoreCase))
  139. {
  140. return "matroska";
  141. }
  142. return container;
  143. }
  144. public string GetDecoderFromCodec(string codec)
  145. {
  146. if (string.Equals(codec, "mp2", StringComparison.OrdinalIgnoreCase))
  147. {
  148. return null;
  149. }
  150. if (string.Equals(codec, "aac_latm", StringComparison.OrdinalIgnoreCase))
  151. {
  152. return null;
  153. }
  154. return codec;
  155. }
  156. /// <summary>
  157. /// Infers the audio codec based on the url
  158. /// </summary>
  159. /// <param name="url">The URL.</param>
  160. /// <returns>System.Nullable{AudioCodecs}.</returns>
  161. public string InferAudioCodec(string url)
  162. {
  163. var ext = Path.GetExtension(url);
  164. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  165. {
  166. return "mp3";
  167. }
  168. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  169. {
  170. return "aac";
  171. }
  172. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  173. {
  174. return "wma";
  175. }
  176. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  177. {
  178. return "vorbis";
  179. }
  180. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  181. {
  182. return "vorbis";
  183. }
  184. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  185. {
  186. return "vorbis";
  187. }
  188. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  189. {
  190. return "vorbis";
  191. }
  192. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  193. {
  194. return "vorbis";
  195. }
  196. return "copy";
  197. }
  198. /// <summary>
  199. /// Infers the video codec.
  200. /// </summary>
  201. /// <param name="url">The URL.</param>
  202. /// <returns>System.Nullable{VideoCodecs}.</returns>
  203. public string InferVideoCodec(string url)
  204. {
  205. var ext = Path.GetExtension(url);
  206. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  207. {
  208. return "wmv";
  209. }
  210. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  211. {
  212. return "vpx";
  213. }
  214. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  215. {
  216. return "theora";
  217. }
  218. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  219. {
  220. return "h264";
  221. }
  222. return "copy";
  223. }
  224. public int GetVideoProfileScore(string profile)
  225. {
  226. var list = new List<string>
  227. {
  228. "Constrained Baseline",
  229. "Baseline",
  230. "Extended",
  231. "Main",
  232. "High",
  233. "Progressive High",
  234. "Constrained High"
  235. };
  236. return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase));
  237. }
  238. public string GetInputPathArgument(EncodingJobInfo state)
  239. {
  240. var protocol = state.InputProtocol;
  241. var mediaPath = state.MediaPath ?? string.Empty;
  242. var inputPath = new[] { mediaPath };
  243. if (state.IsInputVideo)
  244. {
  245. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  246. {
  247. inputPath = MediaEncoderHelpers.GetInputArgument(_fileSystem, mediaPath, state.InputProtocol, state.IsoMount, state.PlayableStreamFileNames);
  248. }
  249. }
  250. return _mediaEncoder.GetInputArgument(inputPath, protocol);
  251. }
  252. /// <summary>
  253. /// Gets the audio encoder.
  254. /// </summary>
  255. /// <param name="state">The state.</param>
  256. /// <returns>System.String.</returns>
  257. public string GetAudioEncoder(EncodingJobInfo state)
  258. {
  259. var codec = state.OutputAudioCodec;
  260. if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase))
  261. {
  262. return "aac -strict experimental";
  263. }
  264. if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase))
  265. {
  266. return "libmp3lame";
  267. }
  268. if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase))
  269. {
  270. return "libvorbis";
  271. }
  272. if (string.Equals(codec, "wma", StringComparison.OrdinalIgnoreCase))
  273. {
  274. return "wmav2";
  275. }
  276. return codec.ToLower();
  277. }
  278. /// <summary>
  279. /// Gets the input argument.
  280. /// </summary>
  281. public string GetInputArgument(EncodingJobInfo state, EncodingOptions encodingOptions)
  282. {
  283. var request = state.BaseRequest;
  284. var arg = string.Format("-i {0}", GetInputPathArgument(state));
  285. if (state.SubtitleStream != null && request.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  286. {
  287. if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  288. {
  289. if (state.VideoStream != null && state.VideoStream.Width.HasValue)
  290. {
  291. // This is hacky but not sure how to get the exact subtitle resolution
  292. double height = state.VideoStream.Width.Value;
  293. height /= 16;
  294. height *= 9;
  295. arg += string.Format(" -canvas_size {0}:{1}", state.VideoStream.Width.Value.ToString(CultureInfo.InvariantCulture), Convert.ToInt32(height).ToString(CultureInfo.InvariantCulture));
  296. }
  297. var subtitlePath = state.SubtitleStream.Path;
  298. if (string.Equals(Path.GetExtension(subtitlePath), ".sub", StringComparison.OrdinalIgnoreCase))
  299. {
  300. var idxFile = Path.ChangeExtension(subtitlePath, ".idx");
  301. if (_fileSystem.FileExists(idxFile))
  302. {
  303. subtitlePath = idxFile;
  304. }
  305. }
  306. arg += " -i \"" + subtitlePath + "\"";
  307. }
  308. }
  309. if (state.IsVideoRequest)
  310. {
  311. if (GetVideoEncoder(state, encodingOptions).IndexOf("vaapi", StringComparison.OrdinalIgnoreCase) != -1)
  312. {
  313. var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && request.SubtitleMethod == SubtitleDeliveryMethod.Encode;
  314. var hwOutputFormat = "vaapi";
  315. if (hasGraphicalSubs)
  316. {
  317. hwOutputFormat = "yuv420p";
  318. }
  319. arg = "-hwaccel vaapi -hwaccel_output_format " + hwOutputFormat + " -vaapi_device " + encodingOptions.VaapiDevice + " " + arg;
  320. }
  321. }
  322. return arg.Trim();
  323. }
  324. /// <summary>
  325. /// Determines whether the specified stream is H264.
  326. /// </summary>
  327. /// <param name="stream">The stream.</param>
  328. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  329. public bool IsH264(MediaStream stream)
  330. {
  331. var codec = stream.Codec ?? string.Empty;
  332. return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  333. codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  334. }
  335. public string GetVideoBitrateParam(EncodingJobInfo state, string videoCodec)
  336. {
  337. var bitrate = state.OutputVideoBitrate;
  338. if (bitrate.HasValue)
  339. {
  340. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  341. {
  342. // With vpx when crf is used, b:v becomes a max rate
  343. // 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.
  344. return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(_usCulture));
  345. }
  346. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  347. {
  348. return string.Format(" -b:v {0}", bitrate.Value.ToString(_usCulture));
  349. }
  350. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  351. {
  352. // h264
  353. return string.Format(" -maxrate {0} -bufsize {1}",
  354. bitrate.Value.ToString(_usCulture),
  355. (bitrate.Value * 2).ToString(_usCulture));
  356. }
  357. // h264
  358. return string.Format(" -b:v {0} -maxrate {0} -bufsize {1}",
  359. bitrate.Value.ToString(_usCulture),
  360. (bitrate.Value * 2).ToString(_usCulture));
  361. }
  362. return string.Empty;
  363. }
  364. public string NormalizeTranscodingLevel(string videoCodec, string level)
  365. {
  366. double requestLevel;
  367. // Clients may direct play higher than level 41, but there's no reason to transcode higher
  368. if (double.TryParse(level, NumberStyles.Any, _usCulture, out requestLevel))
  369. {
  370. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  371. {
  372. if (requestLevel > 41)
  373. {
  374. return "41";
  375. }
  376. }
  377. }
  378. return level;
  379. }
  380. /// <summary>
  381. /// Gets the probe size argument.
  382. /// </summary>
  383. /// <param name="state">The state.</param>
  384. /// <returns>System.String.</returns>
  385. public string GetProbeSizeArgument(EncodingJobInfo state)
  386. {
  387. if (state.PlayableStreamFileNames.Count > 0)
  388. {
  389. return _mediaEncoder.GetProbeSizeAndAnalyzeDurationArgument(state.PlayableStreamFileNames.ToArray(), state.InputProtocol);
  390. }
  391. return _mediaEncoder.GetProbeSizeAndAnalyzeDurationArgument(new[] { state.MediaPath }, state.InputProtocol);
  392. }
  393. /// <summary>
  394. /// Gets the text subtitle param.
  395. /// </summary>
  396. /// <param name="state">The state.</param>
  397. /// <returns>System.String.</returns>
  398. public string GetTextSubtitleParam(EncodingJobInfo state)
  399. {
  400. var seconds = Math.Round(TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds);
  401. var setPtsParam = state.CopyTimestamps
  402. ? string.Empty
  403. : string.Format(",setpts=PTS -{0}/TB", seconds.ToString(_usCulture));
  404. if (state.SubtitleStream.IsExternal)
  405. {
  406. var subtitlePath = state.SubtitleStream.Path;
  407. var charsetParam = string.Empty;
  408. if (!string.IsNullOrEmpty(state.SubtitleStream.Language))
  409. {
  410. var charenc = _subtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language, state.MediaSource.Protocol, CancellationToken.None).Result;
  411. if (!string.IsNullOrEmpty(charenc))
  412. {
  413. charsetParam = ":charenc=" + charenc;
  414. }
  415. }
  416. // TODO: Perhaps also use original_size=1920x800 ??
  417. return string.Format("subtitles=filename='{0}'{1}{2}",
  418. _mediaEncoder.EscapeSubtitleFilterPath(subtitlePath),
  419. charsetParam,
  420. setPtsParam);
  421. }
  422. var mediaPath = state.MediaPath ?? string.Empty;
  423. return string.Format("subtitles='{0}:si={1}'{2}",
  424. _mediaEncoder.EscapeSubtitleFilterPath(mediaPath),
  425. state.InternalSubtitleStreamOffset.ToString(_usCulture),
  426. setPtsParam);
  427. }
  428. public double? GetFramerateParam(EncodingJobInfo state)
  429. {
  430. var request = state.BaseRequest;
  431. if (request.Framerate.HasValue)
  432. {
  433. return request.Framerate.Value;
  434. }
  435. var maxrate = request.MaxFramerate;
  436. if (maxrate.HasValue && state.VideoStream != null)
  437. {
  438. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  439. if (contentRate.HasValue && contentRate.Value > maxrate.Value)
  440. {
  441. return maxrate;
  442. }
  443. }
  444. return null;
  445. }
  446. /// <summary>
  447. /// Gets the video bitrate to specify on the command line
  448. /// </summary>
  449. public string GetVideoQualityParam(EncodingJobInfo state, string videoEncoder, EncodingOptions encodingOptions, string defaultH264Preset)
  450. {
  451. var param = string.Empty;
  452. var isVc1 = state.VideoStream != null &&
  453. string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase);
  454. if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
  455. {
  456. if (!string.IsNullOrWhiteSpace(encodingOptions.H264Preset))
  457. {
  458. param += "-preset " + encodingOptions.H264Preset;
  459. }
  460. else
  461. {
  462. param += "-preset " + defaultH264Preset;
  463. }
  464. if (encodingOptions.H264Crf >= 0 && encodingOptions.H264Crf <= 51)
  465. {
  466. param += " -crf " + encodingOptions.H264Crf.ToString(CultureInfo.InvariantCulture);
  467. }
  468. else
  469. {
  470. param += " -crf 23";
  471. }
  472. }
  473. else if (string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase))
  474. {
  475. param += "-preset fast";
  476. param += " -crf 28";
  477. }
  478. // h264 (h264_qsv)
  479. else if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  480. {
  481. param += "-preset 7 -look_ahead 0";
  482. }
  483. // h264 (h264_nvenc)
  484. else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase))
  485. {
  486. param += "-preset default";
  487. }
  488. // webm
  489. else if (string.Equals(videoEncoder, "libvpx", StringComparison.OrdinalIgnoreCase))
  490. {
  491. // Values 0-3, 0 being highest quality but slower
  492. var profileScore = 0;
  493. string crf;
  494. var qmin = "0";
  495. var qmax = "50";
  496. crf = "10";
  497. if (isVc1)
  498. {
  499. profileScore++;
  500. }
  501. // Max of 2
  502. profileScore = Math.Min(profileScore, 2);
  503. // http://www.webmproject.org/docs/encoder-parameters/
  504. param += string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1} -qmin {2} -qmax {3}",
  505. profileScore.ToString(_usCulture),
  506. crf,
  507. qmin,
  508. qmax);
  509. }
  510. else if (string.Equals(videoEncoder, "mpeg4", StringComparison.OrdinalIgnoreCase))
  511. {
  512. param += "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  513. }
  514. // asf/wmv
  515. else if (string.Equals(videoEncoder, "wmv2", StringComparison.OrdinalIgnoreCase))
  516. {
  517. param += "-qmin 2";
  518. }
  519. else if (string.Equals(videoEncoder, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  520. {
  521. param += "-mbd 2";
  522. }
  523. param += GetVideoBitrateParam(state, videoEncoder);
  524. var framerate = GetFramerateParam(state);
  525. if (framerate.HasValue)
  526. {
  527. param += string.Format(" -r {0}", framerate.Value.ToString(_usCulture));
  528. }
  529. if (!string.IsNullOrEmpty(state.OutputVideoSync))
  530. {
  531. param += " -vsync " + state.OutputVideoSync;
  532. }
  533. var request = state.BaseRequest;
  534. if (!string.IsNullOrEmpty(request.Profile))
  535. {
  536. if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase) &&
  537. !string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  538. {
  539. // not supported by h264_omx
  540. param += " -profile:v " + request.Profile;
  541. }
  542. }
  543. if (!string.IsNullOrEmpty(request.Level))
  544. {
  545. var level = NormalizeTranscodingLevel(state.OutputVideoCodec, request.Level);
  546. // h264_qsv and h264_nvenc expect levels to be expressed as a decimal. libx264 supports decimal and non-decimal format
  547. // also needed for libx264 due to https://trac.ffmpeg.org/ticket/3307
  548. if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) ||
  549. string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) ||
  550. string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
  551. {
  552. switch (level)
  553. {
  554. case "30":
  555. param += " -level 3.0";
  556. break;
  557. case "31":
  558. param += " -level 3.1";
  559. break;
  560. case "32":
  561. param += " -level 3.2";
  562. break;
  563. case "40":
  564. param += " -level 4.0";
  565. break;
  566. case "41":
  567. param += " -level 4.1";
  568. break;
  569. case "42":
  570. param += " -level 4.2";
  571. break;
  572. case "50":
  573. param += " -level 5.0";
  574. break;
  575. case "51":
  576. param += " -level 5.1";
  577. break;
  578. case "52":
  579. param += " -level 5.2";
  580. break;
  581. default:
  582. param += " -level " + level;
  583. break;
  584. }
  585. }
  586. else if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase))
  587. {
  588. param += " -level " + level;
  589. }
  590. }
  591. if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
  592. {
  593. param += " -x264opts:0 subme=0:rc_lookahead=10:me_range=4:me=dia:no_chroma_me:8x8dct=0:partitions=none";
  594. }
  595. if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase) &&
  596. !string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) &&
  597. !string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  598. {
  599. param = "-pix_fmt yuv420p " + param;
  600. }
  601. return param;
  602. }
  603. public bool CanStreamCopyVideo(EncodingJobInfo state, MediaStream videoStream)
  604. {
  605. var request = state.BaseRequest;
  606. if (videoStream.IsInterlaced)
  607. {
  608. return false;
  609. }
  610. if (videoStream.IsAnamorphic ?? false)
  611. {
  612. return false;
  613. }
  614. // Can't stream copy if we're burning in subtitles
  615. if (request.SubtitleStreamIndex.HasValue)
  616. {
  617. if (request.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  618. {
  619. return false;
  620. }
  621. }
  622. if (string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase))
  623. {
  624. if (videoStream.IsAVC.HasValue && !videoStream.IsAVC.Value && request.RequireAvc)
  625. {
  626. return false;
  627. }
  628. }
  629. // Source and target codecs must match
  630. if (string.IsNullOrEmpty(videoStream.Codec) || !state.SupportedVideoCodecs.Contains(videoStream.Codec, StringComparer.OrdinalIgnoreCase))
  631. {
  632. return false;
  633. }
  634. // If client is requesting a specific video profile, it must match the source
  635. if (!string.IsNullOrEmpty(request.Profile))
  636. {
  637. if (string.IsNullOrEmpty(videoStream.Profile))
  638. {
  639. //return false;
  640. }
  641. if (!string.IsNullOrEmpty(videoStream.Profile) && !string.Equals(request.Profile, videoStream.Profile, StringComparison.OrdinalIgnoreCase))
  642. {
  643. var currentScore = GetVideoProfileScore(videoStream.Profile);
  644. var requestedScore = GetVideoProfileScore(request.Profile);
  645. if (currentScore == -1 || currentScore > requestedScore)
  646. {
  647. return false;
  648. }
  649. }
  650. }
  651. // Video width must fall within requested value
  652. if (request.MaxWidth.HasValue)
  653. {
  654. if (!videoStream.Width.HasValue || videoStream.Width.Value > request.MaxWidth.Value)
  655. {
  656. return false;
  657. }
  658. }
  659. // Video height must fall within requested value
  660. if (request.MaxHeight.HasValue)
  661. {
  662. if (!videoStream.Height.HasValue || videoStream.Height.Value > request.MaxHeight.Value)
  663. {
  664. return false;
  665. }
  666. }
  667. // Video framerate must fall within requested value
  668. var requestedFramerate = request.MaxFramerate ?? request.Framerate;
  669. if (requestedFramerate.HasValue)
  670. {
  671. var videoFrameRate = videoStream.AverageFrameRate ?? videoStream.RealFrameRate;
  672. if (!videoFrameRate.HasValue || videoFrameRate.Value > requestedFramerate.Value)
  673. {
  674. return false;
  675. }
  676. }
  677. // Video bitrate must fall within requested value
  678. if (request.VideoBitRate.HasValue)
  679. {
  680. if (!videoStream.BitRate.HasValue || videoStream.BitRate.Value > request.VideoBitRate.Value)
  681. {
  682. return false;
  683. }
  684. }
  685. if (request.MaxVideoBitDepth.HasValue)
  686. {
  687. if (videoStream.BitDepth.HasValue && videoStream.BitDepth.Value > request.MaxVideoBitDepth.Value)
  688. {
  689. return false;
  690. }
  691. }
  692. if (request.MaxRefFrames.HasValue)
  693. {
  694. if (videoStream.RefFrames.HasValue && videoStream.RefFrames.Value > request.MaxRefFrames.Value)
  695. {
  696. return false;
  697. }
  698. }
  699. // If a specific level was requested, the source must match or be less than
  700. if (!string.IsNullOrEmpty(request.Level))
  701. {
  702. double requestLevel;
  703. if (double.TryParse(request.Level, NumberStyles.Any, _usCulture, out requestLevel))
  704. {
  705. if (!videoStream.Level.HasValue)
  706. {
  707. //return false;
  708. }
  709. if (videoStream.Level.HasValue && videoStream.Level.Value > requestLevel)
  710. {
  711. return false;
  712. }
  713. }
  714. }
  715. return request.EnableAutoStreamCopy;
  716. }
  717. public bool CanStreamCopyAudio(EncodingJobInfo state, MediaStream audioStream, List<string> supportedAudioCodecs)
  718. {
  719. var request = state.BaseRequest;
  720. // Source and target codecs must match
  721. if (string.IsNullOrEmpty(audioStream.Codec) || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparer.OrdinalIgnoreCase))
  722. {
  723. return false;
  724. }
  725. // Video bitrate must fall within requested value
  726. if (request.AudioBitRate.HasValue)
  727. {
  728. if (!audioStream.BitRate.HasValue || audioStream.BitRate.Value <= 0)
  729. {
  730. return false;
  731. }
  732. if (audioStream.BitRate.Value > request.AudioBitRate.Value)
  733. {
  734. return false;
  735. }
  736. }
  737. // Channels must fall within requested value
  738. var channels = request.AudioChannels ?? request.MaxAudioChannels;
  739. if (channels.HasValue)
  740. {
  741. if (!audioStream.Channels.HasValue || audioStream.Channels.Value <= 0)
  742. {
  743. return false;
  744. }
  745. if (audioStream.Channels.Value > channels.Value)
  746. {
  747. return false;
  748. }
  749. }
  750. // Sample rate must fall within requested value
  751. if (request.AudioSampleRate.HasValue)
  752. {
  753. if (!audioStream.SampleRate.HasValue || audioStream.SampleRate.Value <= 0)
  754. {
  755. return false;
  756. }
  757. if (audioStream.SampleRate.Value > request.AudioSampleRate.Value)
  758. {
  759. return false;
  760. }
  761. }
  762. return request.EnableAutoStreamCopy;
  763. }
  764. public int? GetVideoBitrateParamValue(BaseEncodingJobOptions request, MediaStream videoStream, string outputVideoCodec)
  765. {
  766. var bitrate = request.VideoBitRate;
  767. if (videoStream != null)
  768. {
  769. var isUpscaling = request.Height.HasValue && videoStream.Height.HasValue &&
  770. request.Height.Value > videoStream.Height.Value;
  771. if (request.Width.HasValue && videoStream.Width.HasValue &&
  772. request.Width.Value > videoStream.Width.Value)
  773. {
  774. isUpscaling = true;
  775. }
  776. // Don't allow bitrate increases unless upscaling
  777. if (!isUpscaling)
  778. {
  779. if (bitrate.HasValue && videoStream.BitRate.HasValue)
  780. {
  781. bitrate = Math.Min(bitrate.Value, videoStream.BitRate.Value);
  782. }
  783. }
  784. }
  785. if (bitrate.HasValue)
  786. {
  787. var inputVideoCodec = videoStream == null ? null : videoStream.Codec;
  788. bitrate = ResolutionNormalizer.ScaleBitrate(bitrate.Value, inputVideoCodec, outputVideoCodec);
  789. // If a max bitrate was requested, don't let the scaled bitrate exceed it
  790. if (request.VideoBitRate.HasValue)
  791. {
  792. bitrate = Math.Min(bitrate.Value, request.VideoBitRate.Value);
  793. }
  794. }
  795. return bitrate;
  796. }
  797. public int? GetAudioBitrateParam(BaseEncodingJobOptions request, MediaStream audioStream)
  798. {
  799. if (request.AudioBitRate.HasValue)
  800. {
  801. // Make sure we don't request a bitrate higher than the source
  802. var currentBitrate = audioStream == null ? request.AudioBitRate.Value : audioStream.BitRate ?? request.AudioBitRate.Value;
  803. // Don't encode any higher than this
  804. return Math.Min(384000, request.AudioBitRate.Value);
  805. //return Math.Min(currentBitrate, request.AudioBitRate.Value);
  806. }
  807. return null;
  808. }
  809. public string GetAudioFilterParam(EncodingJobInfo state, EncodingOptions encodingOptions, bool isHls)
  810. {
  811. var volParam = string.Empty;
  812. var audioSampleRate = string.Empty;
  813. var channels = state.OutputAudioChannels;
  814. // Boost volume to 200% when downsampling from 6ch to 2ch
  815. if (channels.HasValue && channels.Value <= 2)
  816. {
  817. if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5 && !encodingOptions.DownMixAudioBoost.Equals(1))
  818. {
  819. volParam = ",volume=" + encodingOptions.DownMixAudioBoost.ToString(_usCulture);
  820. }
  821. }
  822. if (state.OutputAudioSampleRate.HasValue)
  823. {
  824. audioSampleRate = state.OutputAudioSampleRate.Value + ":";
  825. }
  826. var adelay = isHls ? "adelay=1," : string.Empty;
  827. var pts = string.Empty;
  828. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.BaseRequest.SubtitleMethod == SubtitleDeliveryMethod.Encode && !state.CopyTimestamps)
  829. {
  830. var seconds = TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds;
  831. pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(_usCulture));
  832. }
  833. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  834. adelay,
  835. audioSampleRate,
  836. volParam,
  837. pts,
  838. state.OutputAudioSync);
  839. }
  840. /// <summary>
  841. /// Gets the number of audio channels to specify on the command line
  842. /// </summary>
  843. /// <param name="request">The request.</param>
  844. /// <param name="audioStream">The audio stream.</param>
  845. /// <param name="outputAudioCodec">The output audio codec.</param>
  846. /// <returns>System.Nullable{System.Int32}.</returns>
  847. public int? GetNumAudioChannelsParam(BaseEncodingJobOptions request, MediaStream audioStream, string outputAudioCodec)
  848. {
  849. var inputChannels = audioStream == null
  850. ? null
  851. : audioStream.Channels;
  852. if (inputChannels <= 0)
  853. {
  854. inputChannels = null;
  855. }
  856. int? transcoderChannelLimit = null;
  857. var codec = outputAudioCodec ?? string.Empty;
  858. if (codec.IndexOf("wma", StringComparison.OrdinalIgnoreCase) != -1)
  859. {
  860. // wmav2 currently only supports two channel output
  861. transcoderChannelLimit = 2;
  862. }
  863. else if (codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
  864. {
  865. // libmp3lame currently only supports two channel output
  866. transcoderChannelLimit = 2;
  867. }
  868. else
  869. {
  870. // If we don't have any media info then limit it to 6 to prevent encoding errors due to asking for too many channels
  871. transcoderChannelLimit = 6;
  872. }
  873. var isTranscodingAudio = !string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase);
  874. int? resultChannels = null;
  875. if (isTranscodingAudio)
  876. {
  877. resultChannels = request.TranscodingMaxAudioChannels;
  878. }
  879. resultChannels = resultChannels ?? request.MaxAudioChannels ?? request.AudioChannels;
  880. if (inputChannels.HasValue)
  881. {
  882. resultChannels = resultChannels.HasValue
  883. ? Math.Min(resultChannels.Value, inputChannels.Value)
  884. : inputChannels.Value;
  885. }
  886. if (isTranscodingAudio && transcoderChannelLimit.HasValue)
  887. {
  888. resultChannels = resultChannels.HasValue
  889. ? Math.Min(resultChannels.Value, transcoderChannelLimit.Value)
  890. : transcoderChannelLimit.Value;
  891. }
  892. return resultChannels ?? request.AudioChannels;
  893. }
  894. /// <summary>
  895. /// Enforces the resolution limit.
  896. /// </summary>
  897. /// <param name="state">The state.</param>
  898. public void EnforceResolutionLimit(EncodingJobInfo state)
  899. {
  900. var videoRequest = state.BaseRequest;
  901. // Switch the incoming params to be ceilings rather than fixed values
  902. videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width;
  903. videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height;
  904. videoRequest.Width = null;
  905. videoRequest.Height = null;
  906. }
  907. /// <summary>
  908. /// Gets the fast seek command line parameter.
  909. /// </summary>
  910. /// <param name="request">The request.</param>
  911. /// <returns>System.String.</returns>
  912. /// <value>The fast seek command line parameter.</value>
  913. public string GetFastSeekCommandLineParameter(BaseEncodingJobOptions request)
  914. {
  915. var time = request.StartTimeTicks ?? 0;
  916. if (time > 0)
  917. {
  918. return string.Format("-ss {0}", _mediaEncoder.GetTimeParameter(time));
  919. }
  920. return string.Empty;
  921. }
  922. /// <summary>
  923. /// Gets the map args.
  924. /// </summary>
  925. /// <param name="state">The state.</param>
  926. /// <returns>System.String.</returns>
  927. public string GetMapArgs(EncodingJobInfo state)
  928. {
  929. // If we don't have known media info
  930. // If input is video, use -sn to drop subtitles
  931. // Otherwise just return empty
  932. if (state.VideoStream == null && state.AudioStream == null)
  933. {
  934. return state.IsInputVideo ? "-sn" : string.Empty;
  935. }
  936. // We have media info, but we don't know the stream indexes
  937. if (state.VideoStream != null && state.VideoStream.Index == -1)
  938. {
  939. return "-sn";
  940. }
  941. // We have media info, but we don't know the stream indexes
  942. if (state.AudioStream != null && state.AudioStream.Index == -1)
  943. {
  944. return state.IsInputVideo ? "-sn" : string.Empty;
  945. }
  946. var args = string.Empty;
  947. if (state.VideoStream != null)
  948. {
  949. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  950. }
  951. else
  952. {
  953. // No known video stream
  954. args += "-vn";
  955. }
  956. if (state.AudioStream != null)
  957. {
  958. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  959. }
  960. else
  961. {
  962. args += " -map -0:a";
  963. }
  964. var subtitleMethod = state.BaseRequest.SubtitleMethod;
  965. if (state.SubtitleStream == null || subtitleMethod == SubtitleDeliveryMethod.Hls)
  966. {
  967. args += " -map -0:s";
  968. }
  969. else if (subtitleMethod == SubtitleDeliveryMethod.Embed)
  970. {
  971. args += string.Format(" -map 0:{0}", state.SubtitleStream.Index);
  972. }
  973. else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  974. {
  975. args += " -map 1:0 -sn";
  976. }
  977. return args;
  978. }
  979. /// <summary>
  980. /// Determines which stream will be used for playback
  981. /// </summary>
  982. /// <param name="allStream">All stream.</param>
  983. /// <param name="desiredIndex">Index of the desired.</param>
  984. /// <param name="type">The type.</param>
  985. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  986. /// <returns>MediaStream.</returns>
  987. public MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  988. {
  989. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  990. if (desiredIndex.HasValue)
  991. {
  992. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  993. if (stream != null)
  994. {
  995. return stream;
  996. }
  997. }
  998. if (type == MediaStreamType.Video)
  999. {
  1000. streams = streams.Where(i => !string.Equals(i.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)).ToList();
  1001. }
  1002. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  1003. {
  1004. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  1005. streams.FirstOrDefault();
  1006. }
  1007. // Just return the first one
  1008. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  1009. }
  1010. /// <summary>
  1011. /// Gets the internal graphical subtitle param.
  1012. /// </summary>
  1013. /// <param name="state">The state.</param>
  1014. /// <param name="outputVideoCodec">The output video codec.</param>
  1015. /// <returns>System.String.</returns>
  1016. public string GetGraphicalSubtitleParam(EncodingJobInfo state, string outputVideoCodec)
  1017. {
  1018. var outputSizeParam = string.Empty;
  1019. var request = state.BaseRequest;
  1020. // Add resolution params, if specified
  1021. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  1022. {
  1023. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec).TrimEnd('"');
  1024. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  1025. {
  1026. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("format", StringComparison.OrdinalIgnoreCase));
  1027. }
  1028. else
  1029. {
  1030. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  1031. }
  1032. }
  1033. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase) && outputSizeParam.Length == 0)
  1034. {
  1035. outputSizeParam = ",format=nv12|vaapi,hwupload";
  1036. }
  1037. var videoSizeParam = string.Empty;
  1038. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  1039. {
  1040. videoSizeParam = string.Format("scale={0}:{1}", state.VideoStream.Width.Value.ToString(_usCulture), state.VideoStream.Height.Value.ToString(_usCulture));
  1041. }
  1042. var mapPrefix = state.SubtitleStream.IsExternal ?
  1043. 1 :
  1044. 0;
  1045. var subtitleStreamIndex = state.SubtitleStream.IsExternal
  1046. ? 0
  1047. : state.SubtitleStream.Index;
  1048. return string.Format(" -filter_complex \"[{0}:{1}]{4}[sub] ; [0:{2}] [sub] overlay{3}\"",
  1049. mapPrefix.ToString(_usCulture),
  1050. subtitleStreamIndex.ToString(_usCulture),
  1051. state.VideoStream.Index.ToString(_usCulture),
  1052. outputSizeParam,
  1053. videoSizeParam);
  1054. }
  1055. /// <summary>
  1056. /// If we're going to put a fixed size on the command line, this will calculate it
  1057. /// </summary>
  1058. /// <param name="state">The state.</param>
  1059. /// <param name="outputVideoCodec">The output video codec.</param>
  1060. /// <param name="allowTimeStampCopy">if set to <c>true</c> [allow time stamp copy].</param>
  1061. /// <returns>System.String.</returns>
  1062. public string GetOutputSizeParam(EncodingJobInfo state,
  1063. string outputVideoCodec,
  1064. bool allowTimeStampCopy = true)
  1065. {
  1066. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  1067. var request = state.BaseRequest;
  1068. var filters = new List<string>();
  1069. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  1070. {
  1071. filters.Add("format=nv12|vaapi");
  1072. filters.Add("hwupload");
  1073. }
  1074. else if (state.DeInterlace && !string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  1075. {
  1076. filters.Add("yadif=0:-1:0");
  1077. }
  1078. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  1079. {
  1080. // Work around vaapi's reduced scaling features
  1081. var scaler = "scale_vaapi";
  1082. // Given the input dimensions (inputWidth, inputHeight), determine the output dimensions
  1083. // (outputWidth, outputHeight). The user may request precise output dimensions or maximum
  1084. // output dimensions. Output dimensions are guaranteed to be even.
  1085. decimal inputWidth = Convert.ToDecimal(state.VideoStream.Width);
  1086. decimal inputHeight = Convert.ToDecimal(state.VideoStream.Height);
  1087. decimal outputWidth = request.Width.HasValue ? Convert.ToDecimal(request.Width.Value) : inputWidth;
  1088. decimal outputHeight = request.Height.HasValue ? Convert.ToDecimal(request.Height.Value) : inputHeight;
  1089. decimal maximumWidth = request.MaxWidth.HasValue ? Convert.ToDecimal(request.MaxWidth.Value) : outputWidth;
  1090. decimal maximumHeight = request.MaxHeight.HasValue ? Convert.ToDecimal(request.MaxHeight.Value) : outputHeight;
  1091. if (outputWidth > maximumWidth || outputHeight > maximumHeight)
  1092. {
  1093. var scale = Math.Min(maximumWidth / outputWidth, maximumHeight / outputHeight);
  1094. outputWidth = Math.Min(maximumWidth, Math.Truncate(outputWidth * scale));
  1095. outputHeight = Math.Min(maximumHeight, Math.Truncate(outputHeight * scale));
  1096. }
  1097. outputWidth = 2 * Math.Truncate(outputWidth / 2);
  1098. outputHeight = 2 * Math.Truncate(outputHeight / 2);
  1099. if (outputWidth != inputWidth || outputHeight != inputHeight)
  1100. {
  1101. filters.Add(string.Format("{0}=w={1}:h={2}", scaler, outputWidth.ToString(_usCulture), outputHeight.ToString(_usCulture)));
  1102. }
  1103. }
  1104. else
  1105. {
  1106. // If fixed dimensions were supplied
  1107. if (request.Width.HasValue && request.Height.HasValue)
  1108. {
  1109. var widthParam = request.Width.Value.ToString(_usCulture);
  1110. var heightParam = request.Height.Value.ToString(_usCulture);
  1111. filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam));
  1112. }
  1113. // 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
  1114. else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
  1115. {
  1116. var maxWidthParam = request.MaxWidth.Value.ToString(_usCulture);
  1117. var maxHeightParam = request.MaxHeight.Value.ToString(_usCulture);
  1118. filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,min({0}\\,{1}*dar))/2)*2:trunc(min(max(iw/dar\\,ih)\\,min({0}/dar\\,{1}))/2)*2", maxWidthParam, maxHeightParam));
  1119. }
  1120. // If a fixed width was requested
  1121. else if (request.Width.HasValue)
  1122. {
  1123. var widthParam = request.Width.Value.ToString(_usCulture);
  1124. filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam));
  1125. }
  1126. // If a fixed height was requested
  1127. else if (request.Height.HasValue)
  1128. {
  1129. var heightParam = request.Height.Value.ToString(_usCulture);
  1130. filters.Add(string.Format("scale=trunc(oh*a/2)*2:{0}", heightParam));
  1131. }
  1132. // If a max width was requested
  1133. else if (request.MaxWidth.HasValue)
  1134. {
  1135. var maxWidthParam = request.MaxWidth.Value.ToString(_usCulture);
  1136. filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,{0})/2)*2:trunc(ow/dar/2)*2", maxWidthParam));
  1137. }
  1138. // If a max height was requested
  1139. else if (request.MaxHeight.HasValue)
  1140. {
  1141. var maxHeightParam = request.MaxHeight.Value.ToString(_usCulture);
  1142. filters.Add(string.Format("scale=trunc(oh*a/2)*2:min(max(iw/dar\\,ih)\\,{0})", maxHeightParam));
  1143. }
  1144. }
  1145. var output = string.Empty;
  1146. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && request.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  1147. {
  1148. var subParam = GetTextSubtitleParam(state);
  1149. filters.Add(subParam);
  1150. if (allowTimeStampCopy)
  1151. {
  1152. output += " -copyts";
  1153. }
  1154. }
  1155. if (filters.Count > 0)
  1156. {
  1157. output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray()));
  1158. }
  1159. return output;
  1160. }
  1161. /// <summary>
  1162. /// Gets the number of threads.
  1163. /// </summary>
  1164. /// <returns>System.Int32.</returns>
  1165. public int GetNumberOfThreads(EncodingJobInfo state, EncodingOptions encodingOptions, bool isWebm)
  1166. {
  1167. var threads = GetNumberOfThreadsInternal(state, encodingOptions, isWebm);
  1168. if (state.BaseRequest.CpuCoreLimit.HasValue && state.BaseRequest.CpuCoreLimit.Value > 0)
  1169. {
  1170. threads = Math.Min(threads, state.BaseRequest.CpuCoreLimit.Value);
  1171. }
  1172. return threads;
  1173. }
  1174. public void TryStreamCopy(EncodingJobInfo state)
  1175. {
  1176. if (state.VideoStream != null && CanStreamCopyVideo(state, state.VideoStream))
  1177. {
  1178. state.OutputVideoCodec = "copy";
  1179. }
  1180. else
  1181. {
  1182. var user = state.User;
  1183. // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will be compatible or not
  1184. if (user != null && !user.Policy.EnableVideoPlaybackTranscoding)
  1185. {
  1186. state.OutputVideoCodec = "copy";
  1187. }
  1188. }
  1189. if (state.AudioStream != null && CanStreamCopyAudio(state, state.AudioStream, state.SupportedAudioCodecs))
  1190. {
  1191. state.OutputAudioCodec = "copy";
  1192. }
  1193. else
  1194. {
  1195. var user = state.User;
  1196. // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will be compatible or not
  1197. if (user != null && !user.Policy.EnableAudioPlaybackTranscoding)
  1198. {
  1199. state.OutputAudioCodec = "copy";
  1200. }
  1201. }
  1202. }
  1203. public string GetInputModifier(EncodingJobInfo state, EncodingOptions encodingOptions)
  1204. {
  1205. var inputModifier = string.Empty;
  1206. var probeSize = GetProbeSizeArgument(state);
  1207. inputModifier += " " + probeSize;
  1208. inputModifier = inputModifier.Trim();
  1209. var userAgentParam = GetUserAgentParam(state);
  1210. if (!string.IsNullOrWhiteSpace(userAgentParam))
  1211. {
  1212. inputModifier += " " + userAgentParam;
  1213. }
  1214. inputModifier = inputModifier.Trim();
  1215. inputModifier += " " + GetFastSeekCommandLineParameter(state.BaseRequest);
  1216. inputModifier = inputModifier.Trim();
  1217. //inputModifier += " -fflags +genpts+ignidx+igndts";
  1218. //if (state.IsVideoRequest && genPts)
  1219. //{
  1220. // inputModifier += " -fflags +genpts";
  1221. //}
  1222. if (!string.IsNullOrEmpty(state.InputAudioSync))
  1223. {
  1224. inputModifier += " -async " + state.InputAudioSync;
  1225. }
  1226. if (!string.IsNullOrEmpty(state.InputVideoSync))
  1227. {
  1228. inputModifier += " -vsync " + state.InputVideoSync;
  1229. }
  1230. if (state.ReadInputAtNativeFramerate)
  1231. {
  1232. inputModifier += " -re";
  1233. }
  1234. var videoDecoder = GetVideoDecoder(state, encodingOptions);
  1235. if (!string.IsNullOrWhiteSpace(videoDecoder))
  1236. {
  1237. inputModifier += " " + videoDecoder;
  1238. }
  1239. if (state.IsVideoRequest)
  1240. {
  1241. // Important: If this is ever re-enabled, make sure not to use it with wtv because it breaks seeking
  1242. if (string.Equals(state.OutputContainer, "mkv", StringComparison.OrdinalIgnoreCase) && state.CopyTimestamps)
  1243. {
  1244. //inputModifier += " -noaccurate_seek";
  1245. }
  1246. if (!string.IsNullOrWhiteSpace(state.InputContainer))
  1247. {
  1248. var inputFormat = GetInputFormat(state.InputContainer);
  1249. if (!string.IsNullOrWhiteSpace(inputFormat))
  1250. {
  1251. inputModifier += " -f " + inputFormat;
  1252. }
  1253. }
  1254. if (state.RunTimeTicks.HasValue)
  1255. {
  1256. foreach (var stream in state.MediaSource.MediaStreams)
  1257. {
  1258. if (!stream.IsExternal && stream.Type != MediaStreamType.Subtitle)
  1259. {
  1260. if (!string.IsNullOrWhiteSpace(stream.Codec) && stream.Index != -1)
  1261. {
  1262. var decoder = GetDecoderFromCodec(stream.Codec);
  1263. if (!string.IsNullOrWhiteSpace(decoder))
  1264. {
  1265. inputModifier += " -codec:" + stream.Index.ToString(_usCulture) + " " + decoder;
  1266. }
  1267. }
  1268. }
  1269. }
  1270. }
  1271. }
  1272. return inputModifier;
  1273. }
  1274. public void AttachMediaSourceInfo(EncodingJobInfo state,
  1275. MediaSourceInfo mediaSource,
  1276. string requestedUrl)
  1277. {
  1278. state.MediaPath = mediaSource.Path;
  1279. state.InputProtocol = mediaSource.Protocol;
  1280. state.InputContainer = mediaSource.Container;
  1281. state.RunTimeTicks = mediaSource.RunTimeTicks;
  1282. state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders;
  1283. if (mediaSource.VideoType.HasValue)
  1284. {
  1285. state.VideoType = mediaSource.VideoType.Value;
  1286. }
  1287. state.IsoType = mediaSource.IsoType;
  1288. state.PlayableStreamFileNames = mediaSource.PlayableStreamFileNames.ToList();
  1289. if (mediaSource.Timestamp.HasValue)
  1290. {
  1291. state.InputTimestamp = mediaSource.Timestamp.Value;
  1292. }
  1293. state.InputProtocol = mediaSource.Protocol;
  1294. state.MediaPath = mediaSource.Path;
  1295. state.RunTimeTicks = mediaSource.RunTimeTicks;
  1296. state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders;
  1297. state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate;
  1298. if (state.ReadInputAtNativeFramerate ||
  1299. mediaSource.Protocol == MediaProtocol.File && string.Equals(mediaSource.Container, "wtv", StringComparison.OrdinalIgnoreCase))
  1300. {
  1301. state.OutputAudioSync = "1000";
  1302. state.InputVideoSync = "-1";
  1303. state.InputAudioSync = "1";
  1304. }
  1305. if (string.Equals(mediaSource.Container, "wma", StringComparison.OrdinalIgnoreCase))
  1306. {
  1307. // Seeing some stuttering when transcoding wma to audio-only HLS
  1308. state.InputAudioSync = "1";
  1309. }
  1310. var mediaStreams = mediaSource.MediaStreams;
  1311. if (state.IsVideoRequest)
  1312. {
  1313. var videoRequest = state.BaseRequest;
  1314. if (string.IsNullOrEmpty(videoRequest.VideoCodec))
  1315. {
  1316. if (string.IsNullOrWhiteSpace(requestedUrl))
  1317. {
  1318. requestedUrl = "test." + videoRequest.OutputContainer;
  1319. }
  1320. videoRequest.VideoCodec = InferVideoCodec(requestedUrl);
  1321. }
  1322. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  1323. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  1324. state.SubtitleDeliveryMethod = videoRequest.SubtitleMethod;
  1325. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  1326. if (state.SubtitleStream != null && !state.SubtitleStream.IsExternal)
  1327. {
  1328. state.InternalSubtitleStreamOffset = mediaStreams.Where(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal).ToList().IndexOf(state.SubtitleStream);
  1329. }
  1330. if (state.VideoStream != null && state.VideoStream.IsInterlaced)
  1331. {
  1332. state.DeInterlace = true;
  1333. }
  1334. EnforceResolutionLimit(state);
  1335. }
  1336. else
  1337. {
  1338. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  1339. }
  1340. state.MediaSource = mediaSource;
  1341. }
  1342. /// <summary>
  1343. /// Gets the name of the output video codec
  1344. /// </summary>
  1345. protected string GetVideoDecoder(EncodingJobInfo state, EncodingOptions encodingOptions)
  1346. {
  1347. if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  1348. {
  1349. return null;
  1350. }
  1351. // Only use alternative encoders for video files.
  1352. // When using concat with folder rips, if the mfx session fails to initialize, ffmpeg will be stuck retrying and will not exit gracefully
  1353. // Since transcoding of folder rips is expiremental anyway, it's not worth adding additional variables such as this.
  1354. if (state.VideoType != VideoType.VideoFile)
  1355. {
  1356. return null;
  1357. }
  1358. if (state.VideoStream != null && !string.IsNullOrWhiteSpace(state.VideoStream.Codec))
  1359. {
  1360. if (string.Equals(encodingOptions.HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase))
  1361. {
  1362. switch (state.MediaSource.VideoStream.Codec.ToLower())
  1363. {
  1364. case "avc":
  1365. case "h264":
  1366. if (_mediaEncoder.SupportsDecoder("h264_qsv"))
  1367. {
  1368. return "-c:v h264_qsv ";
  1369. }
  1370. break;
  1371. case "mpeg2video":
  1372. if (_mediaEncoder.SupportsDecoder("mpeg2_qsv"))
  1373. {
  1374. return "-c:v mpeg2_qsv ";
  1375. }
  1376. break;
  1377. case "vc1":
  1378. if (_mediaEncoder.SupportsDecoder("vc1_qsv"))
  1379. {
  1380. return "-c:v vc1_qsv ";
  1381. }
  1382. break;
  1383. }
  1384. }
  1385. }
  1386. // leave blank so ffmpeg will decide
  1387. return null;
  1388. }
  1389. /// <summary>
  1390. /// Gets the number of threads.
  1391. /// </summary>
  1392. /// <returns>System.Int32.</returns>
  1393. private int GetNumberOfThreadsInternal(EncodingJobInfo state, EncodingOptions encodingOptions, bool isWebm)
  1394. {
  1395. var threads = encodingOptions.EncodingThreadCount;
  1396. if (isWebm)
  1397. {
  1398. // Recommended per docs
  1399. return Math.Max(Environment.ProcessorCount - 1, 2);
  1400. }
  1401. // Automatic
  1402. if (threads == -1)
  1403. {
  1404. return 0;
  1405. }
  1406. return threads;
  1407. }
  1408. }
  1409. }