1

I am trying to create a website for myself. It is kind of a Youtube on a local network. I am using video.js (have also tried Plyr.io) to play the video, but i can not fast forward the video. Or go back in the video. I can only play it from begining to the end. If i try to skip forward it only resets. What am I doing wrong? Thanks in advance!

lagtri
  • 43
  • 5

2 Answers2

2

The behaviour sounds like the server doesn't implement range headers. When you try to seek, it returns the start of the file and not the part requested. If you try Safari you'll probably find it won't play at all. Check questions like Byte Ranges in Django

misterben
  • 7,455
  • 2
  • 26
  • 47
0

Yes, I all face the similar issue in using the video js library. But with the help from Byte range in django I solve this issue in video.js library by adding the RangeMiddleware. But I can skip or forward the video.

class RangesMiddleware(MiddlewareMixin):
def process_response(self, request, response):
    if response.status_code != 200 or not hasattr(response, "file_to_stream"):
        return response
    http_range = request.META.get("HTTP_RANGE")
    if not (
        http_range
        and http_range.startswith("bytes=")
        and http_range.count("-") == 1
    ):
        return response
    if_range = request.META.get("HTTP_IF_RANGE")
    if (
        if_range
        and if_range != response.get("Last-Modified")
        and if_range != response.get("ETag")
    ):
        return response
    f = response.file_to_stream
    statobj = os.fstat(f.fileno())
    start, end = http_range.split("=")[1].split("-")
    if not start:  # requesting the last N bytes
        start = max(0, statobj.st_size - int(end))
        end = ""
    start, end = int(start or 0), int(end or statobj.st_size - 1)
    assert 0 <= start < statobj.st_size, (start, statobj.st_size)
    end = min(end, statobj.st_size - 1)
    f.seek(start)
    old_read = f.read
    f.read = lambda n: old_read(min(n, end + 1 - f.tell()))
    response.status_code = 206
    response["Content-Length"] = end + 1 - start
    response["Content-Range"] = "bytes %d-%d/%d" % (start, end, statobj.st_size)
    return response