Skip to content

Fix the bug where the video does not stream to the end in Chrome - #3

Open
schteeben95 wants to merge 2 commits into
stribny:masterfrom
schteeben95:master
Open

Fix the bug where the video does not stream to the end in Chrome #3
schteeben95 wants to merge 2 commits into
stribny:masterfrom
schteeben95:master

Conversation

@schteeben95

Copy link
Copy Markdown

The video did not stream to the end. When Chrome requests for the last chunk it freezes and restarts the playback.

This new commit conforms to the RFC as explained here https://stackoverflow.com/questions/3303029/http-range-header

@devbaraus devbaraus left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a few less modifications

async def video_endpoint(range: str = Header(None)):
    start, end = range.replace("bytes=", "").split("-")
    start = int(start)
    end = int(end) if end else start + CHUNK_SIZE_VIDEO
    
    with open(video_path, "rb") as video:
        video.seek(start)
        data = video.read(end - start)
        filesize = video_path.stat().st_size

        if end >= filesize:
            end = filesize - 1

        headers = {
            'Content-Range': f'bytes {str(start)}-{str(end)}/{str(filesize)}',
            'Accept-Ranges': 'bytes'
        }
        return Response(data, status_code=206, headers=headers, media_type="video/mp4")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants