1

I'm making a small application which sent a part of video file to client to play on <video> element. This is the code I'm having:

[RoutePrefix("api/video")]
public class VideoApiController : ApiController
{
    [Route("")]
    [HttpGet]
    public async Task<HttpResponseMessage> GetVideoAsync([FromUri] GetVideoViewModel model)
    {
        var path = System.Web.Hosting.HostingEnvironment.MapPath($"~/wwwroot/{model.FileName}");
        if (path == null || !File.Exists(path))
            return new HttpResponseMessage(HttpStatusCode.NotFound);

        using (var fileStream = File.OpenRead(path))
        {
            var bytes = new byte[model.To];
            fileStream.Read(bytes, 0, bytes.Length - 1);
            var memoryStream = new MemoryStream(bytes);
            memoryStream.Seek(0, SeekOrigin.Begin);
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.PartialContent);
            httpResponseMessage.Content = new ByteRangeStreamContent(memoryStream, new RangeHeaderValue(model.From, model.To), "video/webm");
            return httpResponseMessage;
        }
    }
}

Then, I tested my end-point with postman. If I selected byte range from 0 to 100000(http://localhost:64186/api/video?fileName=elephants-dream.webm&from=0&to=100000), video could be displayed on result panel:

enter image description here

However, when I selected byte range from 100000 to 200000(http://localhost:64186/api/video?fileName=elephants-dream.webm&from=100000&to=200000) at the first tiem, video was blank:

enter image description here

As I understand, video/webm uses a codec, metadata is included at some first bytes of stream. If I want to play a part of video without play it from the beginning. What should I do ?

Thank you.

Siavash Rostami
  • 1,883
  • 4
  • 17
  • 31
Redplane
  • 2,971
  • 4
  • 30
  • 59

1 Answers1

0

This is not very easy to do.

The first part of the stream is not just some metadata, it is all the information needed to tell the players how and what we are playing and even the color information to use in the streaming.

What you can do is read the header of the video extract the keyframes (Called Cues) then seek through your stream and start streaming the bits out with a new header. Basically, you will be building a web streaming software from scratch that will only work for this very specific video format (codec)

But here is the info to get you started and how to write the header in C++

Or you use ffmpeg and seek keyframes to jump to a good spot and let hundreds of other developers scream in frustration instead.

Microsoft has a zombie project which lets you wrap ffmpeg into c#.

Here are some other options for ffmpeg. And how you can seek keyframes.

The other solution is to install a video streaming software to handle this.

Archlight
  • 2,019
  • 2
  • 21
  • 34