Capturing the video stream from a website into a file

2024/10/5 19:15:40

For my image classification project I need to collect classified images, and for me a good source would be different webcams around the world streaming video in the internet. Like this one:

https://www.skylinewebcams.com/en/webcam/espana/comunidad-valenciana/alicante/benidorm-playa-poniente.html

I don't really have any experience with video streaming and web scraping generally, so after searching for the info in internet, i came up with this naive code in python:

url='https://www.skylinewebcams.com/a816de08-9805-4cc2-94e6-2daa3495eb99'
r1 = requests.get(url, stream=True)
filename = "stream.avi"if(r1.status_code == 200):with open(filename,'w') as f:for chunk in r1.iter_content(chunk_size=1024):f.write(chunk)else:print("Received unexpected status code {}".format(r.status_code))

where the url address was taken from the source of the video block from the website:

<video data-html5-video="" 
poster="//static.skylinewebcams.com/_2933625150.jpg" preload="metadata" 
src="blob:https://www.skylinewebcams.com/a816de08-9805-4cc2-94e6- 
2daa3495eb99"></video>

but it does not work (avi file is empty), even though in the browser video streaming is working good. Can anybody explain me how to capture this video stream into the file?

Answer

I've made some progress since then. Here is the code:

print ("Recording video...")
url='https://hddn01.skylinewebcams.com/02930601ENXS-1523680721427.ts'
r1 = requests.get(url, stream=True)
filename = "stream.avi"num=0
if(r1.status_code == 200):with open(filename,'wb') as f:for chunk in r1.iter_content(chunk_size=1024):num += 1f.write(chunk)if num>5000:print('end')breakelse:print("Received unexpected status code {}".format(r.status_code))

Now i can get some piece of video written in the file. What I've change is 1) in open(filename,'wb') changed 'w' to 'wb' to write binary data, but most important 2) changed url. I looked in Chrome devtools 'network' what requests are sent by browser to get the live stream, and just copied the most fresh one, it requests some .ts file.

Next, i've found out how to get the addresses of .ts video files. One can use m3u8 module (installable by pip) like this:

import m3u8
m3u8_obj = m3u8.load('https://hddn01.skylinewebcams.com/live.m3u8? a=k2makj8nd279g717kt4d145pd3')
playlist=[el['uri'] for el in m3u8_obj.data['segments']]

The playlist of the video files will then be something like that

['https://hddn04.skylinewebcams.com/02930601ENXS-1523720836405.ts','https://hddn04.skylinewebcams.com/02930601ENXS-1523720844347.ts','https://hddn04.skylinewebcams.com/02930601ENXS-1523720852324.ts','https://hddn04.skylinewebcams.com/02930601ENXS-1523720860239.ts','https://hddn04.skylinewebcams.com/02930601ENXS-1523720868277.ts','https://hddn04.skylinewebcams.com/02930601ENXS-1523720876252.ts']

and I can download each of the video files from the list.

The only problem left, is that in order to load the playlist i need first to open the webpage in a browser. Otherwise the playlist is gonna be empty. Probably opening the webpage initiates the streaming and this creates m3u8 file on the server that can be requested. I still don't know how to initialize streaming from python, without opening the page in the browser.

https://en.xdnf.cn/q/70455.html

Related Q&A

Recreating time series data using FFT results without using ifft

I analyzed the sunspots.dat data (below) using fft which is a classic example in this area. I obtained results from fft in real and imaginery parts. Then I tried to use these coefficients (first 20) to…

python BeautifulSoup get all href in Children of div

I am new to python and Ive been trying to get links and inner text from this html code : <div class="someclass"><ul class="listing"><li><a href="http://lin…

Python TypeError: sort() takes no positional arguments

I try to write a small class and want to sort the items based on the weight. The code is provided, class Bird:def __init__(self, weight):# __weight for the private variableself.__weight = weightdef wei…

Trouble with basemap subplots

I need to make a plot with n number of basemap subplots. But when I am doing this the all the values are plotted on the first subplot.My data is a set of n matrixes, stored in data_all.f, map = plt.sub…

Remove circular references in dicts, lists, tuples

I have this following really hack code which removes circular references from any kind of data structure built out of dict, tuple and list objects.import astdef remove_circular_refs(o):return ast.liter…

how to change image format when uploading image in django?

When a user uploads an image from the Django admin panel, I want to change the image format to .webp. I have overridden the save method of the model. Webp file is generated in the media/banner folder b…

Write info about nodes to a CSV file on the controller (the local)

I have written an Ansible playbook that returns some information from various sources. One of the variables I am saving during a task is the number of records in a certain MySQL database table. I can p…

Python minimize function: passing additional arguments to constraint dictionary

I dont know how to pass additional arguments through the minimize function to the constraint dictionary. I can successfully pass additional arguments to the objective function.Documentation on minimiz…

PyQt5 triggering a paintEvent() with keyPressEvent()

I am trying to learn PyQt vector painting. Currently I am stuck in trying to pass information to paintEvent() method which I guess, should call other methods:I am trying to paint different numbers to a…

A python regex that matches the regional indicator character class

I am using python 2.7.10 on a Mac. Flags in emoji are indicated by a pair of Regional Indicator Symbols. I would like to write a python regex to insert spaces between a string of emoji flags.For exampl…