Skip to content

Instantly share code, notes, and snippets.

@thekindlyone
Created December 28, 2013 19:51
Show Gist options
  • Save thekindlyone/8163402 to your computer and use it in GitHub Desktop.
Save thekindlyone/8163402 to your computer and use it in GitHub Desktop.
VLC player hack to load prev/next files in folder in one keystroke. once assigned to windows right click on video files via regedit, uses pyhook to bind HOME and END keys to load prev and next files in folder. stops and starts vlc processes to achieve this behavior.
import os
import sys
import pythoncom, pyHook
import win32api
import subprocess
import ctypes
import threading
from multiprocessing import *
import time
class vlc(object):
def __init__(self,filepath,vlcp):
self.fn=filepath
self.vlcpath=vlcp
self.process=None
self.restart(filepath)
def kill(self):
p, self.process = self.process, None
if p is not None and p.poll() is None:
p.kill()
p.wait()
def is_alive(self):
if self.process is not None and self.process.poll() is None:
return True
else:
return False
def restart(self,filepath):
self.kill()
self.process = subprocess.Popen([self.vlcpath,filepath],close_fds=True)
self.fn=filepath
def play_next(self):
f=self.get_new_file(1)
self.restart(f)
def play_prev(self):
f=self.get_new_file(-1)
self.restart(f)
def get_new_file(self,switch):
dirname= os.path.dirname(self.fn)
supplist=['.mkv','.flv','.avi','.mpg','.wmv','.ogm','.mp4','.rmvb']
files = [os.path.join(dirname,f) for f in os.listdir(dirname) if (os.path.isfile(os.path.join(dirname,f)) and os.path.splitext(f)[-1]in supplist)]
files.sort()
#print files
try: currentindex=files.index(self.fn)
except: currentindex=0
i=0
if switch==1:
if currentindex<(len(files)-1):i=currentindex+1
else:
if currentindex>0:i=currentindex-1
return files[i]
class vlcThread(threading.Thread):
def __init__(self,filepath,vlcp,fl):
threading.Thread.__init__(self)
self.fn,self.vlcpath,self.flag=filepath,vlcp,fl
self.daemon=True
def run(self):
vlcinstance=vlc(self.fn,self.vlcpath)
last_alive=time.time()
while True:
time.sleep(0.3)
if vlcinstance.is_alive(): last_alive=time.time()
else:
print time.time()-last_alive
if (time.time()-last_alive)>20:
sys.exit(0)
break
if(self.flag.value==1):
vlcinstance.play_next()
self.flag.value=0
if(self.flag.value==-1):
vlcinstance.play_prev()
self.flag.value=0
class hookThread(threading.Thread):
def __init__(self,flag):
threading.Thread.__init__(self)
self.flag=flag
self.daemon=True
def kbeventhandler(self,event):
if event.Key=='Home' and 'vlc' in event.WindowName.lower():
self.flag.value =-1
return False
if event.Key=='End' and 'vlc' in event.WindowName.lower():
self.flag.value =1
return False
return True
def run(self):
hm = pyHook.HookManager()
hm.KeyDown = self.kbeventhandler
hm.HookKeyboard()
pythoncom.PumpMessages()
def main():
vlcpath='vlc'
flag=Value('i')
flag.value=0
if os.name=='nt': vlcpath='C:/Program Files (x86)/VideoLAN/VLC/vlc.exe'
fn='H:\\Anime\\needless\\Needless_[E-D]\\[Exiled-Destiny]_Needless_Ep11v2_(04B16479).mkv'
if len(sys.argv)>1:
fn=sys.argv[1] #use argument if available or else use default file
t=vlcThread(fn,vlcpath,flag)
h=hookThread(flag)
t.start()
h.start()
t.join()
print "bye bye"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment