0

I had issue the video tag on chrome because I want to be able to skip to particular time on the video, google chrome does not allow that to happen because it requires a specific kind of response after doing some research I came to this post on stackoverflow

after adding the code, I set the endpoint on the source tag of the video like this <source src="{% url 'namespace:stream_video' obj.video.path %}" type="video/mp4" /> even did it like this <source src="{% url 'namespace:stream_video' path=obj.video.path %}" type="video/mp4" /> But I am getting NoReversMatch

'stream_video' with arguments '('/home/UserName/Work/projectName/media/uploads/2022/8/3/b3f10ea2-b323-4398-a86c-c5fb5936cfd723.mp4',)' not found. 1 pattern(s) tried: ['video\\-stream/(?P<path>[^/]+)\\Z']

The path path("stream/<str:path>", views.stream_video, name="stream_video")

And this is my view it's literally a copy of the view from the post:

def stream_video(request, path):
    range_header = request.META.get('HTTP_RANGE', '').strip()
    range_match = range_re.match(range_header)
    size = os.path.getsize(path)
    content_type, encoding = mimetypes.guess_type(path)
    content_type = content_type or 'application/octet-stream'
    if range_match:
        first_byte, last_byte = range_match.groups()
        first_byte = int(first_byte) if first_byte else 0
        last_byte = int(last_byte) if last_byte else size - 1
        if last_byte >= size:
            last_byte = size - 1
        length = last_byte - first_byte + 1
        resp = StreamingHttpResponse(RangeFileWrapper(open(path, 'rb'), offset=first_byte, length=length), status=206, content_type=content_type)
        resp['Content-Length'] = str(length)
        resp['Content-Range'] = 'bytes %s-%s/%s' % (first_byte, last_byte, size)
    else:
        resp = StreamingHttpResponse(FileWrapper(open(path, 'rb')), content_type=content_type)
        resp['Content-Length'] = str(size)
    resp['Accept-Ranges'] = 'bytes'
    return resp
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

1 Answers1

2

For some weird reason maybe because of the slashes in path that's causing the NoReverseMatch error. So what I basically did was to use the objects id on the views to get the object and set the path on stream_video to none, then pass the path inside the function. This is how the function looks now.

def stream_video(request, video_id):
    vid = Video.objects.get(id=video_id)
    path = vid.field_name.path
    ...

path("video-stream/<video_id>/", views.stream_video, name="stream_video")

<source src="{% url 'namespace:stream_video' video_id=video.id %}" type="video/mp4" />

This is what I ended up with.