BaseEncoder.cs 36 KB

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