BaseEncoder.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  121. process.BeginOutputReadLine();
  122. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  123. new JobLogger(Logger).StartStreamingLog(encodingJob, process.StandardError.BaseStream, encodingJob.LogFileStream);
  124. // Wait for the file to exist before proceeeding
  125. while (!File.Exists(encodingJob.OutputFilePath) && !encodingJob.HasExited)
  126. {
  127. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  128. }
  129. return encodingJob;
  130. }
  131. /// <summary>
  132. /// Processes the exited.
  133. /// </summary>
  134. /// <param name="process">The process.</param>
  135. /// <param name="job">The job.</param>
  136. private void OnFfMpegProcessExited(Process process, EncodingJob job)
  137. {
  138. job.HasExited = true;
  139. Logger.Debug("Disposing stream resources");
  140. job.Dispose();
  141. try
  142. {
  143. Logger.Info("FFMpeg exited with code {0}", process.ExitCode);
  144. try
  145. {
  146. job.TaskCompletionSource.TrySetResult(true);
  147. }
  148. catch
  149. {
  150. }
  151. }
  152. catch
  153. {
  154. Logger.Error("FFMpeg exited with an error.");
  155. try
  156. {
  157. job.TaskCompletionSource.TrySetException(new ApplicationException());
  158. }
  159. catch
  160. {
  161. }
  162. }
  163. // This causes on exited to be called twice:
  164. //try
  165. //{
  166. // // Dispose the process
  167. // process.Dispose();
  168. //}
  169. //catch (Exception ex)
  170. //{
  171. // Logger.ErrorException("Error disposing ffmpeg.", ex);
  172. //}
  173. }
  174. private void OnTranscodeBeginning(EncodingJob job)
  175. {
  176. job.ReportTranscodingProgress(null, null, null, null);
  177. }
  178. private void OnTranscodeFailedToStart(string path, EncodingJob job)
  179. {
  180. if (!string.IsNullOrWhiteSpace(job.Options.DeviceId))
  181. {
  182. SessionManager.ClearTranscodingInfo(job.Options.DeviceId);
  183. }
  184. }
  185. protected virtual bool IsVideoEncoder
  186. {
  187. get { return false; }
  188. }
  189. protected virtual string GetWorkingDirectory(EncodingJobOptions options)
  190. {
  191. return null;
  192. }
  193. protected EncodingOptions GetEncodingOptions()
  194. {
  195. return ConfigurationManager.GetConfiguration<EncodingOptions>("encoding");
  196. }
  197. protected abstract string GetCommandLineArguments(EncodingJob job);
  198. private string GetOutputFilePath(EncodingJob state)
  199. {
  200. var folder = ConfigurationManager.ApplicationPaths.TranscodingTempPath;
  201. var outputFileExtension = GetOutputFileExtension(state);
  202. var context = state.Options.Context;
  203. var filename = state.Id + (outputFileExtension ?? string.Empty).ToLower();
  204. return Path.Combine(folder, context.ToString().ToLower(), filename);
  205. }
  206. protected virtual string GetOutputFileExtension(EncodingJob state)
  207. {
  208. if (!string.IsNullOrWhiteSpace(state.Options.OutputContainer))
  209. {
  210. return "." + state.Options.OutputContainer;
  211. }
  212. return null;
  213. }
  214. /// <summary>
  215. /// Gets the number of threads.
  216. /// </summary>
  217. /// <returns>System.Int32.</returns>
  218. protected int GetNumberOfThreads(EncodingJob job, bool isWebm)
  219. {
  220. if (isWebm)
  221. {
  222. // Recommended per docs
  223. return Math.Max(Environment.ProcessorCount - 1, 2);
  224. }
  225. // Use more when this is true. -re will keep cpu usage under control
  226. if (job.ReadInputAtNativeFramerate)
  227. {
  228. if (isWebm)
  229. {
  230. return Math.Max(Environment.ProcessorCount - 1, 2);
  231. }
  232. return 0;
  233. }
  234. // Webm: http://www.webmproject.org/docs/encoder-parameters/
  235. // 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
  236. // for the coefficient data if the encoder selected --token-parts > 0 at encode time.
  237. switch (GetQualitySetting())
  238. {
  239. case EncodingQuality.HighSpeed:
  240. return 2;
  241. case EncodingQuality.HighQuality:
  242. return 2;
  243. case EncodingQuality.MaxQuality:
  244. return isWebm ? Math.Max(Environment.ProcessorCount - 1, 2) : 0;
  245. default:
  246. throw new Exception("Unrecognized MediaEncodingQuality value.");
  247. }
  248. }
  249. protected EncodingQuality GetQualitySetting()
  250. {
  251. var quality = GetEncodingOptions().EncodingQuality;
  252. if (quality == EncodingQuality.Auto)
  253. {
  254. var cpuCount = Environment.ProcessorCount;
  255. if (cpuCount >= 4)
  256. {
  257. //return EncodingQuality.HighQuality;
  258. }
  259. return EncodingQuality.HighSpeed;
  260. }
  261. return quality;
  262. }
  263. protected string GetInputModifier(EncodingJob job, bool genPts = true)
  264. {
  265. var inputModifier = string.Empty;
  266. var probeSize = GetProbeSizeArgument(job);
  267. inputModifier += " " + probeSize;
  268. inputModifier = inputModifier.Trim();
  269. var userAgentParam = GetUserAgentParam(job);
  270. if (!string.IsNullOrWhiteSpace(userAgentParam))
  271. {
  272. inputModifier += " " + userAgentParam;
  273. }
  274. inputModifier = inputModifier.Trim();
  275. inputModifier += " " + GetFastSeekCommandLineParameter(job.Options);
  276. inputModifier = inputModifier.Trim();
  277. if (job.IsVideoRequest && genPts)
  278. {
  279. inputModifier += " -fflags +genpts";
  280. }
  281. if (!string.IsNullOrEmpty(job.InputAudioSync))
  282. {
  283. inputModifier += " -async " + job.InputAudioSync;
  284. }
  285. if (!string.IsNullOrEmpty(job.InputVideoSync))
  286. {
  287. inputModifier += " -vsync " + job.InputVideoSync;
  288. }
  289. if (job.ReadInputAtNativeFramerate)
  290. {
  291. inputModifier += " -re";
  292. }
  293. return inputModifier;
  294. }
  295. private string GetUserAgentParam(EncodingJob job)
  296. {
  297. string useragent = null;
  298. job.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
  299. if (!string.IsNullOrWhiteSpace(useragent))
  300. {
  301. return "-user-agent \"" + useragent + "\"";
  302. }
  303. return string.Empty;
  304. }
  305. /// <summary>
  306. /// Gets the probe size argument.
  307. /// </summary>
  308. /// <param name="job">The job.</param>
  309. /// <returns>System.String.</returns>
  310. private string GetProbeSizeArgument(EncodingJob job)
  311. {
  312. if (job.PlayableStreamFileNames.Count > 0)
  313. {
  314. return MediaEncoder.GetProbeSizeArgument(job.PlayableStreamFileNames.ToArray(), job.InputProtocol);
  315. }
  316. return MediaEncoder.GetProbeSizeArgument(new[] { job.MediaPath }, job.InputProtocol);
  317. }
  318. /// <summary>
  319. /// Gets the fast seek command line parameter.
  320. /// </summary>
  321. /// <param name="options">The options.</param>
  322. /// <returns>System.String.</returns>
  323. /// <value>The fast seek command line parameter.</value>
  324. protected string GetFastSeekCommandLineParameter(EncodingJobOptions options)
  325. {
  326. var time = options.StartTimeTicks;
  327. if (time.HasValue && time.Value > 0)
  328. {
  329. return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time.Value));
  330. }
  331. return string.Empty;
  332. }
  333. /// <summary>
  334. /// Gets the input argument.
  335. /// </summary>
  336. /// <param name="job">The job.</param>
  337. /// <returns>System.String.</returns>
  338. protected string GetInputArgument(EncodingJob job)
  339. {
  340. var arg = "-i " + GetInputPathArgument(job);
  341. if (job.SubtitleStream != null)
  342. {
  343. if (job.SubtitleStream.IsExternal && !job.SubtitleStream.IsTextSubtitleStream)
  344. {
  345. arg += " -i " + job.SubtitleStream.Path;
  346. }
  347. }
  348. return arg;
  349. }
  350. private string GetInputPathArgument(EncodingJob job)
  351. {
  352. //if (job.InputProtocol == MediaProtocol.File &&
  353. // job.RunTimeTicks.HasValue &&
  354. // job.VideoType == VideoType.VideoFile &&
  355. // !string.Equals(job.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  356. //{
  357. // if (job.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks && job.IsInputVideo)
  358. // {
  359. // if (SupportsThrottleWithStream)
  360. // {
  361. // var url = "http://localhost:" + ServerConfigurationManager.Configuration.HttpServerPortNumber.ToString(UsCulture) + "/mediabrowser/videos/" + job.Request.Id + "/stream?static=true&Throttle=true&mediaSourceId=" + job.Request.MediaSourceId;
  362. // url += "&transcodingJobId=" + transcodingJobId;
  363. // return string.Format("\"{0}\"", url);
  364. // }
  365. // }
  366. //}
  367. var protocol = job.InputProtocol;
  368. var inputPath = new[] { job.MediaPath };
  369. if (job.IsInputVideo)
  370. {
  371. if (!(job.VideoType == VideoType.Iso && job.IsoMount == null))
  372. {
  373. inputPath = MediaEncoderHelpers.GetInputArgument(job.MediaPath, job.InputProtocol, job.IsoMount, job.PlayableStreamFileNames);
  374. }
  375. }
  376. return MediaEncoder.GetInputArgument(inputPath, protocol);
  377. }
  378. private async Task AcquireResources(EncodingJob state, CancellationToken cancellationToken)
  379. {
  380. if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  381. {
  382. state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationToken).ConfigureAwait(false);
  383. }
  384. if (string.IsNullOrEmpty(state.MediaPath))
  385. {
  386. var checkCodecs = false;
  387. if (string.Equals(state.ItemType, typeof(LiveTvChannel).Name))
  388. {
  389. var streamInfo = await LiveTvManager.GetChannelStream(state.Options.ItemId, cancellationToken).ConfigureAwait(false);
  390. state.LiveTvStreamId = streamInfo.Id;
  391. state.MediaPath = streamInfo.Path;
  392. state.InputProtocol = streamInfo.Protocol;
  393. await Task.Delay(1500, cancellationToken).ConfigureAwait(false);
  394. AttachMediaStreamInfo(state, streamInfo, state.Options);
  395. checkCodecs = true;
  396. }
  397. else if (string.Equals(state.ItemType, typeof(LiveTvVideoRecording).Name) ||
  398. string.Equals(state.ItemType, typeof(LiveTvAudioRecording).Name))
  399. {
  400. var streamInfo = await LiveTvManager.GetRecordingStream(state.Options.ItemId, cancellationToken).ConfigureAwait(false);
  401. state.LiveTvStreamId = streamInfo.Id;
  402. state.MediaPath = streamInfo.Path;
  403. state.InputProtocol = streamInfo.Protocol;
  404. await Task.Delay(1500, cancellationToken).ConfigureAwait(false);
  405. AttachMediaStreamInfo(state, streamInfo, state.Options);
  406. checkCodecs = true;
  407. }
  408. if (state.IsVideoRequest && checkCodecs)
  409. {
  410. if (state.VideoStream != null && EncodingJobFactory.CanStreamCopyVideo(state.Options, state.VideoStream))
  411. {
  412. state.OutputVideoCodec = "copy";
  413. }
  414. if (state.AudioStream != null && EncodingJobFactory.CanStreamCopyAudio(state.Options, state.AudioStream, state.SupportedAudioCodecs))
  415. {
  416. state.OutputAudioCodec = "copy";
  417. }
  418. }
  419. }
  420. }
  421. private void AttachMediaStreamInfo(EncodingJob state,
  422. ChannelMediaInfo mediaInfo,
  423. EncodingJobOptions videoRequest)
  424. {
  425. var mediaSource = mediaInfo.ToMediaSource();
  426. state.InputProtocol = mediaSource.Protocol;
  427. state.MediaPath = mediaSource.Path;
  428. state.RunTimeTicks = mediaSource.RunTimeTicks;
  429. state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders;
  430. state.InputBitrate = mediaSource.Bitrate;
  431. state.InputFileSize = mediaSource.Size;
  432. state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate;
  433. if (state.ReadInputAtNativeFramerate)
  434. {
  435. state.OutputAudioSync = "1000";
  436. state.InputVideoSync = "-1";
  437. state.InputAudioSync = "1";
  438. }
  439. EncodingJobFactory.AttachMediaStreamInfo(state, mediaSource.MediaStreams, videoRequest);
  440. }
  441. /// <summary>
  442. /// Gets the internal graphical subtitle param.
  443. /// </summary>
  444. /// <param name="state">The state.</param>
  445. /// <param name="outputVideoCodec">The output video codec.</param>
  446. /// <returns>System.String.</returns>
  447. protected string GetGraphicalSubtitleParam(EncodingJob state, string outputVideoCodec)
  448. {
  449. var outputSizeParam = string.Empty;
  450. var request = state.Options;
  451. // Add resolution params, if specified
  452. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  453. {
  454. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec).TrimEnd('"');
  455. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  456. }
  457. var videoSizeParam = string.Empty;
  458. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  459. {
  460. videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  461. }
  462. var mapPrefix = state.SubtitleStream.IsExternal ?
  463. 1 :
  464. 0;
  465. var subtitleStreamIndex = state.SubtitleStream.IsExternal
  466. ? 0
  467. : state.SubtitleStream.Index;
  468. return string.Format(" -filter_complex \"[{0}:{1}]format=yuva444p{4},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{2}] [sub] overlay{3}\"",
  469. mapPrefix.ToString(UsCulture),
  470. subtitleStreamIndex.ToString(UsCulture),
  471. state.VideoStream.Index.ToString(UsCulture),
  472. outputSizeParam,
  473. videoSizeParam);
  474. }
  475. /// <summary>
  476. /// Gets the video bitrate to specify on the command line
  477. /// </summary>
  478. /// <param name="state">The state.</param>
  479. /// <param name="videoCodec">The video codec.</param>
  480. /// <param name="isHls">if set to <c>true</c> [is HLS].</param>
  481. /// <returns>System.String.</returns>
  482. protected string GetVideoQualityParam(EncodingJob state, string videoCodec, bool isHls)
  483. {
  484. var param = string.Empty;
  485. var isVc1 = state.VideoStream != null &&
  486. string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase);
  487. var qualitySetting = GetQualitySetting();
  488. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  489. {
  490. switch (qualitySetting)
  491. {
  492. case EncodingQuality.HighSpeed:
  493. param = "-preset superfast";
  494. break;
  495. case EncodingQuality.HighQuality:
  496. param = "-preset superfast";
  497. break;
  498. case EncodingQuality.MaxQuality:
  499. param = "-preset superfast";
  500. break;
  501. }
  502. switch (qualitySetting)
  503. {
  504. case EncodingQuality.HighSpeed:
  505. param += " -crf 23";
  506. break;
  507. case EncodingQuality.HighQuality:
  508. param += " -crf 20";
  509. break;
  510. case EncodingQuality.MaxQuality:
  511. param += " -crf 18";
  512. break;
  513. }
  514. }
  515. // webm
  516. else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  517. {
  518. // Values 0-3, 0 being highest quality but slower
  519. var profileScore = 0;
  520. string crf;
  521. var qmin = "0";
  522. var qmax = "50";
  523. switch (qualitySetting)
  524. {
  525. case EncodingQuality.HighSpeed:
  526. crf = "10";
  527. break;
  528. case EncodingQuality.HighQuality:
  529. crf = "6";
  530. break;
  531. case EncodingQuality.MaxQuality:
  532. crf = "4";
  533. break;
  534. default:
  535. throw new ArgumentException("Unrecognized quality setting");
  536. }
  537. if (isVc1)
  538. {
  539. profileScore++;
  540. }
  541. // Max of 2
  542. profileScore = Math.Min(profileScore, 2);
  543. // http://www.webmproject.org/docs/encoder-parameters/
  544. param = string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1} -qmin {2} -qmax {3}",
  545. profileScore.ToString(UsCulture),
  546. crf,
  547. qmin,
  548. qmax);
  549. }
  550. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  551. {
  552. param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  553. }
  554. // asf/wmv
  555. else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase))
  556. {
  557. param = "-qmin 2";
  558. }
  559. else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  560. {
  561. param = "-mbd 2";
  562. }
  563. param += GetVideoBitrateParam(state, videoCodec, isHls);
  564. var framerate = GetFramerateParam(state);
  565. if (framerate.HasValue)
  566. {
  567. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  568. }
  569. if (!string.IsNullOrEmpty(state.OutputVideoSync))
  570. {
  571. param += " -vsync " + state.OutputVideoSync;
  572. }
  573. if (!string.IsNullOrEmpty(state.Options.Profile))
  574. {
  575. param += " -profile:v " + state.Options.Profile;
  576. }
  577. if (state.Options.Level.HasValue)
  578. {
  579. param += " -level " + state.Options.Level.Value.ToString(UsCulture);
  580. }
  581. return param;
  582. }
  583. protected string GetVideoBitrateParam(EncodingJob state, string videoCodec, bool isHls)
  584. {
  585. var bitrate = state.OutputVideoBitrate;
  586. if (bitrate.HasValue)
  587. {
  588. var hasFixedResolution = state.Options.HasFixedResolution;
  589. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  590. {
  591. if (hasFixedResolution)
  592. {
  593. return string.Format(" -minrate:v ({0}*.90) -maxrate:v ({0}*1.10) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture));
  594. }
  595. // With vpx when crf is used, b:v becomes a max rate
  596. // 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.
  597. return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture));
  598. }
  599. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  600. {
  601. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  602. }
  603. // H264
  604. if (hasFixedResolution)
  605. {
  606. if (isHls)
  607. {
  608. return string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture));
  609. }
  610. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  611. }
  612. return string.Format(" -maxrate {0} -bufsize {1}",
  613. bitrate.Value.ToString(UsCulture),
  614. (bitrate.Value * 2).ToString(UsCulture));
  615. }
  616. return string.Empty;
  617. }
  618. protected double? GetFramerateParam(EncodingJob state)
  619. {
  620. if (state.Options.Framerate.HasValue)
  621. {
  622. return state.Options.Framerate.Value;
  623. }
  624. var maxrate = state.Options.MaxFramerate;
  625. if (maxrate.HasValue && state.VideoStream != null)
  626. {
  627. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  628. if (contentRate.HasValue && contentRate.Value > maxrate.Value)
  629. {
  630. return maxrate;
  631. }
  632. }
  633. return null;
  634. }
  635. /// <summary>
  636. /// Gets the map args.
  637. /// </summary>
  638. /// <param name="state">The state.</param>
  639. /// <returns>System.String.</returns>
  640. protected virtual string GetMapArgs(EncodingJob state)
  641. {
  642. // If we don't have known media info
  643. // If input is video, use -sn to drop subtitles
  644. // Otherwise just return empty
  645. if (state.VideoStream == null && state.AudioStream == null)
  646. {
  647. return state.IsInputVideo ? "-sn" : string.Empty;
  648. }
  649. // We have media info, but we don't know the stream indexes
  650. if (state.VideoStream != null && state.VideoStream.Index == -1)
  651. {
  652. return "-sn";
  653. }
  654. // We have media info, but we don't know the stream indexes
  655. if (state.AudioStream != null && state.AudioStream.Index == -1)
  656. {
  657. return state.IsInputVideo ? "-sn" : string.Empty;
  658. }
  659. var args = string.Empty;
  660. if (state.VideoStream != null)
  661. {
  662. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  663. }
  664. else
  665. {
  666. args += "-map -0:v";
  667. }
  668. if (state.AudioStream != null)
  669. {
  670. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  671. }
  672. else
  673. {
  674. args += " -map -0:a";
  675. }
  676. if (state.SubtitleStream == null)
  677. {
  678. args += " -map -0:s";
  679. }
  680. else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  681. {
  682. args += " -map 1:0 -sn";
  683. }
  684. return args;
  685. }
  686. /// <summary>
  687. /// Determines whether the specified stream is H264.
  688. /// </summary>
  689. /// <param name="stream">The stream.</param>
  690. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  691. protected bool IsH264(MediaStream stream)
  692. {
  693. var codec = stream.Codec ?? string.Empty;
  694. return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  695. codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  696. }
  697. /// <summary>
  698. /// If we're going to put a fixed size on the command line, this will calculate it
  699. /// </summary>
  700. /// <param name="state">The state.</param>
  701. /// <param name="outputVideoCodec">The output video codec.</param>
  702. /// <param name="allowTimeStampCopy">if set to <c>true</c> [allow time stamp copy].</param>
  703. /// <returns>System.String.</returns>
  704. protected string GetOutputSizeParam(EncodingJob state,
  705. string outputVideoCodec,
  706. bool allowTimeStampCopy = true)
  707. {
  708. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  709. var request = state.Options;
  710. var filters = new List<string>();
  711. if (state.DeInterlace)
  712. {
  713. filters.Add("yadif=0:-1:0");
  714. }
  715. // If fixed dimensions were supplied
  716. if (request.Width.HasValue && request.Height.HasValue)
  717. {
  718. var widthParam = request.Width.Value.ToString(UsCulture);
  719. var heightParam = request.Height.Value.ToString(UsCulture);
  720. filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam));
  721. }
  722. // 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
  723. else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
  724. {
  725. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  726. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  727. filters.Add(string.Format("scale=trunc(min(iw\\,{0})/2)*2:trunc(min((iw/dar)\\,{1})/2)*2", maxWidthParam, maxHeightParam));
  728. }
  729. // If a fixed width was requested
  730. else if (request.Width.HasValue)
  731. {
  732. var widthParam = request.Width.Value.ToString(UsCulture);
  733. filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam));
  734. }
  735. // If a fixed height was requested
  736. else if (request.Height.HasValue)
  737. {
  738. var heightParam = request.Height.Value.ToString(UsCulture);
  739. filters.Add(string.Format("scale=trunc(oh*a*2)/2:{0}", heightParam));
  740. }
  741. // If a max width was requested
  742. else if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null))
  743. {
  744. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  745. filters.Add(string.Format("scale=min(iw\\,{0}):trunc(ow/dar/2)*2", maxWidthParam));
  746. }
  747. // If a max height was requested
  748. else if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null))
  749. {
  750. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  751. filters.Add(string.Format("scale=trunc(oh*a*2)/2:min(ih\\,{0})", maxHeightParam));
  752. }
  753. else if (request.MaxWidth.HasValue ||
  754. request.MaxHeight.HasValue ||
  755. request.Width.HasValue ||
  756. request.Height.HasValue)
  757. {
  758. if (state.VideoStream != null)
  759. {
  760. // Need to perform calculations manually
  761. // Try to account for bad media info
  762. var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0;
  763. var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0;
  764. var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight);
  765. var manualWidthParam = outputSize.Width.ToString(UsCulture);
  766. var manualHeightParam = outputSize.Height.ToString(UsCulture);
  767. filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", manualWidthParam, manualHeightParam));
  768. }
  769. }
  770. var output = string.Empty;
  771. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream)
  772. {
  773. var subParam = GetTextSubtitleParam(state);
  774. filters.Add(subParam);
  775. if (allowTimeStampCopy)
  776. {
  777. output += " -copyts";
  778. }
  779. }
  780. if (filters.Count > 0)
  781. {
  782. output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray()));
  783. }
  784. return output;
  785. }
  786. /// <summary>
  787. /// Gets the text subtitle param.
  788. /// </summary>
  789. /// <param name="state">The state.</param>
  790. /// <returns>System.String.</returns>
  791. protected string GetTextSubtitleParam(EncodingJob state)
  792. {
  793. var seconds = Math.Round(TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds);
  794. if (state.SubtitleStream.IsExternal)
  795. {
  796. var subtitlePath = state.SubtitleStream.Path;
  797. var charsetParam = string.Empty;
  798. if (!string.IsNullOrEmpty(state.SubtitleStream.Language))
  799. {
  800. var charenc = SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language);
  801. if (!string.IsNullOrEmpty(charenc))
  802. {
  803. charsetParam = ":charenc=" + charenc;
  804. }
  805. }
  806. // TODO: Perhaps also use original_size=1920x800 ??
  807. return string.Format("subtitles=filename='{0}'{1},setpts=PTS -{2}/TB",
  808. subtitlePath.Replace('\\', '/').Replace(":/", "\\:/"),
  809. charsetParam,
  810. seconds.ToString(UsCulture));
  811. }
  812. return string.Format("subtitles='{0}:si={1}',setpts=PTS -{2}/TB",
  813. state.MediaPath.Replace('\\', '/').Replace(":/", "\\:/"),
  814. state.InternalSubtitleStreamOffset.ToString(UsCulture),
  815. seconds.ToString(UsCulture));
  816. }
  817. protected string GetAudioFilterParam(EncodingJob state, bool isHls)
  818. {
  819. var volParam = string.Empty;
  820. var audioSampleRate = string.Empty;
  821. var channels = state.OutputAudioChannels;
  822. // Boost volume to 200% when downsampling from 6ch to 2ch
  823. if (channels.HasValue && channels.Value <= 2)
  824. {
  825. if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  826. {
  827. volParam = ",volume=" + GetEncodingOptions().DownMixAudioBoost.ToString(UsCulture);
  828. }
  829. }
  830. if (state.OutputAudioSampleRate.HasValue)
  831. {
  832. audioSampleRate = state.OutputAudioSampleRate.Value + ":";
  833. }
  834. var adelay = isHls ? "adelay=1," : string.Empty;
  835. var pts = string.Empty;
  836. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream)
  837. {
  838. var seconds = TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds;
  839. pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(UsCulture));
  840. }
  841. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  842. adelay,
  843. audioSampleRate,
  844. volParam,
  845. pts,
  846. state.OutputAudioSync);
  847. }
  848. }
  849. }