python script to record online live streaming vide

2019-03-10 09:57发布

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.

3条回答
【Aperson】
2楼-- · 2019-03-10 10:28

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

查看更多
何必那么认真
3楼-- · 2019-03-10 10:29

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()
查看更多
Explosion°爆炸
4楼-- · 2019-03-10 10:39

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

video_file_size_end = response.info().getheader('Content-Length')
查看更多
登录 后发表回答