0

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();
}

問題は次の行にあると思います。削除してもビデオは再生できますが、最初から if (position > 0) { fs.Position = position; シーク位置 > 0 の場合、ストリームを認識できないためにシーク位置を追跡するために flv ストリーミングで使用されるような開始 mp4 ヘッダーがある可能性があります

誰でもこれで私を助けることができますか?

よろしく。

4

1 に答える 1

0

Content-Length をファイルの長さに設定しますが、ファイルの一部のみを送信します。

また、そのようにビデオを分割することはできないと思います。ファイルの位置を I フレームの先頭に設定する必要があると思います。これは、何らかの方法で mp4 ファイルを解析し、最も近い I フレームを見つけることを意味します。必要な時間にファイル位置をそのバイトに設定し、そこからストリーミングを開始します。

于 2015-06-15T14:20:36.757 に答える