BaseEncoder.cs 39 KB

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