BaseEncoder.cs 39 KB

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