HorizonBloom
HorizonBloom

Reputation: 54

How to download and play a specific part of a large video file using requests python

I'm trying to download a specific scene of a large video file that is 3GB+ using python requests and I'm limited on data usage and don't want to download the whole 3GB file just to get the specific part of around 300MB.

The website supports Range requests and I've tried downloading the last scene by adding the range header to my GET request:

{"Range":"bytes=16872448-"}

The website returns a valid response with the header: 'Content-Range': 'bytes 16872448-20873418/20873419' and could download the chunks.

But when I try playing back the downloaded content its doesn't open using multiple media players with no success.

Was surprised that I couldn't find an answered question on here but hope someone can help me and anyone else with this issue.

Full Code:

import requests

header = {"Range":"bytes=16872448-"}

with requests.get(url, headers=header, stream=True) as resp:
    with open("video-part.mp4", 'wb') as f:
        for chunk in resp.iter_content(chunk_size=1024):
            f.write(chunk)

Upvotes: 0

Views: 251

Answers (0)

Related Questions