【问题标题】:Progressive Mp4 Streaming using ASP.NET使用 ASP.NET 的渐进式 Mp4 流式传输
【发布时间】:2012-03-16 01:50:18
【问题描述】:

我在使用 asp.net 脚本从任何部分或部分流式传输 mp4 视频时遇到问题。当您从一开始流式传输 mp4 视频时脚本运行良好,但如果您想选择任何起点,则流式传输失败。

我正在使用的示例脚本

if (filename.EndsWith(".mp4") && filename.Length > 2)
{
   FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
   // Sample logic to calculate approx length based on starting time.
   if (context.Request.Params["starttime"] != null && context.Request.Params["d"] != null)
   {
       double total_duration = Convert.ToDouble(context.Request.Params["d"]);
       double startduration = Convert.ToDouble(context.Request.Params["starttime"]);
       double length_sec = (double)fs.Length / total_duration; // total length per second
       seekpos = (long)(length_sec * startduration);
   }
   if (seekpos==0)
   {
       position = 0;
       length = Convert.ToInt32(fs.Length);
   }
   else
   {
       position = Convert.ToInt32(seekpos);
       length = Convert.ToInt32(fs.Length - position);
   }
   // Add HTTP header stuff: cache, content type and length        
   context.Response.Cache.SetCacheability(HttpCacheability.Public);
   context.Response.Cache.SetLastModified(DateTime.Now);
   context.Response.AppendHeader("Content-Type", "video/mp4");
   context.Response.AppendHeader("Content-Length", length.ToString());
   if (position > 0)
   {
       fs.Position = position;
   }
   // Read buffer and write stream to the response stream
   const int buffersize = 16384;
   byte[] buffer = new byte[buffersize];

   int count = fs.Read(buffer, 0, buffersize);
   while (count > 0)
   {
      if (context.Response.IsClientConnected)
      {
          context.Response.OutputStream.Write(buffer,0, count);
          context.Response.Flush();
          count = fs.Read(buffer, 0, buffersize);
      }
      else
      {
          count = -1;
      }
   }
   fs.Close();
}

我认为问题出在以下行,如果我删除它,视频仍然可以播放,但从头开始 如果(位置 > 0) { fs.Position = 位置; } 如果 seek position > 0

,可能会出现类似 flv 流中用于跟踪搜索位置的启动 mp4 标头,因为无法识别流

谁能帮帮我。

问候。

【问题讨论】:

  • 也许您应该考虑一些播放器(silverlight 或 flash),因为在 FileStream 中设置位置仅适用于文本文件。在 mp4 中工作的机会非常小。
  • 问题是,例如,当用户点击 jwplayer 之类的 Flash 播放器时,它会将 starttime 发送到流式脚本,但流式脚本无法从需要的位置发送内容,以防 mp4,它适用于 flash flv 案例。它在实现h264.code-shop.com/trac/wiki/… 时可以工作,但如果直接通过 http 脚本工作会很好且容易。

标签: c# asp.net streaming mp4 http-streaming


【解决方案1】:

您将 Content-Length 设置为文件长度,然后只发送文件的一部分。

另外我不认为你可以像那样分割视频,我认为你必须将文件位置设置为 I 帧的开头,这意味着以某种方式解析 mp4 文件并找到最近的 I-帧到您想要的时间并将文件位置设置为该字节,然后从那里开始流式传输。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    相关资源
    最近更新 更多