用python捕获屏幕视频的几个问题 [英] Several issues with capturing a screen video with python

查看:68
本文介绍了用python捕获屏幕视频的几个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在stackoverflow上问过类似的问题,但不一样:-)我的代码有三个问题:

i have already asked a similar question on stackoverflow, but its not the same :-) I have three problems with my code:

  1. 帧率不稳定.当我在没有屏幕抓取的情况下执行代码时,我的帧率仍然很低,更糟糕的是,帧率会有所不同.我在下面发布输出.有什么提示可以解决这个问题吗?
  2. 速度很慢.这段代码将做的是检查屏幕上的像素并记录导致该像素出现的时间.由于记录之间可能存在长时间的停顿,我想包含一个写入工作线程的缓冲区,但尚未弄清楚这一点.这会影响性能吗?
  3. 有时可能会发生像素在第一次记录后很快出现的情况.如何防止帧率下降以及如何将其与其他帧合并?

这是我的代码:

import PIL.ImageGrab
import time

def get_pixel_colour(i_x, i_y):

    return PIL.ImageGrab.grab().load()[i_x, i_y]

print get_pixel_colour(0, 0)
fps = 5
skipticks = 1/(fps*1.0)
i= 0
allframes=[]
nextsnap=time.clock()
print skipticks, fps
while (True):
    tim= time.clock()
    i=i+1
    #x = PIL.ImageGrab.grab()
    # this prints the fps
    #'print 'Fps at start',i, 1/(time.time()-tim)
    #   x.save("tm\screengrab"+ str(i) +".jpg")
    # this is the sleep that limits the fps
    nextsnap+=skipticks
    # print nextsnap, skipticks
    sleeptime = nextsnap-time.clock()
    #print "want to sleep",sleeptime
    if (sleeptime>0):
        time.sleep (sleeptime)
    else:
        print 'took too long'
    print 'Fps at end:#', i, 1/(time.clock()-tim)

这是我的输出,当屏幕抓取被注释掉时:

here is my output, when the screengrab is commented out:

Fps at end:# 1 4.93170511268
Fps at end:# 2 5.0171558081
Fps at end:# 3 5.43777363226
Fps at end:# 4 5.00931318904
Fps at end:# 5 5.00381624434
Fps at end:# 6 5.01183110503
Fps at end:# 7 5.01101022488
Fps at end:# 8 5.43623758467
Fps at end:# 9 5.04215239941

这里没有注释掉:

Fps at end:# 13 5.05128838338
Fps at end:# 14 5.07528828608
Fps at end:# 15 5.51303968815
Fps at end:# 16 5.05357755475
Fps at end:# 17 5.0671482485
Fps at end:# 18 5.06788717279
Fps at end:# 19 5.06737062052
Fps at end:# 20 5.48886217371
Fps at end:# 21 5.067822598

我希望有人能够在这里提供帮助:-)

i hope someone is able to help here :-)

推荐答案

好吧,看了一会儿,我把这些链接放在了一起.首先,这类似于在游戏中编写刷新屏幕循环.阅读本文了解睡眠的一些陷阱.为了更好的解决方案,您确实希望您的程序等到内核更新显示,这应该是 linux 中的内核事件.您可以通过使用 选择库 设置事件队列来完成此操作.

Alrighty, after looking around for a while, I've gotten these links together. First, this is similar to writing a refresh screen loop in a game. Read this for some of the pitfalls with sleeping. For a better solution, you really want to have your program wait until the kernel updates the display, which should be a kernel event in linux. You'd do this by setting up a event queue with the select library.

我能找到的唯一用于监视事件队列的名称在这里,其中 fbdev_evtchn 为这个频道.这可能不适用.

The only name I could find for the event queue to monitor was here, with fbdev_evtchn as the channel. This may not apply.

如果您发现自己在 Windows 上,或者无法获得屏幕刷新通知,则选择库和 信号库 有实时计时器.这些会给你一个准确的等待,根据实现,应该把程序放在计时器用完之前.

If you find yourself on windows, or can't get notified of screen refreshes, then both the select library and the signal library have real-time timers. Those will give you an accurate wait, and depending on the implementation, should put the program under until the timer runs out.

这篇关于用python捕获屏幕视频的几个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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