Background

我的意思是实现特定视频格式的按需转码,如".mkv"、".wmv"、".mov"等,以便在使用ASP.NET Core 6.0C#ffmpeg的媒体管理服务器上提供它们.

My Approach

我决定使用的方法是提供一个动态生成的.m3u8文件,该文件只是使用 Select 的片段时长(例如10s)和已知的视频时长生成的.这就是我是如何做到的.请注意,该决议目前未执行并被丢弃:

public string GenerateVideoOnDemandPlaylist(double duration, int segment)
{
   double interval = (double)segment;
   var content = new StringBuilder();

   content.AppendLine("#EXTM3U");
   content.AppendLine("#EXT-X-VERSION:6");
   content.AppendLine(String.Format("#EXT-X-TARGETDURATION:{0}", segment));
   content.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
   content.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD");
   content.AppendLine("#EXT-X-INDEPENDENT-SEGMENTS");

   for (double index = 0; (index * interval) < duration; index++)
   {
      content.AppendLine(String.Format("#EXTINF:{0:#.000000},", ((duration - (index * interval)) > interval) ? interval : ((duration - (index * interval)))));
      content.AppendLine(String.Format("{0:00000}.ts", index));
   }

   content.AppendLine("#EXT-X-ENDLIST");

   return content.ToString();
}

[HttpGet]
[Route("stream/{id}/{resolution}.m3u8")]
public IActionResult Stream(string id, string resolution)
{
   double duration = RetrieveVideoLengthInSeconds();
   return Content(GenerateVideoOnDemandPlaylist(duration, 10), "application/x-mpegURL", Encoding.UTF8);
}

以下是.m3u8文件的示例:

#EXTM3U
#EXT-X-VERSION:6
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-INDEPENDENT-SEGMENTS
#EXTINF:10.000000,
00000.ts
#EXTINF:3.386667,
00001.ts
#EXT-X-ENDLIST

因此,玩家会要求00000.ts00001.ts等,下一步是按需生成它们:

public byte[] GenerateVideoOnDemandSegment(int index, int duration, string path)
{
   int timeout = 30000;
   int totalWaitTime = 0;
   int waitInterval = 100;
   byte[] output = Array.Empty<byte>();
   string executable = "/opt/homebrew/bin/ffmpeg";
   DirectoryInfo temp = Directory.CreateDirectory(System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()));
   string format = System.IO.Path.Combine(temp.FullName, "output-%05d.ts");

   using (Process ffmpeg = new())
   {
      ffmpeg.StartInfo.FileName = executable;

      ffmpeg.StartInfo.Arguments = String.Format("-ss {0} ", index * duration);
      ffmpeg.StartInfo.Arguments += String.Format("-y -t {0} ", duration);
      ffmpeg.StartInfo.Arguments += String.Format("-i \"{0}\" ", path);
      ffmpeg.StartInfo.Arguments += String.Format("-c:v libx264 -c:a aac ");
      ffmpeg.StartInfo.Arguments += String.Format("-segment_time {0} -reset_timestamps 1 -break_non_keyframes 1 -map 0 ", duration);
      ffmpeg.StartInfo.Arguments += String.Format("-initial_offset {0} ", index * duration);
      ffmpeg.StartInfo.Arguments += String.Format("-f segment -segment_format mpegts {0}", format);

      ffmpeg.StartInfo.CreateNoWindow = true;
      ffmpeg.StartInfo.UseShellExecute = false;
      ffmpeg.StartInfo.RedirectStandardError = false;
      ffmpeg.StartInfo.RedirectStandardOutput = false;

      ffmpeg.Start();

      do
      {
         Thread.Sleep(waitInterval);
         totalWaitTime += waitInterval;
      }
      while ((!ffmpeg.HasExited) && (totalWaitTime < timeout));

      if (ffmpeg.HasExited)
      {
         string filename = System.IO.Path.Combine(temp.FullName, "output-00000.ts");

         if (!File.Exists(filename))
         {
            throw new FileNotFoundException("Unable to find the generated segment: " + filename);
         }

         output = File.ReadAllBytes(filename);
      }
      else
      {
         // It's been too long. Kill it!
         ffmpeg.Kill();
      }
   }

   // Remove the temporary directory and all its contents.
   temp.Delete(true);

   return output;
}

[HttpGet]
[Route("stream/{id}/{index}.ts")]
public IActionResult Segment(string id, int index)
{
   string path = RetrieveVideoPath(id);
   return File(GenerateVideoOnDemandSegment(index, 10, path), "application/x-mpegURL", true);
}

如您所见,下面是我用来生成每个段的命令, for each 段递增-ss和-Initial_Offset 10:

ffmpeg -ss 0 -y -t 10 -i "video.mov" -c:v libx264 -c:a aac -segment_time 10 -reset_timestamps 1 -break_non_keyframes 1 -map 0 -initial_offset 0 -f segment -segment_format mpegts /var/folders/8h/3xdhhky96b5bk2w2br6bt8n00000gn/T/4ynrwu0q.z24/output-%05d.ts

The Problem

事情的工作在一个功能层面上,但段之间的过渡是略有故障,特别是音频有非常短的中断,在每10秒标记.我如何确保这些细分市场是无缝的?在这个过程中,我可以改进什么?

推荐答案

由于您正在使用段多路复用器,因此您的输入持续时间应该是您需要的段的总和.

对于段00002.ts到00004.ts,

ffmpeg -ss 20 -t 30 -copyts -i "video.mov" -map 0 -c:v libx264 -c:a aac -f segment -segment_time 10 -reset_timestamps 0 -break_non_keyframes 1 -segment_format mpegts output-%05d.ts -y

(对于每个连续的输出段集,只运行一个命令)

Csharp相关问答推荐

如何在Visual Studio中为C# spread操作符设置格式规则?

下拉图片点击MudBlazor

Monty Hall游戏节目模拟给我50/50的结果

处理. netstandard2.0项目中HttpClient.SslProtocol的PlatformNotSupportedException问题""

当我使用NET6作为目标框架时,为什么DotNet使用NET8作为MS包?

使用命令初始化可绑定属性

未找到任何HTTP触发器.成功部署Azure Functions Project后(c#)

Linux Docker上的.NET6在某个时间抛出后,在加密操作期间发生System.Security.Cryptography.CryptographicException:错误

是否由DI容器自动处理由ActivatorUilties.CreateInstance()创建的服务?

Azure Functions v4中的Serilog控制台主题

MSI无法将快捷方式添加到启动文件夹

未显示详细信息的弹出对话框

try 创建一个C#程序,该程序使用自动实现的属性、覆盖ToString()并使用子类

try 访问字典中的模拟对象时引发KeyNotFoundException

C#-如何将int引用获取到byte[]

为什么我的UserControl没有加载到我的主窗口中?

将文本从剪贴板粘贴到RichTextBox时,新文本不会在RichTextBox ForeColor中着色

我想我必须手动使用res1(字符串形式的PowerShell哈希表)

如果图表S批注包含使用LINQ的具有特定名称的批注,我如何签入C#

为什么INTEGER在通过反射调用时对空对象返回TRUE,而在INTEGER上调用时返回FALSE?