Python: How to download a blob url video?
Asked Answered
H

4

10

I'd like to download a video from a website with Python script, however, the video is served by a blob URL as below.

<video class="jw-video jw-reset" style="object-fit: fill;" jw-loaded="data" src="blob:https://xxxxxxx.com/f717096e-5e1a-42e1-8c3c-3ec777b5d478"></video>
Hertha answered 30/12, 2017 at 14:9 Comment(4)
have you tried : #39518022 ?Meaganmeager
Hi, thanks for your comment. actually my situation is a bit complex. I firstly used selenium to login to the website and obtained the cookies, then I passed the session cookies to python. def request(driver): s = requests.Session() cookies = driver.get_cookies() for cookie in cookies: s.cookies.set(cookie['name'], cookie['value']) return s req = request(browser) Could you please detail a bit how to save the video after the above codes? thanks!Hertha
put all information in question. You can also create minimal working example.Snook
This question is rather incomplete, and could do with improvement. It may close as "unclear" or "lacking minimal reproducible example".Keturahkeung
H
4

Blob video can be download by using the below python code you have to get the master segment url from page inspect like in the image given , past the url in the code where mentioned it

enter image description here

import requests
import m3u8
import subprocess

master_url ='master_url_from_inspect_network' 
#past your page inspect request header

r = requests.get(master_url)
m3u8_master = m3u8.loads(r.text)
print(m3u8_master)
playlist_url =m3u8_master.data['playlists'][0]['uri']
play_r = requests.get(playlist_url)
m3u8_master_play = m3u8.loads(play_r.text)
m3_data=(m3u8_master_play.data)

m3_datas = m3_data['segments'][0]['uri']

with open('video.ts','wb') as fs:
    for segments in m3_data['segments']:
       uri = segments['uri']
       print(uri)
       r = requests.get(uri)
       fs.write(r.content)

subprocess.run(['ffmpeg','-i','video.ts','video.mp4'])

Habilitate answered 19/12, 2021 at 21:3 Comment(1)
is it possible to get the network master segment URL programatically?Overstock
L
2

In the cases I came across, the pages in which I saw these blob:https://... URLS were also serving .m3u8 files. These had the real links to the video, in many separate pieces. And sometimes also an encryption key.

However, the links to these .m3u8 files are sometimes generated by javascript, and don't exist in the source of the original page. So you may need to use your browser's dev tools and look at the network tab while refreshing the page with the video, to be able to see the requests to these .m3u8 URLs.

In my case, youtube-dl (which is a Python script) was able to download the video when given that .m3u8 URL, and feed it to ffmpeg.

So you could try that and then see in the youtube-dl source how it does it with Python .

Lymanlymann answered 23/6, 2020 at 21:52 Comment(0)
G
1

You can’t “download it”. A blob is a pseudo url that represents a buffer in memory. It does not point to any file in a server. https://developer.mozilla.org/en-US/docs/Web/API/Blob

Gisser answered 30/12, 2017 at 15:59 Comment(5)
But is it really impossible to access the buffer's content ?Meaganmeager
No, but that’s a different question. “Download” implies the ability to access the content from another process, and that is impossible. To save a blob is straight forward. Google can answer that faster than I can. But you will need to ingect the JS into the running page if you don’t own the server.Gisser
@szatmary, I've been trying to solve this problem in a google extension, and I've googled to the best of my capability, but I guess my searching skills have taken a toll. I've not been able to find a solution to this. Could you point me in a direction?Dink
Sure. First implement a player using MSE so you understand how it works, then works backwards from there.Gisser
"Google can answer that faster than I can" Google brought me here so...Important
P
-15

You can use urllib2 for that.

import urllib2
response = urllib2.urlopen('http://www.example.com/')
html = response.read()
Praedial answered 9/1, 2018 at 17:33 Comment(1)
Just flat out wrong.Avram

© 2022 - 2024 — McMap. All rights reserved.