2
0

BaseEncoder.cs 44 KB

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