python script to record online live streaming videos

19,909

Solution 1

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).

Solution 2

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

video_file_size_end = response.info().getheader('Content-Length')

Solution 3

response.read() does not work. response.iter_content() seem to do the trick.

import time
import requests


print("Recording video...")
filename = time.strftime("/tmp/" + "%Y%m%d%H%M%S",time.localtime())+".avi"
file_handle = open(filename, 'wb')
chunk_size = 1024

start_time_in_seconds = time.time()

time_limit = 10 # time in seconds, for recording
time_elapsed = 0
url = "http://demo.codesamplez.com/html5/video/sample"
with requests.Session() as session:
    response = session.get(url, stream=True)
    for chunk in response.iter_content(chunk_size=chunk_size):
        if time_elapsed > time_limit:
            break
        # to print time elapsed   
        if int(time.time() - start_time_in_seconds)- time_elapsed > 0 :
            time_elapsed = int(time.time() - start_time_in_seconds)
            print(time_elapsed, end='\r', flush=True)
        if chunk:
            file_handle.write(chunk)

    file_handle.close()
Share:
19,909
AGR
Author by

AGR

Updated on July 26, 2022

Comments

  • AGR
    AGR over 1 year

    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.

  • user2813606
    user2813606 over 2 years
    Hey there @patel deven! Great work! I'm trying to run your code and I plug in a link to my video I want to download - I get a .avi file, but it's 0 seconds long. Not sure what the difference is in your file vs mine. I just tried a youtube file.