DynamicHlsService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Channels;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Dlna;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using MediaBrowser.Model.Dlna;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.IO;
  12. using ServiceStack;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. namespace MediaBrowser.Api.Playback.Hls
  22. {
  23. /// <summary>
  24. /// Options is needed for chromecast. Threw Head in there since it's related
  25. /// </summary>
  26. [Route("/Videos/{Id}/master.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
  27. [Route("/Videos/{Id}/master.m3u8", "HEAD", Summary = "Gets a video stream using HTTP live streaming.")]
  28. public class GetMasterHlsVideoStream : VideoStreamRequest
  29. {
  30. public bool EnableAdaptiveBitrateStreaming { get; set; }
  31. public GetMasterHlsVideoStream()
  32. {
  33. EnableAdaptiveBitrateStreaming = true;
  34. }
  35. }
  36. [Route("/Videos/{Id}/main.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
  37. public class GetMainHlsVideoStream : VideoStreamRequest
  38. {
  39. }
  40. /// <summary>
  41. /// Class GetHlsVideoSegment
  42. /// </summary>
  43. [Route("/Videos/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.ts", "GET")]
  44. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  45. public class GetDynamicHlsVideoSegment : VideoStreamRequest
  46. {
  47. public string PlaylistId { get; set; }
  48. /// <summary>
  49. /// Gets or sets the segment id.
  50. /// </summary>
  51. /// <value>The segment id.</value>
  52. public string SegmentId { get; set; }
  53. }
  54. public class DynamicHlsService : BaseHlsService
  55. {
  56. public DynamicHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, ILiveTvManager liveTvManager, IDlnaManager dlnaManager, IChannelManager channelManager, ISubtitleEncoder subtitleEncoder)
  57. : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, liveTvManager, dlnaManager, channelManager, subtitleEncoder)
  58. {
  59. }
  60. public object Get(GetMasterHlsVideoStream request)
  61. {
  62. var result = GetAsync(request, "GET").Result;
  63. return result;
  64. }
  65. public object Head(GetMasterHlsVideoStream request)
  66. {
  67. var result = GetAsync(request, "HEAD").Result;
  68. return result;
  69. }
  70. public object Get(GetMainHlsVideoStream request)
  71. {
  72. var result = GetPlaylistAsync(request, "main").Result;
  73. return result;
  74. }
  75. public object Get(GetDynamicHlsVideoSegment request)
  76. {
  77. return GetDynamicSegment(request, request.SegmentId).Result;
  78. }
  79. private async Task<object> GetDynamicSegment(VideoStreamRequest request, string segmentId)
  80. {
  81. if ((request.StartTimeTicks ?? 0) > 0)
  82. {
  83. throw new ArgumentException("StartTimeTicks is not allowed.");
  84. }
  85. var cancellationTokenSource = new CancellationTokenSource();
  86. var cancellationToken = cancellationTokenSource.Token;
  87. var index = int.Parse(segmentId, NumberStyles.Integer, UsCulture);
  88. var state = await GetState(request, cancellationToken).ConfigureAwait(false);
  89. var playlistPath = Path.ChangeExtension(state.OutputFilePath, ".m3u8");
  90. var segmentPath = GetSegmentPath(playlistPath, index);
  91. if (File.Exists(segmentPath))
  92. {
  93. ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  94. return await GetSegmentResult(playlistPath, segmentPath, index, cancellationToken).ConfigureAwait(false);
  95. }
  96. await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
  97. try
  98. {
  99. if (File.Exists(segmentPath))
  100. {
  101. ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  102. return await GetSegmentResult(playlistPath, segmentPath, index, cancellationToken).ConfigureAwait(false);
  103. }
  104. else
  105. {
  106. var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath);
  107. if (currentTranscodingIndex == null || index < currentTranscodingIndex.Value || (index - currentTranscodingIndex.Value) > 4)
  108. {
  109. // If the playlist doesn't already exist, startup ffmpeg
  110. try
  111. {
  112. await ApiEntryPoint.Instance.KillTranscodingJobs(j => j.Type == TranscodingJobType.Hls && string.Equals(j.DeviceId, request.DeviceId, StringComparison.OrdinalIgnoreCase), p => !string.Equals(p, playlistPath, StringComparison.OrdinalIgnoreCase), false).ConfigureAwait(false);
  113. if (currentTranscodingIndex.HasValue)
  114. {
  115. DeleteLastFile(playlistPath, 0);
  116. }
  117. var startSeconds = index * state.SegmentLength;
  118. request.StartTimeTicks = TimeSpan.FromSeconds(startSeconds).Ticks;
  119. await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false);
  120. }
  121. catch
  122. {
  123. state.Dispose();
  124. throw;
  125. }
  126. await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false);
  127. }
  128. }
  129. }
  130. finally
  131. {
  132. ApiEntryPoint.Instance.TranscodingStartLock.Release();
  133. }
  134. Logger.Info("waiting for {0}", segmentPath);
  135. while (!File.Exists(segmentPath))
  136. {
  137. await Task.Delay(50, cancellationToken).ConfigureAwait(false);
  138. }
  139. Logger.Info("returning {0}", segmentPath);
  140. return await GetSegmentResult(playlistPath, segmentPath, index, cancellationToken).ConfigureAwait(false);
  141. }
  142. public int? GetCurrentTranscodingIndex(string playlist)
  143. {
  144. var file = GetLastTranscodingFile(playlist, FileSystem);
  145. if (file == null)
  146. {
  147. return null;
  148. }
  149. var playlistFilename = Path.GetFileNameWithoutExtension(playlist);
  150. var indexString = Path.GetFileNameWithoutExtension(file.Name).Substring(playlistFilename.Length);
  151. return int.Parse(indexString, NumberStyles.Integer, UsCulture);
  152. }
  153. private void DeleteLastFile(string path, int retryCount)
  154. {
  155. if (retryCount >= 5)
  156. {
  157. return;
  158. }
  159. var file = GetLastTranscodingFile(path, FileSystem);
  160. if (file != null)
  161. {
  162. try
  163. {
  164. File.Delete(file.FullName);
  165. }
  166. catch (IOException ex)
  167. {
  168. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, file.FullName);
  169. Thread.Sleep(100);
  170. DeleteLastFile(path, retryCount + 1);
  171. }
  172. catch (Exception ex)
  173. {
  174. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, file.FullName);
  175. }
  176. }
  177. }
  178. private static FileInfo GetLastTranscodingFile(string playlist, IFileSystem fileSystem)
  179. {
  180. var folder = Path.GetDirectoryName(playlist);
  181. try
  182. {
  183. return new DirectoryInfo(folder)
  184. .EnumerateFiles("*", SearchOption.TopDirectoryOnly)
  185. .Where(i => string.Equals(i.Extension, ".ts", StringComparison.OrdinalIgnoreCase))
  186. .OrderByDescending(fileSystem.GetLastWriteTimeUtc)
  187. .FirstOrDefault();
  188. }
  189. catch (DirectoryNotFoundException)
  190. {
  191. return null;
  192. }
  193. }
  194. protected override int GetStartNumber(StreamState state)
  195. {
  196. return GetStartNumber(state.VideoRequest);
  197. }
  198. private int GetStartNumber(VideoStreamRequest request)
  199. {
  200. var segmentId = "0";
  201. var segmentRequest = request as GetDynamicHlsVideoSegment;
  202. if (segmentRequest != null)
  203. {
  204. segmentId = segmentRequest.SegmentId;
  205. }
  206. return int.Parse(segmentId, NumberStyles.Integer, UsCulture);
  207. }
  208. private string GetSegmentPath(string playlist, int index)
  209. {
  210. var folder = Path.GetDirectoryName(playlist);
  211. var filename = Path.GetFileNameWithoutExtension(playlist);
  212. return Path.Combine(folder, filename + index.ToString(UsCulture) + ".ts");
  213. }
  214. private async Task<object> GetSegmentResult(string playlistPath, string segmentPath, int segmentIndex, CancellationToken cancellationToken)
  215. {
  216. // If all transcoding has completed, just return immediately
  217. if (!IsTranscoding(playlistPath))
  218. {
  219. return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
  220. }
  221. var segmentFilename = Path.GetFileName(segmentPath);
  222. using (var fileStream = FileSystem.GetFileStream(playlistPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  223. {
  224. using (var reader = new StreamReader(fileStream))
  225. {
  226. var text = await reader.ReadToEndAsync().ConfigureAwait(false);
  227. // If it appears in the playlist, it's done
  228. if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
  229. {
  230. return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
  231. }
  232. }
  233. }
  234. // if a different file is encoding, it's done
  235. //var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath);
  236. //if (currentTranscodingIndex > segmentIndex)
  237. //{
  238. // return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
  239. //}
  240. // Wait for the file to stop being written to, then stream it
  241. var length = new FileInfo(segmentPath).Length;
  242. var eofCount = 0;
  243. while (eofCount < 10)
  244. {
  245. var info = new FileInfo(segmentPath);
  246. if (!info.Exists)
  247. {
  248. break;
  249. }
  250. var newLength = info.Length;
  251. if (newLength == length)
  252. {
  253. eofCount++;
  254. }
  255. else
  256. {
  257. eofCount = 0;
  258. }
  259. length = newLength;
  260. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  261. }
  262. return ResultFactory.GetStaticFileResult(Request, segmentPath, FileShare.ReadWrite);
  263. }
  264. private bool IsTranscoding(string playlistPath)
  265. {
  266. var job = ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType);
  267. return job != null && !job.HasExited;
  268. }
  269. private async Task<object> GetAsync(GetMasterHlsVideoStream request, string method)
  270. {
  271. var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
  272. if (string.Equals(request.AudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
  273. {
  274. throw new ArgumentException("Audio codec copy is not allowed here.");
  275. }
  276. if (string.Equals(request.VideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  277. {
  278. throw new ArgumentException("Video codec copy is not allowed here.");
  279. }
  280. if (string.IsNullOrEmpty(request.MediaSourceId))
  281. {
  282. throw new ArgumentException("MediaSourceId is required");
  283. }
  284. var playlistText = string.Empty;
  285. if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase))
  286. {
  287. var audioBitrate = state.OutputAudioBitrate ?? 0;
  288. var videoBitrate = state.OutputVideoBitrate ?? 0;
  289. playlistText = GetMasterPlaylistFileText(state, videoBitrate + audioBitrate);
  290. }
  291. return ResultFactory.GetResult(playlistText, Common.Net.MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
  292. }
  293. private string GetMasterPlaylistFileText(StreamState state, int totalBitrate)
  294. {
  295. var builder = new StringBuilder();
  296. builder.AppendLine("#EXTM3U");
  297. var queryStringIndex = Request.RawUrl.IndexOf('?');
  298. var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
  299. // Main stream
  300. var playlistUrl = (state.RunTimeTicks ?? 0) > 0 ? "main.m3u8" : "live.m3u8";
  301. playlistUrl += queryString;
  302. var request = (GetMasterHlsVideoStream)state.Request;
  303. var subtitleStreams = state.AllMediaStreams
  304. .Where(i => i.IsTextSubtitleStream)
  305. .ToList();
  306. var subtitleGroup = subtitleStreams.Count > 0 && request.SubtitleMethod == SubtitleDeliveryMethod.Hls ?
  307. "subs" :
  308. null;
  309. AppendPlaylist(builder, playlistUrl, totalBitrate, subtitleGroup);
  310. if (EnableAdaptiveBitrateStreaming(state))
  311. {
  312. var requestedVideoBitrate = state.VideoRequest.VideoBitRate.Value;
  313. // By default, vary by just 200k
  314. var variation = GetBitrateVariation(totalBitrate);
  315. var newBitrate = totalBitrate - variation;
  316. var variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, (requestedVideoBitrate - variation));
  317. AppendPlaylist(builder, variantUrl, newBitrate, subtitleGroup);
  318. variation *= 2;
  319. newBitrate = totalBitrate - variation;
  320. variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, (requestedVideoBitrate - variation));
  321. AppendPlaylist(builder, variantUrl, newBitrate, subtitleGroup);
  322. }
  323. if (!string.IsNullOrWhiteSpace(subtitleGroup))
  324. {
  325. AddSubtitles(state, subtitleStreams, builder);
  326. }
  327. return builder.ToString();
  328. }
  329. private string ReplaceBitrate(string url, int oldValue, int newValue)
  330. {
  331. return url.Replace(
  332. "videobitrate=" + oldValue.ToString(UsCulture),
  333. "videobitrate=" + newValue.ToString(UsCulture),
  334. StringComparison.OrdinalIgnoreCase);
  335. }
  336. private void AddSubtitles(StreamState state, IEnumerable<MediaStream> subtitles, StringBuilder builder)
  337. {
  338. var selectedIndex = state.SubtitleStream == null ? (int?)null : state.SubtitleStream.Index;
  339. foreach (var stream in subtitles)
  340. {
  341. const string format = "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"subs\",NAME=\"{0}\",DEFAULT={1},FORCED={2},URI=\"{3}\",LANGUAGE=\"{4}\"";
  342. var name = stream.Language;
  343. var isDefault = selectedIndex.HasValue && selectedIndex.Value == stream.Index;
  344. var isForced = stream.IsForced;
  345. if (string.IsNullOrWhiteSpace(name)) name = stream.Codec ?? "Unknown";
  346. var url = string.Format("{0}/Subtitles/{1}/subtitles.m3u8?SegmentLength={2}",
  347. state.Request.MediaSourceId,
  348. stream.Index.ToString(UsCulture),
  349. 30.ToString(UsCulture));
  350. var line = string.Format(format,
  351. name,
  352. isDefault ? "YES" : "NO",
  353. isForced ? "YES" : "NO",
  354. url,
  355. stream.Language ?? "Unknown");
  356. builder.AppendLine(line);
  357. }
  358. }
  359. private bool EnableAdaptiveBitrateStreaming(StreamState state)
  360. {
  361. var request = state.Request as GetMasterHlsVideoStream;
  362. if (request != null && !request.EnableAdaptiveBitrateStreaming)
  363. {
  364. return false;
  365. }
  366. if (string.IsNullOrWhiteSpace(state.MediaPath))
  367. {
  368. // Opening live streams is so slow it's not even worth it
  369. return false;
  370. }
  371. return state.VideoRequest.VideoBitRate.HasValue;
  372. }
  373. private void AppendPlaylist(StringBuilder builder, string url, int bitrate, string subtitleGroup)
  374. {
  375. var header = "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=" + bitrate.ToString(UsCulture);
  376. if (!string.IsNullOrWhiteSpace(subtitleGroup))
  377. {
  378. header += string.Format(",SUBTITLES=\"{0}\"", subtitleGroup);
  379. }
  380. builder.AppendLine(header);
  381. builder.AppendLine(url);
  382. }
  383. private int GetBitrateVariation(int bitrate)
  384. {
  385. // By default, vary by just 50k
  386. var variation = 50000;
  387. if (bitrate >= 10000000)
  388. {
  389. variation = 2000000;
  390. }
  391. else if (bitrate >= 5000000)
  392. {
  393. variation = 1500000;
  394. }
  395. else if (bitrate >= 3000000)
  396. {
  397. variation = 1000000;
  398. }
  399. else if (bitrate >= 2000000)
  400. {
  401. variation = 500000;
  402. }
  403. else if (bitrate >= 1000000)
  404. {
  405. variation = 300000;
  406. }
  407. else if (bitrate >= 600000)
  408. {
  409. variation = 200000;
  410. }
  411. else if (bitrate >= 400000)
  412. {
  413. variation = 100000;
  414. }
  415. return variation;
  416. }
  417. private async Task<object> GetPlaylistAsync(VideoStreamRequest request, string name)
  418. {
  419. var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
  420. var builder = new StringBuilder();
  421. builder.AppendLine("#EXTM3U");
  422. builder.AppendLine("#EXT-X-VERSION:3");
  423. builder.AppendLine("#EXT-X-TARGETDURATION:" + state.SegmentLength.ToString(UsCulture));
  424. builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
  425. builder.AppendLine("#EXT-X-ALLOW-CACHE:NO");
  426. var queryStringIndex = Request.RawUrl.IndexOf('?');
  427. var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
  428. var seconds = TimeSpan.FromTicks(state.RunTimeTicks ?? 0).TotalSeconds;
  429. var index = 0;
  430. while (seconds > 0)
  431. {
  432. var length = seconds >= state.SegmentLength ? state.SegmentLength : seconds;
  433. builder.AppendLine("#EXTINF:" + length.ToString(UsCulture) + ",");
  434. builder.AppendLine(string.Format("hlsdynamic/{0}/{1}.ts{2}",
  435. name,
  436. index.ToString(UsCulture),
  437. queryString));
  438. seconds -= state.SegmentLength;
  439. index++;
  440. }
  441. builder.AppendLine("#EXT-X-ENDLIST");
  442. var playlistText = builder.ToString();
  443. return ResultFactory.GetResult(playlistText, Common.Net.MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
  444. }
  445. protected override string GetAudioArguments(StreamState state)
  446. {
  447. var codec = state.OutputAudioCodec;
  448. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  449. {
  450. return "-codec:a:0 copy";
  451. }
  452. var args = "-codec:a:0 " + codec;
  453. var channels = state.OutputAudioChannels;
  454. if (channels.HasValue)
  455. {
  456. args += " -ac " + channels.Value;
  457. }
  458. var bitrate = state.OutputAudioBitrate;
  459. if (bitrate.HasValue)
  460. {
  461. args += " -ab " + bitrate.Value.ToString(UsCulture);
  462. }
  463. args += " " + GetAudioFilterParam(state, true);
  464. return args;
  465. }
  466. protected override string GetVideoArguments(StreamState state)
  467. {
  468. var codec = state.OutputVideoCodec;
  469. // See if we can save come cpu cycles by avoiding encoding
  470. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  471. {
  472. // TOOD: Switch to -bsf dump_extra?
  473. return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  474. }
  475. var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
  476. state.SegmentLength.ToString(UsCulture));
  477. var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
  478. var args = "-codec:v:0 " + codec + " " + GetVideoQualityParam(state, "libx264", true) + keyFrameArg;
  479. // Add resolution params, if specified
  480. if (!hasGraphicalSubs)
  481. {
  482. args += GetOutputSizeParam(state, codec, false);
  483. }
  484. // This is for internal graphical subs
  485. if (hasGraphicalSubs)
  486. {
  487. args += GetGraphicalSubtitleParam(state, codec);
  488. }
  489. return args;
  490. }
  491. /// <summary>
  492. /// Gets the command line arguments.
  493. /// </summary>
  494. /// <param name="outputPath">The output path.</param>
  495. /// <param name="state">The state.</param>
  496. /// <param name="isEncoding">if set to <c>true</c> [is encoding].</param>
  497. /// <returns>System.String.</returns>
  498. protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
  499. {
  500. var threads = GetNumberOfThreads(state, false);
  501. var inputModifier = GetInputModifier(state);
  502. // If isEncoding is true we're actually starting ffmpeg
  503. var startNumberParam = isEncoding ? GetStartNumber(state).ToString(UsCulture) : "0";
  504. var args = string.Format("{0} -i {1} -map_metadata -1 -threads {2} {3} {4} -copyts -flags -global_header {5} -hls_time {6} -start_number {7} -hls_list_size {8} -y \"{9}\"",
  505. inputModifier,
  506. GetInputArgument(state),
  507. threads,
  508. GetMapArgs(state),
  509. GetVideoArguments(state),
  510. GetAudioArguments(state),
  511. state.SegmentLength.ToString(UsCulture),
  512. startNumberParam,
  513. state.HlsListSize.ToString(UsCulture),
  514. outputPath
  515. ).Trim();
  516. return args;
  517. }
  518. /// <summary>
  519. /// Gets the segment file extension.
  520. /// </summary>
  521. /// <param name="state">The state.</param>
  522. /// <returns>System.String.</returns>
  523. protected override string GetSegmentFileExtension(StreamState state)
  524. {
  525. return ".ts";
  526. }
  527. protected override TranscodingJobType TranscodingJobType
  528. {
  529. get
  530. {
  531. return TranscodingJobType.Hls;
  532. }
  533. }
  534. }
  535. }