BaseEncoder.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.MediaEncoding;
  5. using MediaBrowser.Controller.Session;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.Dto;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.MediaInfo;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.Globalization;
  16. using System.IO;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using CommonIO;
  21. using MediaBrowser.Model.Dlna;
  22. namespace MediaBrowser.MediaEncoding.Encoder
  23. {
  24. public abstract class BaseEncoder
  25. {
  26. protected readonly MediaEncoder MediaEncoder;
  27. protected readonly ILogger Logger;
  28. protected readonly IServerConfigurationManager ConfigurationManager;
  29. protected readonly IFileSystem FileSystem;
  30. protected readonly IIsoManager IsoManager;
  31. protected readonly ILibraryManager LibraryManager;
  32. protected readonly ISessionManager SessionManager;
  33. protected readonly ISubtitleEncoder SubtitleEncoder;
  34. protected readonly IMediaSourceManager MediaSourceManager;
  35. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  36. protected BaseEncoder(MediaEncoder mediaEncoder,
  37. ILogger logger,
  38. IServerConfigurationManager configurationManager,
  39. IFileSystem fileSystem,
  40. IIsoManager isoManager,
  41. ILibraryManager libraryManager,
  42. ISessionManager sessionManager,
  43. ISubtitleEncoder subtitleEncoder,
  44. IMediaSourceManager mediaSourceManager)
  45. {
  46. MediaEncoder = mediaEncoder;
  47. Logger = logger;
  48. ConfigurationManager = configurationManager;
  49. FileSystem = fileSystem;
  50. IsoManager = isoManager;
  51. LibraryManager = libraryManager;
  52. SessionManager = sessionManager;
  53. SubtitleEncoder = subtitleEncoder;
  54. MediaSourceManager = mediaSourceManager;
  55. }
  56. public async Task<EncodingJob> Start(EncodingJobOptions options,
  57. IProgress<double> progress,
  58. CancellationToken cancellationToken)
  59. {
  60. var encodingJob = await new EncodingJobFactory(Logger, LibraryManager, MediaSourceManager, ConfigurationManager)
  61. .CreateJob(options, IsVideoEncoder, progress, cancellationToken).ConfigureAwait(false);
  62. encodingJob.OutputFilePath = GetOutputFilePath(encodingJob);
  63. FileSystem.CreateDirectory(Path.GetDirectoryName(encodingJob.OutputFilePath));
  64. encodingJob.ReadInputAtNativeFramerate = options.ReadInputAtNativeFramerate;
  65. await AcquireResources(encodingJob, cancellationToken).ConfigureAwait(false);
  66. var commandLineArgs = GetCommandLineArguments(encodingJob);
  67. if (GetEncodingOptions().EnableDebugLogging)
  68. {
  69. commandLineArgs = "-loglevel debug " + commandLineArgs;
  70. }
  71. var process = new Process
  72. {
  73. StartInfo = new ProcessStartInfo
  74. {
  75. CreateNoWindow = true,
  76. UseShellExecute = false,
  77. // Must consume both stdout and stderr or deadlocks may occur
  78. RedirectStandardOutput = true,
  79. RedirectStandardError = true,
  80. RedirectStandardInput = true,
  81. FileName = MediaEncoder.EncoderPath,
  82. Arguments = commandLineArgs,
  83. WindowStyle = ProcessWindowStyle.Hidden,
  84. ErrorDialog = false
  85. },
  86. EnableRaisingEvents = true
  87. };
  88. var workingDirectory = GetWorkingDirectory(options);
  89. if (!string.IsNullOrWhiteSpace(workingDirectory))
  90. {
  91. process.StartInfo.WorkingDirectory = workingDirectory;
  92. }
  93. OnTranscodeBeginning(encodingJob);
  94. var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;
  95. Logger.Info(commandLineLogMessage);
  96. var logFilePath = Path.Combine(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, "transcode-" + Guid.NewGuid() + ".txt");
  97. FileSystem.CreateDirectory(Path.GetDirectoryName(logFilePath));
  98. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  99. encodingJob.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);
  100. var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(commandLineLogMessage + Environment.NewLine + Environment.NewLine);
  101. await encodingJob.LogFileStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationToken).ConfigureAwait(false);
  102. process.Exited += (sender, args) => OnFfMpegProcessExited(process, encodingJob);
  103. try
  104. {
  105. process.Start();
  106. }
  107. catch (Exception ex)
  108. {
  109. Logger.ErrorException("Error starting ffmpeg", ex);
  110. OnTranscodeFailedToStart(encodingJob.OutputFilePath, encodingJob);
  111. throw;
  112. }
  113. cancellationToken.Register(() => Cancel(process, encodingJob));
  114. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  115. process.BeginOutputReadLine();
  116. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  117. new JobLogger(Logger).StartStreamingLog(encodingJob, process.StandardError.BaseStream, encodingJob.LogFileStream);
  118. // Wait for the file to exist before proceeeding
  119. while (!FileSystem.FileExists(encodingJob.OutputFilePath) && !encodingJob.HasExited)
  120. {
  121. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  122. }
  123. return encodingJob;
  124. }
  125. private void Cancel(Process process, EncodingJob job)
  126. {
  127. Logger.Info("Killing ffmpeg process for {0}", job.OutputFilePath);
  128. //process.Kill();
  129. process.StandardInput.WriteLine("q");
  130. job.IsCancelled = true;
  131. }
  132. /// <summary>
  133. /// Processes the exited.
  134. /// </summary>
  135. /// <param name="process">The process.</param>
  136. /// <param name="job">The job.</param>
  137. private void OnFfMpegProcessExited(Process process, EncodingJob job)
  138. {
  139. job.HasExited = true;
  140. Logger.Debug("Disposing stream resources");
  141. job.Dispose();
  142. var isSuccesful = false;
  143. try
  144. {
  145. var exitCode = process.ExitCode;
  146. Logger.Info("FFMpeg exited with code {0}", exitCode);
  147. isSuccesful = exitCode == 0;
  148. }
  149. catch
  150. {
  151. Logger.Error("FFMpeg exited with an error.");
  152. }
  153. if (isSuccesful && !job.IsCancelled)
  154. {
  155. job.TaskCompletionSource.TrySetResult(true);
  156. }
  157. else if (job.IsCancelled)
  158. {
  159. try
  160. {
  161. DeleteFiles(job);
  162. }
  163. catch
  164. {
  165. }
  166. try
  167. {
  168. job.TaskCompletionSource.TrySetException(new OperationCanceledException());
  169. }
  170. catch
  171. {
  172. }
  173. }
  174. else
  175. {
  176. try
  177. {
  178. DeleteFiles(job);
  179. }
  180. catch
  181. {
  182. }
  183. try
  184. {
  185. job.TaskCompletionSource.TrySetException(new ApplicationException("Encoding failed"));
  186. }
  187. catch
  188. {
  189. }
  190. }
  191. // This causes on exited to be called twice:
  192. //try
  193. //{
  194. // // Dispose the process
  195. // process.Dispose();
  196. //}
  197. //catch (Exception ex)
  198. //{
  199. // Logger.ErrorException("Error disposing ffmpeg.", ex);
  200. //}
  201. }
  202. protected virtual void DeleteFiles(EncodingJob job)
  203. {
  204. FileSystem.DeleteFile(job.OutputFilePath);
  205. }
  206. private void OnTranscodeBeginning(EncodingJob job)
  207. {
  208. job.ReportTranscodingProgress(null, null, null, null);
  209. }
  210. private void OnTranscodeFailedToStart(string path, EncodingJob job)
  211. {
  212. if (!string.IsNullOrWhiteSpace(job.Options.DeviceId))
  213. {
  214. SessionManager.ClearTranscodingInfo(job.Options.DeviceId);
  215. }
  216. }
  217. protected abstract bool IsVideoEncoder { get; }
  218. protected virtual string GetWorkingDirectory(EncodingJobOptions options)
  219. {
  220. return null;
  221. }
  222. protected EncodingOptions GetEncodingOptions()
  223. {
  224. return ConfigurationManager.GetConfiguration<EncodingOptions>("encoding");
  225. }
  226. protected abstract string GetCommandLineArguments(EncodingJob job);
  227. private string GetOutputFilePath(EncodingJob state)
  228. {
  229. var folder = string.IsNullOrWhiteSpace(state.Options.OutputDirectory) ?
  230. ConfigurationManager.ApplicationPaths.TranscodingTempPath :
  231. state.Options.OutputDirectory;
  232. var outputFileExtension = GetOutputFileExtension(state);
  233. var filename = state.Id + (outputFileExtension ?? string.Empty).ToLower();
  234. return Path.Combine(folder, filename);
  235. }
  236. protected virtual string GetOutputFileExtension(EncodingJob state)
  237. {
  238. if (!string.IsNullOrWhiteSpace(state.Options.OutputContainer))
  239. {
  240. return "." + state.Options.OutputContainer;
  241. }
  242. return null;
  243. }
  244. /// <summary>
  245. /// Gets the number of threads.
  246. /// </summary>
  247. /// <returns>System.Int32.</returns>
  248. protected int GetNumberOfThreads(EncodingJob job, bool isWebm)
  249. {
  250. return job.Options.CpuCoreLimit ?? 0;
  251. }
  252. protected string GetInputModifier(EncodingJob state, bool genPts = true)
  253. {
  254. var inputModifier = string.Empty;
  255. var probeSize = GetProbeSizeArgument(state);
  256. inputModifier += " " + probeSize;
  257. inputModifier = inputModifier.Trim();
  258. var userAgentParam = GetUserAgentParam(state);
  259. if (!string.IsNullOrWhiteSpace(userAgentParam))
  260. {
  261. inputModifier += " " + userAgentParam;
  262. }
  263. inputModifier = inputModifier.Trim();
  264. inputModifier += " " + GetFastSeekCommandLineParameter(state.Options);
  265. inputModifier = inputModifier.Trim();
  266. if (state.IsVideoRequest && genPts)
  267. {
  268. inputModifier += " -fflags +genpts";
  269. }
  270. if (!string.IsNullOrEmpty(state.InputAudioSync))
  271. {
  272. inputModifier += " -async " + state.InputAudioSync;
  273. }
  274. if (!string.IsNullOrEmpty(state.InputVideoSync))
  275. {
  276. inputModifier += " -vsync " + state.InputVideoSync;
  277. }
  278. if (state.ReadInputAtNativeFramerate)
  279. {
  280. inputModifier += " -re";
  281. }
  282. var videoDecoder = GetVideoDecoder(state);
  283. if (!string.IsNullOrWhiteSpace(videoDecoder))
  284. {
  285. inputModifier += " " + videoDecoder;
  286. }
  287. //if (state.IsVideoRequest)
  288. //{
  289. // if (string.Equals(state.OutputContainer, "mkv", StringComparison.OrdinalIgnoreCase))
  290. // {
  291. // //inputModifier += " -noaccurate_seek";
  292. // }
  293. //}
  294. return inputModifier;
  295. }
  296. /// <summary>
  297. /// Gets the name of the output video codec
  298. /// </summary>
  299. /// <param name="state">The state.</param>
  300. /// <returns>System.String.</returns>
  301. protected string GetVideoDecoder(EncodingJob state)
  302. {
  303. if (string.Equals(GetEncodingOptions().HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase))
  304. {
  305. if (state.VideoStream != null && !string.IsNullOrWhiteSpace(state.VideoStream.Codec))
  306. {
  307. switch (state.MediaSource.VideoStream.Codec.ToLower())
  308. {
  309. case "avc":
  310. case "h264":
  311. if (MediaEncoder.SupportsDecoder("h264_qsv"))
  312. {
  313. return "-c:v h264_qsv ";
  314. }
  315. break;
  316. case "mpeg2video":
  317. if (MediaEncoder.SupportsDecoder("mpeg2_qsv"))
  318. {
  319. return "-c:v mpeg2_qsv ";
  320. }
  321. break;
  322. case "vc1":
  323. if (MediaEncoder.SupportsDecoder("vc1_qsv"))
  324. {
  325. return "-c:v vc1_qsv ";
  326. }
  327. break;
  328. }
  329. }
  330. }
  331. // leave blank so ffmpeg will decide
  332. return null;
  333. }
  334. private string GetUserAgentParam(EncodingJob state)
  335. {
  336. string useragent = null;
  337. state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
  338. if (!string.IsNullOrWhiteSpace(useragent))
  339. {
  340. return "-user-agent \"" + useragent + "\"";
  341. }
  342. return string.Empty;
  343. }
  344. /// <summary>
  345. /// Gets the probe size argument.
  346. /// </summary>
  347. /// <param name="state">The state.</param>
  348. /// <returns>System.String.</returns>
  349. private string GetProbeSizeArgument(EncodingJob state)
  350. {
  351. if (state.PlayableStreamFileNames.Count > 0)
  352. {
  353. return MediaEncoder.GetProbeSizeArgument(state.PlayableStreamFileNames.ToArray(), state.InputProtocol);
  354. }
  355. return MediaEncoder.GetProbeSizeArgument(new[] { state.MediaPath }, state.InputProtocol);
  356. }
  357. /// <summary>
  358. /// Gets the fast seek command line parameter.
  359. /// </summary>
  360. /// <param name="request">The request.</param>
  361. /// <returns>System.String.</returns>
  362. /// <value>The fast seek command line parameter.</value>
  363. protected string GetFastSeekCommandLineParameter(EncodingJobOptions request)
  364. {
  365. var time = request.StartTimeTicks ?? 0;
  366. if (time > 0)
  367. {
  368. return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time));
  369. }
  370. return string.Empty;
  371. }
  372. /// <summary>
  373. /// Gets the input argument.
  374. /// </summary>
  375. /// <param name="state">The state.</param>
  376. /// <returns>System.String.</returns>
  377. protected string GetInputArgument(EncodingJob state)
  378. {
  379. var arg = string.Format("-i {0}", GetInputPathArgument(state));
  380. if (state.SubtitleStream != null && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  381. {
  382. if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  383. {
  384. if (state.VideoStream != null && state.VideoStream.Width.HasValue)
  385. {
  386. // This is hacky but not sure how to get the exact subtitle resolution
  387. double height = state.VideoStream.Width.Value;
  388. height /= 16;
  389. height *= 9;
  390. arg += string.Format(" -canvas_size {0}:{1}", state.VideoStream.Width.Value.ToString(CultureInfo.InvariantCulture), Convert.ToInt32(height).ToString(CultureInfo.InvariantCulture));
  391. }
  392. arg += " -i \"" + state.SubtitleStream.Path + "\"";
  393. }
  394. }
  395. return arg.Trim();
  396. }
  397. private string GetInputPathArgument(EncodingJob state)
  398. {
  399. var protocol = state.InputProtocol;
  400. var mediaPath = state.MediaPath ?? string.Empty;
  401. var inputPath = new[] { mediaPath };
  402. if (state.IsInputVideo)
  403. {
  404. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  405. {
  406. inputPath = MediaEncoderHelpers.GetInputArgument(FileSystem, mediaPath, state.InputProtocol, state.IsoMount, state.PlayableStreamFileNames);
  407. }
  408. }
  409. return MediaEncoder.GetInputArgument(inputPath, protocol);
  410. }
  411. private async Task AcquireResources(EncodingJob state, CancellationToken cancellationToken)
  412. {
  413. if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  414. {
  415. state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationToken).ConfigureAwait(false);
  416. }
  417. if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.LiveStreamId))
  418. {
  419. var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest
  420. {
  421. OpenToken = state.MediaSource.OpenToken
  422. }, false, cancellationToken).ConfigureAwait(false);
  423. AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.Options);
  424. if (state.IsVideoRequest)
  425. {
  426. EncodingJobFactory.TryStreamCopy(state, state.Options);
  427. }
  428. }
  429. if (state.MediaSource.BufferMs.HasValue)
  430. {
  431. await Task.Delay(state.MediaSource.BufferMs.Value, cancellationToken).ConfigureAwait(false);
  432. }
  433. }
  434. private void AttachMediaSourceInfo(EncodingJob state,
  435. MediaSourceInfo mediaSource,
  436. EncodingJobOptions videoRequest)
  437. {
  438. EncodingJobFactory.AttachMediaSourceInfo(state, mediaSource, videoRequest);
  439. }
  440. /// <summary>
  441. /// Gets the internal graphical subtitle param.
  442. /// </summary>
  443. /// <param name="state">The state.</param>
  444. /// <param name="outputVideoCodec">The output video codec.</param>
  445. /// <returns>System.String.</returns>
  446. protected string GetGraphicalSubtitleParam(EncodingJob state, string outputVideoCodec)
  447. {
  448. var outputSizeParam = string.Empty;
  449. var request = state.Options;
  450. // Add resolution params, if specified
  451. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  452. {
  453. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec).TrimEnd('"');
  454. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  455. }
  456. var videoSizeParam = string.Empty;
  457. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  458. {
  459. videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  460. }
  461. var mapPrefix = state.SubtitleStream.IsExternal ?
  462. 1 :
  463. 0;
  464. var subtitleStreamIndex = state.SubtitleStream.IsExternal
  465. ? 0
  466. : state.SubtitleStream.Index;
  467. return string.Format(" -filter_complex \"[{0}:{1}]format=yuva444p{4},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{2}] [sub] overlay{3}\"",
  468. mapPrefix.ToString(UsCulture),
  469. subtitleStreamIndex.ToString(UsCulture),
  470. state.VideoStream.Index.ToString(UsCulture),
  471. outputSizeParam,
  472. videoSizeParam);
  473. }
  474. /// <summary>
  475. /// Gets the video bitrate to specify on the command line
  476. /// </summary>
  477. /// <param name="state">The state.</param>
  478. /// <param name="videoCodec">The video codec.</param>
  479. /// <returns>System.String.</returns>
  480. protected string GetVideoQualityParam(EncodingJob state, string videoCodec)
  481. {
  482. var param = string.Empty;
  483. var isVc1 = state.VideoStream != null &&
  484. string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase);
  485. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  486. {
  487. param = "-preset superfast";
  488. param += " -crf 23";
  489. }
  490. else if (string.Equals(videoCodec, "libx265", StringComparison.OrdinalIgnoreCase))
  491. {
  492. param = "-preset fast";
  493. param += " -crf 28";
  494. }
  495. // h264 (h264_qsv)
  496. else if (string.Equals(videoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  497. {
  498. param = "-preset 7 -look_ahead 0";
  499. }
  500. // h264 (libnvenc)
  501. else if (string.Equals(videoCodec, "libnvenc", StringComparison.OrdinalIgnoreCase))
  502. {
  503. param = "-preset high-performance";
  504. }
  505. // webm
  506. else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  507. {
  508. // Values 0-3, 0 being highest quality but slower
  509. var profileScore = 0;
  510. string crf;
  511. var qmin = "0";
  512. var qmax = "50";
  513. crf = "10";
  514. if (isVc1)
  515. {
  516. profileScore++;
  517. }
  518. // Max of 2
  519. profileScore = Math.Min(profileScore, 2);
  520. // http://www.webmproject.org/docs/encoder-parameters/
  521. param = string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1} -qmin {2} -qmax {3}",
  522. profileScore.ToString(UsCulture),
  523. crf,
  524. qmin,
  525. qmax);
  526. }
  527. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  528. {
  529. param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  530. }
  531. // asf/wmv
  532. else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase))
  533. {
  534. param = "-qmin 2";
  535. }
  536. else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  537. {
  538. param = "-mbd 2";
  539. }
  540. param += GetVideoBitrateParam(state, videoCodec);
  541. var framerate = GetFramerateParam(state);
  542. if (framerate.HasValue)
  543. {
  544. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  545. }
  546. if (!string.IsNullOrEmpty(state.OutputVideoSync))
  547. {
  548. param += " -vsync " + state.OutputVideoSync;
  549. }
  550. if (!string.IsNullOrEmpty(state.Options.Profile))
  551. {
  552. param += " -profile:v " + state.Options.Profile;
  553. }
  554. var levelString = state.Options.Level.HasValue ? state.Options.Level.Value.ToString(CultureInfo.InvariantCulture) : null;
  555. if (!string.IsNullOrEmpty(levelString))
  556. {
  557. var h264Encoder = EncodingJobFactory.GetH264Encoder(state, GetEncodingOptions());
  558. // h264_qsv and libnvenc expect levels to be expressed as a decimal. libx264 supports decimal and non-decimal format
  559. if (String.Equals(h264Encoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) || String.Equals(h264Encoder, "libnvenc", StringComparison.OrdinalIgnoreCase))
  560. {
  561. switch (levelString)
  562. {
  563. case "30":
  564. param += " -level 3";
  565. break;
  566. case "31":
  567. param += " -level 3.1";
  568. break;
  569. case "32":
  570. param += " -level 3.2";
  571. break;
  572. case "40":
  573. param += " -level 4";
  574. break;
  575. case "41":
  576. param += " -level 4.1";
  577. break;
  578. case "42":
  579. param += " -level 4.2";
  580. break;
  581. case "50":
  582. param += " -level 5";
  583. break;
  584. case "51":
  585. param += " -level 5.1";
  586. break;
  587. case "52":
  588. param += " -level 5.2";
  589. break;
  590. default:
  591. param += " -level " + levelString;
  592. break;
  593. }
  594. }
  595. else
  596. {
  597. param += " -level " + levelString;
  598. }
  599. }
  600. return "-pix_fmt yuv420p " + param;
  601. }
  602. protected string GetVideoBitrateParam(EncodingJob state, string videoCodec)
  603. {
  604. var bitrate = state.OutputVideoBitrate;
  605. if (bitrate.HasValue)
  606. {
  607. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  608. {
  609. // With vpx when crf is used, b:v becomes a max rate
  610. // 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.
  611. return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture));
  612. }
  613. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  614. {
  615. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  616. }
  617. // h264
  618. return string.Format(" -b:v {0} -maxrate {0} -bufsize {1}",
  619. bitrate.Value.ToString(UsCulture),
  620. (bitrate.Value * 2).ToString(UsCulture));
  621. }
  622. return string.Empty;
  623. }
  624. protected double? GetFramerateParam(EncodingJob state)
  625. {
  626. if (state.Options != null)
  627. {
  628. if (state.Options.Framerate.HasValue)
  629. {
  630. return state.Options.Framerate.Value;
  631. }
  632. var maxrate = state.Options.MaxFramerate;
  633. if (maxrate.HasValue && state.VideoStream != null)
  634. {
  635. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  636. if (contentRate.HasValue && contentRate.Value > maxrate.Value)
  637. {
  638. return maxrate;
  639. }
  640. }
  641. }
  642. return null;
  643. }
  644. /// <summary>
  645. /// Gets the map args.
  646. /// </summary>
  647. /// <param name="state">The state.</param>
  648. /// <returns>System.String.</returns>
  649. protected virtual string GetMapArgs(EncodingJob state)
  650. {
  651. // If we don't have known media info
  652. // If input is video, use -sn to drop subtitles
  653. // Otherwise just return empty
  654. if (state.VideoStream == null && state.AudioStream == null)
  655. {
  656. return state.IsInputVideo ? "-sn" : string.Empty;
  657. }
  658. // We have media info, but we don't know the stream indexes
  659. if (state.VideoStream != null && state.VideoStream.Index == -1)
  660. {
  661. return "-sn";
  662. }
  663. // We have media info, but we don't know the stream indexes
  664. if (state.AudioStream != null && state.AudioStream.Index == -1)
  665. {
  666. return state.IsInputVideo ? "-sn" : string.Empty;
  667. }
  668. var args = string.Empty;
  669. if (state.VideoStream != null)
  670. {
  671. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  672. }
  673. else
  674. {
  675. args += "-map -0:v";
  676. }
  677. if (state.AudioStream != null)
  678. {
  679. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  680. }
  681. else
  682. {
  683. args += " -map -0:a";
  684. }
  685. if (state.SubtitleStream == null || state.Options.SubtitleMethod == SubtitleDeliveryMethod.Hls)
  686. {
  687. args += " -map -0:s";
  688. }
  689. else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  690. {
  691. args += " -map 1:0 -sn";
  692. }
  693. return args;
  694. }
  695. /// <summary>
  696. /// Determines whether the specified stream is H264.
  697. /// </summary>
  698. /// <param name="stream">The stream.</param>
  699. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  700. protected bool IsH264(MediaStream stream)
  701. {
  702. var codec = stream.Codec ?? string.Empty;
  703. return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  704. codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  705. }
  706. /// <summary>
  707. /// If we're going to put a fixed size on the command line, this will calculate it
  708. /// </summary>
  709. /// <param name="state">The state.</param>
  710. /// <param name="outputVideoCodec">The output video codec.</param>
  711. /// <param name="allowTimeStampCopy">if set to <c>true</c> [allow time stamp copy].</param>
  712. /// <returns>System.String.</returns>
  713. protected string GetOutputSizeParam(EncodingJob state,
  714. string outputVideoCodec,
  715. bool allowTimeStampCopy = true)
  716. {
  717. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  718. var request = state.Options;
  719. var filters = new List<string>();
  720. if (state.DeInterlace)
  721. {
  722. filters.Add("yadif=0:-1:0");
  723. }
  724. // If fixed dimensions were supplied
  725. if (request.Width.HasValue && request.Height.HasValue)
  726. {
  727. var widthParam = request.Width.Value.ToString(UsCulture);
  728. var heightParam = request.Height.Value.ToString(UsCulture);
  729. filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam));
  730. }
  731. // 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
  732. else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
  733. {
  734. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  735. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  736. 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));
  737. }
  738. // If a fixed width was requested
  739. else if (request.Width.HasValue)
  740. {
  741. var widthParam = request.Width.Value.ToString(UsCulture);
  742. filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam));
  743. }
  744. // If a fixed height was requested
  745. else if (request.Height.HasValue)
  746. {
  747. var heightParam = request.Height.Value.ToString(UsCulture);
  748. filters.Add(string.Format("scale=trunc(oh*a/2)*2:{0}", heightParam));
  749. }
  750. // If a max width was requested
  751. else if (request.MaxWidth.HasValue)
  752. {
  753. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  754. filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,{0})/2)*2:trunc(ow/dar/2)*2", maxWidthParam));
  755. }
  756. // If a max height was requested
  757. else if (request.MaxHeight.HasValue)
  758. {
  759. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  760. filters.Add(string.Format("scale=trunc(oh*a/2)*2:min(ih\\,{0})", maxHeightParam));
  761. }
  762. if (string.Equals(outputVideoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  763. {
  764. if (filters.Count > 1)
  765. {
  766. //filters[filters.Count - 1] += ":flags=fast_bilinear";
  767. }
  768. }
  769. var output = string.Empty;
  770. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  771. {
  772. var subParam = GetTextSubtitleParam(state);
  773. filters.Add(subParam);
  774. if (allowTimeStampCopy)
  775. {
  776. output += " -copyts";
  777. }
  778. }
  779. if (filters.Count > 0)
  780. {
  781. output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray()));
  782. }
  783. return output;
  784. }
  785. /// <summary>
  786. /// Gets the text subtitle param.
  787. /// </summary>
  788. /// <param name="state">The state.</param>
  789. /// <returns>System.String.</returns>
  790. protected string GetTextSubtitleParam(EncodingJob state)
  791. {
  792. var seconds = Math.Round(TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds);
  793. if (state.SubtitleStream.IsExternal)
  794. {
  795. var subtitlePath = state.SubtitleStream.Path;
  796. var charsetParam = string.Empty;
  797. if (!string.IsNullOrEmpty(state.SubtitleStream.Language))
  798. {
  799. var charenc = SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language, state.MediaSource.Protocol, CancellationToken.None).Result;
  800. if (!string.IsNullOrEmpty(charenc))
  801. {
  802. charsetParam = ":charenc=" + charenc;
  803. }
  804. }
  805. // TODO: Perhaps also use original_size=1920x800 ??
  806. return string.Format("subtitles=filename='{0}'{1},setpts=PTS -{2}/TB",
  807. MediaEncoder.EscapeSubtitleFilterPath(subtitlePath),
  808. charsetParam,
  809. seconds.ToString(UsCulture));
  810. }
  811. var mediaPath = state.MediaPath ?? string.Empty;
  812. return string.Format("subtitles='{0}:si={1}',setpts=PTS -{2}/TB",
  813. MediaEncoder.EscapeSubtitleFilterPath(mediaPath),
  814. state.InternalSubtitleStreamOffset.ToString(UsCulture),
  815. seconds.ToString(UsCulture));
  816. }
  817. protected string GetAudioFilterParam(EncodingJob state, bool isHls)
  818. {
  819. var volParam = string.Empty;
  820. var audioSampleRate = string.Empty;
  821. var channels = state.OutputAudioChannels;
  822. // Boost volume to 200% when downsampling from 6ch to 2ch
  823. if (channels.HasValue && channels.Value <= 2)
  824. {
  825. if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  826. {
  827. volParam = ",volume=" + GetEncodingOptions().DownMixAudioBoost.ToString(UsCulture);
  828. }
  829. }
  830. if (state.OutputAudioSampleRate.HasValue)
  831. {
  832. audioSampleRate = state.OutputAudioSampleRate.Value + ":";
  833. }
  834. var adelay = isHls ? "adelay=1," : string.Empty;
  835. var pts = string.Empty;
  836. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode && !state.Options.CopyTimestamps)
  837. {
  838. var seconds = TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds;
  839. pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(UsCulture));
  840. }
  841. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  842. adelay,
  843. audioSampleRate,
  844. volParam,
  845. pts,
  846. state.OutputAudioSync);
  847. }
  848. }
  849. }