用于录制在线直播视频的python脚本 [英] python script to record online live streaming videos

查看:65
本文介绍了用于录制在线直播视频的python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个脚本来下载在线直播视频.

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

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

上面的脚本可以很好地从实时流媒体内容下载 7Mb 的视频并将其存储到 *.avi 文件中.

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

但是,无论文件大小如何,我只想下载 10 秒的视频并将其存储在 avi 文件中.

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.

提前致谢.

推荐答案

我认为如果不经常分析视频,就没有办法做到这一点,这会很昂贵.因此,您可以猜测您需要多少 MB,完成后检查它是否足够长.如果它太长,就剪掉它.您还可以建立一些关于需要检索多少的统计信息,而不是猜测.您还可以将 while True 替换为:

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

这应该为您提供至少 10 秒的视频,除非连接花费太多时间(然后不到 10 秒)或服务器发送更多用于缓冲(但这不太可能用于实时流).

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

这篇关于用于录制在线直播视频的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆