python script to record online live streaming videos

后端 未结 3 1005
生来不讨喜
生来不讨喜 2021-02-01 10:56

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

My Script:

print \"Recording video...\"
response = urllib2.urlopen(\"streaming onlin         


        
3条回答
  •  不知归路
    2021-02-01 11:18

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

提交回复
热议问题