Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python script to record online live streaming videos

i am developing a script to download online live streaming videos.

My Script:

print "Recording video..."
response = urllib2.urlopen("streaming online video url")
filename = time.strftime("%Y%m%d%H%M%S",time.localtime())+".avi"
f = open(filename, 'wb')

video_file_size_start = 0  
video_file_size_end = 1048576 * 7  # end in 7 mb 
block_size = 1024

while True:
    try:
        buffer = response.read(block_size)
        if not buffer:
            break
        video_file_size_start += len(buffer)
        if video_file_size_start > video_file_size_end:
            break
        f.write(buffer)

    except Exception, e:
        logger.exception(e)
f.close()

above script is working fine to download 7Mb of video from live streaming contents and storing it in to *.avi files.

However, I would like to download just 10 secs of video regardless of the file size and store it in avi file.

I tried different possibilities but to no success.

Could any one please share your knowledge here to fix my issue.

Thanks in advance.

like image 269
AGR Avatar asked Apr 19 '12 08:04

AGR


People also ask

Can you record a live streamed event?

There are three ways to record live streamed videos: via streaming video capture software, with the help of built-in screen capture tools, and through a streaming service like Restream.


2 Answers

I don't think there is any way of doing that without constantly analysing the video, which will be way to costly. So you could take a guess of how many MB you need and once done check it's long enough. If it's too long, just cut it. Instead of guessing you could also build up some statistics of how much you need to retrieve. You could also replace the while True with:

start_time_in_seconds = time.time()
time_limit = 10
while time.time() - start_time_in_seconds < time_limit:
    ...

This should give you at least 10 seconds of video, unless connecting takes too much time (less then 10 seconds then) or server sends more for buffering (but that's unlikely for live streams).

like image 119
Sebastian Blask Avatar answered Oct 02 '22 14:10

Sebastian Blask


You can use the 'Content-Length' header to retrieve the video filesize if it exists.

video_file_size_end = response.info().getheader('Content-Length')
like image 27
exxy- Avatar answered Oct 02 '22 13:10

exxy-