Stream video content through Web API 2

前端 未结 2 1648
悲哀的现实
悲哀的现实 2021-01-30 18:39

I\'m in the process of working out what the best way is going to be to do the following:

I have a bunch of CCTV footage files (MP4 files, ranging from 4MB-50MB in size),

相关标签:
2条回答
  • 2021-01-30 18:54

    Place the static video files on a web server and reference them in a video player. You can use HTML's standard player (<video src="http://location/of/file.mp4">) or go for something fancier - just google for "html video player".

    To make sure that the video files don't have to download completely before starting to play just run them beforehand through qt-faststart.

    0 讨论(0)
  • 2021-01-30 19:06

    Two things:

    1. Use a video element in your HTML (this works in browsers AND iOS):

      <video src="http://yoursite.com/api/Media/GetVideo?videoId=42" /> 
      
    2. Support 206 PARTIAL CONTENT requests in you Web API code. This is crucial for both streaming and iOS support, and is mentioned in that thread you posted.

    Just follow this example:

    https://devblogs.microsoft.com/aspnet/asp-net-web-api-and-http-byte-range-support/

    In a nutshell:

    if (Request.Headers.Range != null)
    {
        // Return part of the video
        HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
        partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, mediaType);
        return partialResponse;
    }
    else 
    {
        // Return complete video
        HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK);
        fullResponse.Content = new StreamContent(stream);
        fullResponse.Content.Headers.ContentType = mediaType;
        return fullResponse;
    }
    
    0 讨论(0)
提交回复
热议问题