2
0

BaseEncoder.cs 37 KB

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