In python, on windows, how to wait until mouse moves? - python

I'm developing a screen capture tool, this tools aims to help software developers understand how users ended up crashing the application. The idea is to start screen capture once the mouse starts moving and stop it after 5 minutes the mouse didn't move. screen capture works fine through subprocess with ffmpeg, the only remaining problem (except for the application crashes) is to start and stop the screen capture. How can I do this? ideally it would work with condition variable but even a loop which tests if the mouse moved in the last second would do. Is there any chance python supports something like OnMouseMove()?

A loop + pywin32, like this:
import win32api
from time import sleep
count = 0
savedpos = win32api.GetCursorPos()
while(True):
if count>20*5: # break after 5sec
break
curpos = win32api.GetCursorPos()
if savedpos != curpos:
savedpos = curpos
print "moved to " + str(savedpos)
sleep(0.05)
count +=1

wxPython gives you access to a whole set of OnMouse events that you can bind to.

After considering my alternatives I think this the right way to handle my problem, please note I've updated the code to support remote desktop disconnect by checking if the GetCursorPos() throws an exception, also please note that when closing the remote desktop ffmpeg outputs
[dshow # ] real-time buffer full! frame dropped!
But the output file looks fine to me. This script was tested on Windows server 2012
# http://ffmpeg.zeranoe.com/builds/
# http://www.videohelp.com/tools/UScreenCapture
# http://sourceforge.net/projects/pywin32/files/pywin32/
import time, win32api, threading, subprocess, datetime, string, winerror
StopRecordingTimeout = 10
def captureFunction():
pos = None
proc = None
counter = 0
while True:
time.sleep(1)
exceptFlag = False
try:
newPos = win32api.GetCursorPos()
if pos == None:
pos = newPos
except Exception as e:
if e[0] == winerror.ERROR_ACCESS_DENIED:
exceptFlag = True
if newPos != pos and proc != None:
# mouse moved and we are recording already so just make sure the time out counter is zero
counter = 0
elif newPos != pos and proc == None:
# mouse moved and recording didn't start already
fileName = filter(lambda x : x in string.digits, str(datetime.datetime.now()))
fileName = 'output' + fileName + '.flv'
print 'start recording to ' + fileName
proc = subprocess.Popen('ffmpeg -f dshow -i video=UScreenCapture ' + fileName)
elif proc != None and (newPos == pos or exceptFlag):
# mouse didn't moved and recording already started
if counter < StopRecordingTimeout and not exceptFlag:
counter = counter + 1
print 'stop recording in ' + str(StopRecordingTimeout - counter) + ' seconds'
elif exceptFlag or counter >= StopRecordingTimeout:
print 'stop recording'
proc.terminate()
proc = None
counter = 0
pos = newPos
print 'start'
captureThread = threading.Thread(target = captureFunction)
captureThread.start()
captureThread.join()
print 'end'

Related

Python VLC - Next track

I am writing a program in Python to run on a Raspberry Pi in order to control my Wurlitzer jukebox. The program current accepts the codes for "record selection" (A1, B1, C4, etc.), add those codes to a playlist, and executes the list. My issue is that once a song starts, I would like to be able to press a button ("Y" in the current code) to skip the currently playing song. I can't get this to work.
If I use "player.next()" I get an error: 'MediaPlayer' object has no attribute 'next'.
I tried to stop the player and restart it (thinking it would pick up the next song in the Playlist. This doesn't even stop the player.
I do not want to use subprocess if I can avoid it. I'd like to figure out a way within Python to do the skipping. How would one accomplish this?
import os, sys, csv, vlc, time, threading
from pynput.keyboard import Key, Listener
DefaultUSBPath="/media/pi"
PlayHistory="PlayHistory.csv"
#
# Declare variables
#
USBDrive = None
Action = None
Playlist = []
SelectionCount = []
Sel_char = None
#
# Find the USB Drive
#
for item in os.listdir(DefaultUSBPath):
if os.path.isdir(os.path.join(DefaultUSBPath, item)):
if USBDrive is None:
USBDrive = os.path.join(DefaultUSBPath, item)
else:
USBDrive = USBDrive + ";" + os.path.join(DefaultUSBPath, item)
if USBDrive is None:
print ("Error(0) - No USB Drive detected")
sys.exit()
elif ";" in USBDrive:
print ("Error(1) - More than one USB Drive detected.")
sys.exit()
#
# Adding to playlist - Returns directory contents and adds to playlist
#
def addplaylist(track):
list = None
if os.path.isdir(os.path.join(USBDrive, track)):
files = [f for f in os.listdir(os.path.join(USBDrive, track)) if os.path.isfile(os.path.join(USBDrive, track, f))]
for f in files:
if list is None:
list = os.path.join(USBDrive, track, f)
else:
list = list + ";" + os.path.join(USBDrive, track, f)
else:
print ("Error(2) - Selection is invalid")
if list is not None:
if ";" in list:
list = list.split(";")
else:
print ("Error(3) - Selection has no media")
return list
#
# MediaPlayer function
#
def MusicPlayer(P):
global Playlist, player
while len(Playlist) > 0:
song=Playlist.pop(0)
print("Song: ")
print(song)
player=vlc.MediaPlayer(song)
player.play()
#
# Define keyboard actions
#
def on_press(key):
global Action, Playlist, player
try:
Sel_char = int(key.char)
except:
try:
Sel_char = str(key.char)
Sel_char = Sel_char.upper()
except:
Sel_char = None
if Sel_char == "Z":
return False
elif Sel_char == "Y":
print("Skip")
#player.next() This line causes 'MediaPlayer' object has no attribute 'next'
time.sleep(1)
MusicPlayer(Playlist)
elif type(Sel_char) == str:
Action = Sel_char
elif type(Sel_char) == int:
Action = Action + str(Sel_char)
print("Action: " + Action)
Plist = addplaylist(Action)
if Plist is not None:
print("Added to playlist")
Playlist.append(Plist)
print(Plist)
MusicPlayer(Playlist)
else:
pass
#
# Read keyboard input
#
with Listener(on_press=on_press) as listener:
listener.join()
print ("")
print ("Have a nice day!")
print ("")
sys.exit()
The way you have it coded, I expect you'd have to stop it, remove the current media from the list, then re-start it.
However you may be better off running with a media_list_player, see below for a bare bones version. Note, I'm on Linux and had to hack some code to get the key input, rather than using a specific library or spend time on it, but it should give you enough to work with.
Edit
I apologise, there is a much simpler method when using a media_list_player, although if you want finer control you should use the media_list_player where you control the list's index or for full control use a media_player_new() but that's beyond the scope of this question. (I'll leave the original answer below this code, as it may be useful)
import vlc
import time
## pinched from vlc for keyboard input
import termios
import tty
import sys
mymedia = ["vp.mp3","vp1.mp3","happy.mp3","V1.mp4"]
def getch(): # getchar(), getc(stdin) #PYCHOK flake
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return ch
## end pinched code
def jukebox():
player.play_item_at_index(0)
while True:
time.sleep(0.25)
k = getch()
if k == "n": #Next
player.next()
if k == "p": #Previous
player.previous()
if k == " ": #Pause
player.pause()
if k == "q": #Quit
player.stop()
return True
player.next()
vlc_instance = vlc.Instance('--no-xlib --quiet ') # no-xlib for linux and quiet don't complain
player = vlc_instance.media_list_player_new()
Media = vlc_instance.media_list_new(mymedia)
player.set_media_list(Media)
print("Welcome to Jukebox")
print("Options - space = Play/Pause, n = Next, p = Previous, q = Quit")
print("Media",mymedia)
jukebox()
Original code
import vlc
import time
## pinched from vlc for keyboard input
import termios
import tty
import sys
def getch(): # getchar(), getc(stdin) #PYCHOK flake
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return ch
## end pinched code
vlc_instance = vlc.Instance()
player = vlc_instance.media_list_player_new()
mymedia = ["vp.mp3","vp1.mp3","happy.mp3"]
Media = vlc_instance.media_list_new(mymedia)
player.set_media_list(Media)
for index, name in enumerate(mymedia):
print("Playing:",name)
player.play_item_at_index(index)
time.sleep(1)
while player.get_state() != 6:
time.sleep(1)
k = getch()
if k == "y":
player.stop()
break

How can I play random audio files based on current time in Python 2.7?

Background: I'm using as Raspberry Pi rev 2 B to run a nature sound white noise generator of sorts that will randomly play audio tracks of varying length based on the time of night/morning. Some tracks are only a minute, some are several hours long. I'm looking for a way to check the time and change which type of sounds play based on time.
Current issue: I can start the appropriate audio for the time when the program first executes, but the timeloop execution stops polling once omxplayer starts up.
I have tried to call OMXPlayer without interrupting the time checker that determines what kind of audio to play, but once the audio playback starts I have been unable to continue checking time. Even if the play_audio() function wasn't recursive I would still like a way for the time checker to continue executing while the audio plays
#!/usr/bin/env python
import datetime, time, os, subprocess, random
from timeloop import Timeloop
from datetime import timedelta
from time import sleep
from omxplayer.player import OMXPlayer
from pathlib import Path
tl = Timeloop()
running_cycle = "off" # default value for the time cycle currently running
#function to check current time cycle
def check_time () :
dt_now = datetime.datetime.now()
t_now = dt_now.time()
t_night = datetime.time(hour=2,minute=0)
t_twilight = datetime.time(hour=4,minute=45)
t_morning = datetime.time(hour=7,minute=0)
t_end = datetime.time(hour=10,minute=0)
if t_night <= t_now < t_twilight:
return "night"
elif t_twilight <= t_now < t_morning:
return "twilight"
elif t_morning <= t_now < t_end:
return "morning"
else:
return "off"
# function that plays the audio
def play_audio (time_cycle):
subprocess.call ("killall omxplayer", shell=True)
randomfile = random.choice(os.listdir("/home/pi/music/nature-sounds/" + time_cycle))
file = '/home/pi/music/nature-sounds/' + time_cycle + '/' + randomfile
path = Path(file)
player = OMXPlayer(path)
play_audio (time_cycle)
# function that determines whether to maintain current audio cycle or play another
def stay_or_change():
global running_cycle
current_cycle = check_time()
if running_cycle != current_cycle:
if current_cycle == "off" :
player.quit()
else:
running_cycle = current_cycle
print "Now playing: " + running_cycle + " #{}".format(time.ctime())
play_audio(running_cycle)
#starts timeloop checker to play audio - works until stay_or_change() calls play_audio
#tl.job(interval=timedelta(seconds=10))
def job_10s():
print "10s job - running cycle: " + running_cycle + " - current time: {}".format(time.ctime())
stay_or_change()
# starts the timeloop
if __name__ == "__main__":
tl.start(block=True)
I have also tried running OMXPlayer with subprocess.run() but it still seems to hang up after the player starts. I'm completely open to any recommendations for background threading media players, process daemons, or time based execution methods.
I'm new to Python.
I had the recursion all wrong so it got caught in an infinite loop and the timeloop function wasn't really viable for this solution. Instead I had a function that played the sound, and then called the function that checked the time and plays from the appropriate sub-directory (or play nothing and wait).
Here's what I managed to come up with:
#!/usr/bin/env python
import datetime, time, os, subprocess, random
from datetime import timedelta
from time import sleep
from omxplayer.player import OMXPlayer
def check_time () :
dt_now = datetime.datetime.now()
t_now = dt_now.time()
t_night = datetime.time(hour=0,minute=0)
t_twilight = datetime.time(hour=5,minute=45)
t_morning = datetime.time(hour=7,minute=45)
t_end = datetime.time(hour=10,minute=0)
if t_night <= t_now < t_twilight:
return "night"
elif t_twilight <= t_now < t_morning:
return "twilight"
elif t_morning <= t_now < t_end:
return "morning"
else:
return "off"
def play_audio (time_cycle):
randomfile = random.choice(os.listdir("/home/pi/music/nature-sounds/" + time_cycle))
file = '/home/pi/music/nature-sounds/' + time_cycle + '/' + randomfile
print "playing track: " + randomfile
cmd = 'omxplayer --vol -200 ' + file
subprocess.call (cmd, shell=True)
what_to_play()
def what_to_play():
current_cycle = check_time()
if current_cycle == "off" :
print "sounds currently off - #{}".format(time.ctime())
time.sleep(30)
what_to_play()
else:
print "Now playing from " + current_cycle + " #{}".format(time.ctime())
play_audio(current_cycle)
what_to_play()

Python - Detect input within a terminal screensaver

I'm writing a code that basically runs without a loop for the sake of mobility and functionality. Let's say that when I run the program I use the -i option to continue using the python interpreter as it loads every function written. The thing is that I'm using a screensaver like function after the program print some basic information to make it look not so boring.
My question is: How can I maintain the screen saver running without blocking the interpreter prompt. I already wrote how to handle keyboard input to stop it using curses "getch()". I tried using threading but with no avail as it doesn't detect any keyboard input so it keeps running.
Is there any way I can get around this? How can I detect any input without blocking therefore retaining the interpreter prompt?
Right know it detects an input and raise a KeyboardException captured to make the screensaver stop.
Thanks in advance
I'm using this code with some added modifications:
Matrix-Curses
What I have so far:
Completed Code
This are the modifications done to the code:
def clear(int=None):
""" Clear Terminal Screen """
from subprocess import call
call('clear')
if int == 0:
exit()
def Matrix():
steps = 0
global scr
curses.curs_set(0)
curses.noecho()
if USE_COLORS:
curses.start_color()
curses.use_default_colors()
curses.init_pair(COLOR_CHAR_NORMAL, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(COLOR_CHAR_HIGHLIGHT, curses.COLOR_WHITE, curses.COLOR_GREEN)
curses.init_pair(COLOR_WINDOW, curses.COLOR_GREEN, curses.COLOR_GREEN)
height, width = scr.getmaxyx()
window_animation = None
lines = []
for i in range(DROPPING_CHARS):
l = FallingChar(width, MIN_SPEED, MAX_SPEED)
l.y = randint(0, height-2)
lines.append(l)
scr.refresh()
while True:
height, width = scr.getmaxyx()
for line in lines:
line.tick(scr, steps)
for i in range(RANDOM_CLEANUP):
x = randint(0, width-1)
y = randint(0, height-1)
scr.addstr(y, x, ' ')
if randint(0, WINDOW_CHANCE) == 1:
if window_animation is None:
#start window animation
line = random.choice(lines)
window_animation = WindowAnimation(line.x, line.y)
if not window_animation is None:
still_active = window_animation.tick(scr, steps)
if not still_active:
window_animation = None
scr.refresh()
time.sleep(SLEEP_MILLIS)
if SCREENSAVER_MODE:
key_pressed = scr.getch() != -1
if key_pressed:
raise KeyboardInterrupt
steps += 1
def ScreenSaver():
from time import sleep
from datetime import datetime as dt
global scr
TimeLimit = 10
StartTime = dt.now()
while True:
try:
sleep(1)
StopTime = (dt.now() - StartTime)
LastTime = StopTime.days*86400000 + StopTime.seconds*1000 + StopTime.microseconds/1000
if LastTime >= TimeLimit:
GetLocaleStatus = locale.getlocale()
locale.setlocale(locale.LC_ALL, '')
scr = curses.initscr()
scr.nodelay(1)
key_being_pressed = scr.getch() != -1
if not key_being_pressed:
try:
Matrix()
except KeyboardInterrupt:
TimeLimit = 30000
StartTime = dt.now()
raise KeyboardInterrupt
except KeyboardInterrupt:
curses.endwin()
curses.curs_set(1)
curses.reset_shell_mode()
curses.echo()
clear()
# return
main()

Input without stopping program

I am trying to make a countdown timer that prints the time remaining, and when you input something, it prints what you inputted. My problem is I don't want to wait for the input, just keep running the timer. My incorrect code:
timer = 100
while True:
print(timer)
timer -= 1
if input('> '):
print('the output of input')
You could say I want to have the timer printing the time in the background.
Here's a function that will timeout if no input is given:
import select
import sys
def timeout_input(timeout, prompt="", timeout_value=None):
sys.stdout.write(prompt)
sys.stdout.flush()
ready, _, _ = select.select([sys.stdin], [], [], timeout)
if ready:
return sys.stdin.readline().rstrip('\n')
else:
sys.stdout.write('\n')
sys.stdout.flush()
return timeout_value
You can easily modify it so it shows the remaining time by changing the timeout value on select.select to 1, and looping timeout times.
Standard input and standard output (accessed via input() and print()) are not a good choice for writing interactive asynchronous user interface (UI). Python support a few UIs via it's standard library. For example, curses is a text based user interface available on many POSIX systems. Here is an exmaple code for showing a countdown timer while accepting a number from the user:
import time
import curses
def get_number(seconds):
def _get_number(stdscr):
stdscr.clear()
timeout = time.time() + seconds
s = ""
while time.time() <= timeout:
time_left = int(timeout - time.time())
stdscr.addstr(0, 0, 'Countdown: {} {}'.format(time_left,
"*" * time_left + " " * seconds))
stdscr.addstr(2, 0, ' ' * 50)
stdscr.addstr(2, 0, 'Your Input: {}'.format(s))
stdscr.refresh()
stdscr.timeout(100)
code = stdscr.getch()
stdscr.addstr(10, 0, 'Code: {}'.format(code)) # for debug only
stdscr.refresh()
if ord("0") <= code <= ord("9"):
s += chr(code)
continue
if code == 10 and s: # newline
return int(s)
if code == 127: # backspace
s = s[:-1]
return curses.wrapper(_get_number)
print(get_number(10))

Pause and resume a running script in Python 3.42 in Windows

I'm new to Python and have been googling for a couple of days and read all I can find on this forum. Might be that I don't understand it all but I haven't found a solution to my problem yet. Ask for forgiveness already if there's an answer already to my problem, then I haven't understood it.
I want to make a Pause function for my program Tennismatch. The program will when it's being run print the score of a tennis match like this: "15-0, 15-15 etc ongoing till the match ends. It will print the score line by line.
I want the user to be able to pause after x number of balls, games, etc. So I don't know when the user wants to pause and after the user has paused I want the user to be able to resume the tennismatch where it was.
Have seen the time.sleep() but as I have understood it you must know when you want to pause to use this and it also ain't an indefinetie pause like I want. With input() it's the same.
Am going to make a GUI later on when the code is finished. Happy for anything that leads me to solving my problem.
I use Windows and Python 3.42 and run the program in Shell.
A piece of the code (haven't written it all yet, it's more of a general situation when something is being printed line after line for some time and want to be able do pause in the CIL:
#self.__points = [0,0]
def playGame(self):
if self.server == True: #self.server is either True or False when someone calls playGame()
server = self.player_1.get_win_serve() #self.player_1 = an object of a class Player():
else:
server = self.player_2.get_win_serve() #get_win_serve() method returns the probability to win his serv (1-0)
while (0 < self.__points[0] - self.__points[1] >= 2 or 0 < self.__points[1] - self.__points[0] >= 2) and (self.__points[1] >= 4 or self.__points[0] >= 4):
x = random.uniform(0,1)
if x > 0 and x < server:
self.__points[0] += 1
else:
self.__points[1] += 1
# print('The score, by calling a score() function that I haven't written yet')
For dealing with events in main loop you need to make a separated thread which capture input or any other event.
import sys
from sys import stdin
from time import sleep
from threading import Thread
from Queue import Queue, Empty
def do_something():
sleep(1)
print 42
def enqueue_output(queue):
while True:
# reading line from stdin and pushing to shared queue
input = stdin.readline()
print "got input ", input
queue.put(input)
queue = Queue()
t = Thread(target=enqueue_output, args=(queue,))
t.daemon = True
t.start()
pause = False
try:
while True:
try:
command = queue.get_nowait().strip()
print 'got from queue ', command
except Empty:
print "queue is empty"
command = None
if command:
if command == 'p':
pause = True
if command == 'u':
pause = False
if not pause:
print pause
do_something()
except KeyboardInterrupt:
sys.exit(0)
I came up with the following.
while True:
try:
## Keep doing something here
## your regular code
print '.',
except KeyboardInterrupt:
## write or call pause function which could be time.sleep()
print '\nPausing... (Hit ENTER to continue, type quit to exit.)'
try:
response = raw_input()
if response.lower() == 'quit':
break
print 'Quitting...'
except KeyboardInterrupt:
print 'Resuming...'
continue
The Event loop might as well be the code I wrote with.
I don't see any user input so I assume that x emulates it. To pause the game if x < 0.1 and to unpause(/resume) it if x > 0.9, you could:
while your_condition(self.__points):
x = random.random()
if x < 0.1: # pause
self.pause()
elif x > 0.9: # resume
self.resume()
if self.is_paused:
continue # do nothing else only wait for input (`x`)
# assume your_condition() has no side-effects
# here's what the resumed version does:
print("...")
# change self.__points, etc
where pause(), resume(), is_paused() methods could be implemented as:
def __init__(self):
self.is_paused = False
def pause(self):
self.is_paused = True
def resume(self):
self.is_paused = False
as you can see the implementation is very simple.

Categories