BaseEncoder.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  304. {
  305. return null;
  306. }
  307. if (state.VideoStream != null && !string.IsNullOrWhiteSpace(state.VideoStream.Codec))
  308. {
  309. if (string.Equals(GetEncodingOptions().HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase))
  310. {
  311. switch (state.MediaSource.VideoStream.Codec.ToLower())
  312. {
  313. case "avc":
  314. case "h264":
  315. if (MediaEncoder.SupportsDecoder("h264_qsv"))
  316. {
  317. // Seeing stalls and failures with decoding. Not worth it compared to encoding.
  318. //return "-c:v h264_qsv ";
  319. }
  320. break;
  321. case "mpeg2video":
  322. if (MediaEncoder.SupportsDecoder("mpeg2_qsv"))
  323. {
  324. return "-c:v mpeg2_qsv ";
  325. }
  326. break;
  327. case "vc1":
  328. if (MediaEncoder.SupportsDecoder("vc1_qsv"))
  329. {
  330. return "-c:v vc1_qsv ";
  331. }
  332. break;
  333. }
  334. }
  335. }
  336. // leave blank so ffmpeg will decide
  337. return null;
  338. }
  339. private string GetUserAgentParam(EncodingJob state)
  340. {
  341. string useragent = null;
  342. state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
  343. if (!string.IsNullOrWhiteSpace(useragent))
  344. {
  345. return "-user-agent \"" + useragent + "\"";
  346. }
  347. return string.Empty;
  348. }
  349. /// <summary>
  350. /// Gets the probe size argument.
  351. /// </summary>
  352. /// <param name="state">The state.</param>
  353. /// <returns>System.String.</returns>
  354. private string GetProbeSizeArgument(EncodingJob state)
  355. {
  356. if (state.PlayableStreamFileNames.Count > 0)
  357. {
  358. return MediaEncoder.GetProbeSizeArgument(state.PlayableStreamFileNames.ToArray(), state.InputProtocol);
  359. }
  360. return MediaEncoder.GetProbeSizeArgument(new[] { state.MediaPath }, state.InputProtocol);
  361. }
  362. /// <summary>
  363. /// Gets the fast seek command line parameter.
  364. /// </summary>
  365. /// <param name="request">The request.</param>
  366. /// <returns>System.String.</returns>
  367. /// <value>The fast seek command line parameter.</value>
  368. protected string GetFastSeekCommandLineParameter(EncodingJobOptions request)
  369. {
  370. var time = request.StartTimeTicks ?? 0;
  371. if (time > 0)
  372. {
  373. return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time));
  374. }
  375. return string.Empty;
  376. }
  377. /// <summary>
  378. /// Gets the input argument.
  379. /// </summary>
  380. /// <param name="state">The state.</param>
  381. /// <returns>System.String.</returns>
  382. protected string GetInputArgument(EncodingJob state)
  383. {
  384. var arg = string.Format("-i {0}", GetInputPathArgument(state));
  385. if (state.SubtitleStream != null && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  386. {
  387. if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  388. {
  389. if (state.VideoStream != null && state.VideoStream.Width.HasValue)
  390. {
  391. // This is hacky but not sure how to get the exact subtitle resolution
  392. double height = state.VideoStream.Width.Value;
  393. height /= 16;
  394. height *= 9;
  395. arg += string.Format(" -canvas_size {0}:{1}", state.VideoStream.Width.Value.ToString(CultureInfo.InvariantCulture), Convert.ToInt32(height).ToString(CultureInfo.InvariantCulture));
  396. }
  397. arg += " -i \"" + state.SubtitleStream.Path + "\"";
  398. }
  399. }
  400. return arg.Trim();
  401. }
  402. private string GetInputPathArgument(EncodingJob state)
  403. {
  404. var protocol = state.InputProtocol;
  405. var mediaPath = state.MediaPath ?? string.Empty;
  406. var inputPath = new[] { mediaPath };
  407. if (state.IsInputVideo)
  408. {
  409. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  410. {
  411. inputPath = MediaEncoderHelpers.GetInputArgument(FileSystem, mediaPath, state.InputProtocol, state.IsoMount, state.PlayableStreamFileNames);
  412. }
  413. }
  414. return MediaEncoder.GetInputArgument(inputPath, protocol);
  415. }
  416. private async Task AcquireResources(EncodingJob state, CancellationToken cancellationToken)
  417. {
  418. if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  419. {
  420. state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationToken).ConfigureAwait(false);
  421. }
  422. if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.LiveStreamId))
  423. {
  424. var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest
  425. {
  426. OpenToken = state.MediaSource.OpenToken
  427. }, false, cancellationToken).ConfigureAwait(false);
  428. AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.Options);
  429. if (state.IsVideoRequest)
  430. {
  431. EncodingJobFactory.TryStreamCopy(state, state.Options);
  432. }
  433. }
  434. if (state.MediaSource.BufferMs.HasValue)
  435. {
  436. await Task.Delay(state.MediaSource.BufferMs.Value, cancellationToken).ConfigureAwait(false);
  437. }
  438. }
  439. private void AttachMediaSourceInfo(EncodingJob state,
  440. MediaSourceInfo mediaSource,
  441. EncodingJobOptions videoRequest)
  442. {
  443. EncodingJobFactory.AttachMediaSourceInfo(state, mediaSource, videoRequest);
  444. }
  445. /// <summary>
  446. /// Gets the internal graphical subtitle param.
  447. /// </summary>
  448. /// <param name="state">The state.</param>
  449. /// <param name="outputVideoCodec">The output video codec.</param>
  450. /// <returns>System.String.</returns>
  451. protected string GetGraphicalSubtitleParam(EncodingJob state, string outputVideoCodec)
  452. {
  453. var outputSizeParam = string.Empty;
  454. var request = state.Options;
  455. // Add resolution params, if specified
  456. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  457. {
  458. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec).TrimEnd('"');
  459. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  460. }
  461. var videoSizeParam = string.Empty;
  462. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  463. {
  464. videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  465. }
  466. var mapPrefix = state.SubtitleStream.IsExternal ?
  467. 1 :
  468. 0;
  469. var subtitleStreamIndex = state.SubtitleStream.IsExternal
  470. ? 0
  471. : state.SubtitleStream.Index;
  472. return string.Format(" -filter_complex \"[{0}:{1}]format=yuva444p{4},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{2}] [sub] overlay{3}\"",
  473. mapPrefix.ToString(UsCulture),
  474. subtitleStreamIndex.ToString(UsCulture),
  475. state.VideoStream.Index.ToString(UsCulture),
  476. outputSizeParam,
  477. videoSizeParam);
  478. }
  479. /// <summary>
  480. /// Gets the video bitrate to specify on the command line
  481. /// </summary>
  482. /// <param name="state">The state.</param>
  483. /// <param name="videoCodec">The video codec.</param>
  484. /// <returns>System.String.</returns>
  485. protected string GetVideoQualityParam(EncodingJob state, string videoCodec)
  486. {
  487. var param = string.Empty;
  488. var isVc1 = state.VideoStream != null &&
  489. string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase);
  490. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  491. {
  492. param = "-preset superfast";
  493. param += " -crf 23";
  494. }
  495. else if (string.Equals(videoCodec, "libx265", StringComparison.OrdinalIgnoreCase))
  496. {
  497. param = "-preset fast";
  498. param += " -crf 28";
  499. }
  500. // h264 (h264_qsv)
  501. else if (string.Equals(videoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  502. {
  503. param = "-preset 7 -look_ahead 0";
  504. }
  505. // h264 (libnvenc)
  506. else if (string.Equals(videoCodec, "libnvenc", StringComparison.OrdinalIgnoreCase))
  507. {
  508. param = "-preset high-performance";
  509. }
  510. // webm
  511. else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  512. {
  513. // Values 0-3, 0 being highest quality but slower
  514. var profileScore = 0;
  515. string crf;
  516. var qmin = "0";
  517. var qmax = "50";
  518. crf = "10";
  519. if (isVc1)
  520. {
  521. profileScore++;
  522. }
  523. // Max of 2
  524. profileScore = Math.Min(profileScore, 2);
  525. // http://www.webmproject.org/docs/encoder-parameters/
  526. param = string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1} -qmin {2} -qmax {3}",
  527. profileScore.ToString(UsCulture),
  528. crf,
  529. qmin,
  530. qmax);
  531. }
  532. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  533. {
  534. param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  535. }
  536. // asf/wmv
  537. else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase))
  538. {
  539. param = "-qmin 2";
  540. }
  541. else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  542. {
  543. param = "-mbd 2";
  544. }
  545. param += GetVideoBitrateParam(state, videoCodec);
  546. var framerate = GetFramerateParam(state);
  547. if (framerate.HasValue)
  548. {
  549. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  550. }
  551. if (!string.IsNullOrEmpty(state.OutputVideoSync))
  552. {
  553. param += " -vsync " + state.OutputVideoSync;
  554. }
  555. if (!string.IsNullOrEmpty(state.Options.Profile))
  556. {
  557. param += " -profile:v " + state.Options.Profile;
  558. }
  559. var levelString = state.Options.Level.HasValue ? state.Options.Level.Value.ToString(CultureInfo.InvariantCulture) : null;
  560. if (!string.IsNullOrEmpty(levelString))
  561. {
  562. var h264Encoder = EncodingJobFactory.GetH264Encoder(state, GetEncodingOptions());
  563. // h264_qsv and libnvenc expect levels to be expressed as a decimal. libx264 supports decimal and non-decimal format
  564. if (String.Equals(h264Encoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) || String.Equals(h264Encoder, "libnvenc", StringComparison.OrdinalIgnoreCase))
  565. {
  566. switch (levelString)
  567. {
  568. case "30":
  569. param += " -level 3";
  570. break;
  571. case "31":
  572. param += " -level 3.1";
  573. break;
  574. case "32":
  575. param += " -level 3.2";
  576. break;
  577. case "40":
  578. param += " -level 4";
  579. break;
  580. case "41":
  581. param += " -level 4.1";
  582. break;
  583. case "42":
  584. param += " -level 4.2";
  585. break;
  586. case "50":
  587. param += " -level 5";
  588. break;
  589. case "51":
  590. param += " -level 5.1";
  591. break;
  592. case "52":
  593. param += " -level 5.2";
  594. break;
  595. default:
  596. param += " -level " + levelString;
  597. break;
  598. }
  599. }
  600. else
  601. {
  602. param += " -level " + levelString;
  603. }
  604. }
  605. return "-pix_fmt yuv420p " + param;
  606. }
  607. protected string GetVideoBitrateParam(EncodingJob state, string videoCodec)
  608. {
  609. var bitrate = state.OutputVideoBitrate;
  610. if (bitrate.HasValue)
  611. {
  612. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  613. {
  614. // With vpx when crf is used, b:v becomes a max rate
  615. // 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.
  616. return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture));
  617. }
  618. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  619. {
  620. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  621. }
  622. // h264
  623. return string.Format(" -b:v {0} -maxrate {0} -bufsize {1}",
  624. bitrate.Value.ToString(UsCulture),
  625. (bitrate.Value * 2).ToString(UsCulture));
  626. }
  627. return string.Empty;
  628. }
  629. protected double? GetFramerateParam(EncodingJob state)
  630. {
  631. if (state.Options != null)
  632. {
  633. if (state.Options.Framerate.HasValue)
  634. {
  635. return state.Options.Framerate.Value;
  636. }
  637. var maxrate = state.Options.MaxFramerate;
  638. if (maxrate.HasValue && state.VideoStream != null)
  639. {
  640. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  641. if (contentRate.HasValue && contentRate.Value > maxrate.Value)
  642. {
  643. return maxrate;
  644. }
  645. }
  646. }
  647. return null;
  648. }
  649. /// <summary>
  650. /// Gets the map args.
  651. /// </summary>
  652. /// <param name="state">The state.</param>
  653. /// <returns>System.String.</returns>
  654. protected virtual string GetMapArgs(EncodingJob state)
  655. {
  656. // If we don't have known media info
  657. // If input is video, use -sn to drop subtitles
  658. // Otherwise just return empty
  659. if (state.VideoStream == null && state.AudioStream == null)
  660. {
  661. return state.IsInputVideo ? "-sn" : string.Empty;
  662. }
  663. // We have media info, but we don't know the stream indexes
  664. if (state.VideoStream != null && state.VideoStream.Index == -1)
  665. {
  666. return "-sn";
  667. }
  668. // We have media info, but we don't know the stream indexes
  669. if (state.AudioStream != null && state.AudioStream.Index == -1)
  670. {
  671. return state.IsInputVideo ? "-sn" : string.Empty;
  672. }
  673. var args = string.Empty;
  674. if (state.VideoStream != null)
  675. {
  676. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  677. }
  678. else
  679. {
  680. args += "-map -0:v";
  681. }
  682. if (state.AudioStream != null)
  683. {
  684. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  685. }
  686. else
  687. {
  688. args += " -map -0:a";
  689. }
  690. if (state.SubtitleStream == null || state.Options.SubtitleMethod == SubtitleDeliveryMethod.Hls)
  691. {
  692. args += " -map -0:s";
  693. }
  694. else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  695. {
  696. args += " -map 1:0 -sn";
  697. }
  698. return args;
  699. }
  700. /// <summary>
  701. /// Determines whether the specified stream is H264.
  702. /// </summary>
  703. /// <param name="stream">The stream.</param>
  704. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  705. protected bool IsH264(MediaStream stream)
  706. {
  707. var codec = stream.Codec ?? string.Empty;
  708. return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  709. codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  710. }
  711. /// <summary>
  712. /// If we're going to put a fixed size on the command line, this will calculate it
  713. /// </summary>
  714. /// <param name="state">The state.</param>
  715. /// <param name="outputVideoCodec">The output video codec.</param>
  716. /// <param name="allowTimeStampCopy">if set to <c>true</c> [allow time stamp copy].</param>
  717. /// <returns>System.String.</returns>
  718. protected string GetOutputSizeParam(EncodingJob state,
  719. string outputVideoCodec,
  720. bool allowTimeStampCopy = true)
  721. {
  722. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  723. var request = state.Options;
  724. var filters = new List<string>();
  725. if (state.DeInterlace)
  726. {
  727. filters.Add("yadif=0:-1:0");
  728. }
  729. // If fixed dimensions were supplied
  730. if (request.Width.HasValue && request.Height.HasValue)
  731. {
  732. var widthParam = request.Width.Value.ToString(UsCulture);
  733. var heightParam = request.Height.Value.ToString(UsCulture);
  734. filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam));
  735. }
  736. // 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
  737. else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
  738. {
  739. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  740. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  741. 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));
  742. }
  743. // If a fixed width was requested
  744. else if (request.Width.HasValue)
  745. {
  746. var widthParam = request.Width.Value.ToString(UsCulture);
  747. filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam));
  748. }
  749. // If a fixed height was requested
  750. else if (request.Height.HasValue)
  751. {
  752. var heightParam = request.Height.Value.ToString(UsCulture);
  753. filters.Add(string.Format("scale=trunc(oh*a/2)*2:{0}", heightParam));
  754. }
  755. // If a max width was requested
  756. else if (request.MaxWidth.HasValue)
  757. {
  758. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  759. filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,{0})/2)*2:trunc(ow/dar/2)*2", maxWidthParam));
  760. }
  761. // If a max height was requested
  762. else if (request.MaxHeight.HasValue)
  763. {
  764. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  765. filters.Add(string.Format("scale=trunc(oh*a/2)*2:min(ih\\,{0})", maxHeightParam));
  766. }
  767. if (string.Equals(outputVideoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  768. {
  769. if (filters.Count > 1)
  770. {
  771. //filters[filters.Count - 1] += ":flags=fast_bilinear";
  772. }
  773. }
  774. var output = string.Empty;
  775. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  776. {
  777. var subParam = GetTextSubtitleParam(state);
  778. filters.Add(subParam);
  779. if (allowTimeStampCopy)
  780. {
  781. output += " -copyts";
  782. }
  783. }
  784. if (filters.Count > 0)
  785. {
  786. output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray()));
  787. }
  788. return output;
  789. }
  790. /// <summary>
  791. /// Gets the text subtitle param.
  792. /// </summary>
  793. /// <param name="state">The state.</param>
  794. /// <returns>System.String.</returns>
  795. protected string GetTextSubtitleParam(EncodingJob state)
  796. {
  797. var seconds = Math.Round(TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds);
  798. if (state.SubtitleStream.IsExternal)
  799. {
  800. var subtitlePath = state.SubtitleStream.Path;
  801. var charsetParam = string.Empty;
  802. if (!string.IsNullOrEmpty(state.SubtitleStream.Language))
  803. {
  804. var charenc = SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language, state.MediaSource.Protocol, CancellationToken.None).Result;
  805. if (!string.IsNullOrEmpty(charenc))
  806. {
  807. charsetParam = ":charenc=" + charenc;
  808. }
  809. }
  810. // TODO: Perhaps also use original_size=1920x800 ??
  811. return string.Format("subtitles=filename='{0}'{1},setpts=PTS -{2}/TB",
  812. MediaEncoder.EscapeSubtitleFilterPath(subtitlePath),
  813. charsetParam,
  814. seconds.ToString(UsCulture));
  815. }
  816. var mediaPath = state.MediaPath ?? string.Empty;
  817. return string.Format("subtitles='{0}:si={1}',setpts=PTS -{2}/TB",
  818. MediaEncoder.EscapeSubtitleFilterPath(mediaPath),
  819. state.InternalSubtitleStreamOffset.ToString(UsCulture),
  820. seconds.ToString(UsCulture));
  821. }
  822. protected string GetAudioFilterParam(EncodingJob state, bool isHls)
  823. {
  824. var volParam = string.Empty;
  825. var audioSampleRate = string.Empty;
  826. var channels = state.OutputAudioChannels;
  827. // Boost volume to 200% when downsampling from 6ch to 2ch
  828. if (channels.HasValue && channels.Value <= 2)
  829. {
  830. if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5 && !GetEncodingOptions().DownMixAudioBoost.Equals(1))
  831. {
  832. volParam = ",volume=" + GetEncodingOptions().DownMixAudioBoost.ToString(UsCulture);
  833. }
  834. }
  835. if (state.OutputAudioSampleRate.HasValue)
  836. {
  837. audioSampleRate = state.OutputAudioSampleRate.Value + ":";
  838. }
  839. var adelay = isHls ? "adelay=1," : string.Empty;
  840. var pts = string.Empty;
  841. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode && !state.Options.CopyTimestamps)
  842. {
  843. var seconds = TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds;
  844. pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(UsCulture));
  845. }
  846. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  847. adelay,
  848. audioSampleRate,
  849. volParam,
  850. pts,
  851. state.OutputAudioSync);
  852. }
  853. }
  854. }