Created
January 2, 2018 19:11
-
-
Save takuya/527527610a69029b1b25a3829fd9229d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import re | |
import atexit | |
import inspect | |
import time | |
import subprocess | |
import atexit | |
import subprocess | |
import shlex | |
import argparse | |
class CecClient(object): | |
regex_pattern = "(?<=key released: )\w+\s(((\w+)?\s?(\w+)?)?\s?\(\w+\))" | |
def __init__(self): | |
self.pattern = re.compile( CecClient.regex_pattern ) | |
def start(self): | |
self.cec_proc = subprocess.Popen(["cec-client"], stdout=subprocess.PIPE) | |
while True: | |
line = self.cec_proc.stdout.readline() | |
line = line.decode("utf-8") | |
match = re.search( self.regex_pattern ,line ) | |
if match : | |
key = match[0] | |
self.dispatch(key) | |
def dispatch(self,key): | |
match = re.search( "[^\(]+", key ) | |
key = match[0] | |
key = re.sub('\s+$', "", key) | |
key = re.sub('\s', "_", key) | |
name = f"on_{key}" | |
if hasattr(self,name) : | |
method = getattr(self,name) | |
method() | |
else: | |
print(f"{key} pressed, but no action defined") | |
def on_right(self): | |
print(" right button.") | |
def on_down(self): | |
print(" down button.") | |
def on_left(self): | |
print(" left button.") | |
def on_up(self): | |
print(" up button.") | |
def on_play(self): | |
print(" play button.") | |
def on_select(self): | |
print(" select button.") | |
def on_pause(self): | |
print(" pause button.") | |
def on_forward(self): | |
print(" forward button.") | |
def on_backward(self): | |
print(" backward button.") | |
def on_exit(self): | |
print(" exit button.") | |
def on_clear(self): | |
print(" clear button.") | |
class OmxplayerWithTVRemote(CecClient): | |
KEY_UP = b'\[A' | |
KEY_DOWN = b'\[B' | |
KEY_RIGHT= b'\[C' | |
KEY_LEFT = b'\[D' | |
""" | |
1 decrease speed | |
2 increase speed | |
< rewind | |
> fast forward | |
z show info | |
j previous audio stream | |
k next audio stream | |
i previous chapter | |
o next chapter | |
n previous subtitle stream | |
m next subtitle stream | |
s toggle subtitles | |
w show subtitles | |
x hide subtitles | |
d decrease subtitle delay (- 250 ms) | |
f increase subtitle delay (+ 250 ms) | |
q exit omxplayer | |
p / space pause/resume | |
- decrease volume | |
+ / = increase volume | |
left arrow seek -30 seconds | |
right arrow seek +30 seconds | |
down arrow seek -600 seconds | |
up arrow seek +600 seconds | |
""" | |
keymap_of_omxplayer = { | |
'decrease speed': b'1', | |
'increase speed': b'2', | |
'rewind': b'<', | |
'fast forward': b'>', | |
'show info': b'z', | |
'previous audio stream': b'j', | |
'next audio stream': b'k', | |
'previous chapter': b'i', | |
'next chapter': b'o', | |
'previous subtitle stream': b'n', | |
'next subtitle stream': b'm', | |
'toggle subtitles': b's', | |
'show subtitles': b'w', | |
'hide subtitles': b'x', | |
'decrease subtitle delay (- 250 ms)': b'd', | |
'increase subtitle delay (+ 250 ms)': b'f', | |
'exit omxplayer': b'q', | |
'pause/resume': b'p', | |
'decrease volume': b'-', | |
'increase volume': b'+', | |
'seek -30 seconds': KEY_LEFT, | |
'seek +30 seconds': KEY_RIGHT, | |
'seek -600 seconds': KEY_DOWN, | |
'seek +600 seconds': KEY_UP, | |
} | |
keymap_tvremote_to_omxplayer_action ={ | |
'select':'pause/resume', | |
'right':'seek +30 seconds', | |
'left':'seek -30 seconds', | |
'down':'seek -600 seconds', | |
'up':'seek +600 seconds', | |
'F1':'decrease speed', | |
'F2':'increase speed', | |
'F3':'rewind', | |
'F4':'fast forward', | |
'exit':'exit omxplayer', | |
'channel_down':'decrease volume', | |
'channel_up':'increase volume', | |
'rewind':'rewind', | |
'Fast_forward':'fast forward', | |
} | |
def __init__(self): | |
super().__init__() | |
def play(self, url, *options): | |
cmd = f"omxplayer %s '{url}' " % ' '.join(options) | |
print(cmd) | |
cmd = shlex.split(cmd) | |
self.p = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE) | |
self.start() | |
def send_key_to_omxplayer(self,name_of_command): | |
key = self.keymap_of_omxplayer[name_of_command] | |
self.p.stdin.write(key) | |
self.p.stdin.flush() | |
def exit(self): | |
self.on_exit() | |
def on_exit(self): | |
self.p.terminate() | |
self.cec_proc.terminate() | |
def dispatch(self,key): | |
match = re.search( "[^\(]+", key ) | |
key = match[0] | |
key = re.sub('\s+$', "", key) | |
key = re.sub('\s', "_", key) | |
if key in self.keymap_tvremote_to_omxplayer_action: | |
omxplayer_key = self.keymap_tvremote_to_omxplayer_action[key] | |
self.send_key_to_omxplayer(omxplayer_key) | |
if key == 'exit': | |
self.on_exit() | |
exit() | |
else: | |
print(f"{key} pressed, but no action defined") | |
def main(args): | |
player = OmxplayerWithTVRemote() | |
def on_exit(): | |
player.exit() | |
pass | |
# print("Good-Bye\n") | |
atexit.register( on_exit ) | |
args = vars(args) | |
url = args['URL'][0] | |
options = args['omxplayer_options'] | |
player.play( url , *options ) | |
if __name__ == "__main__" : | |
try: | |
parser = argparse.ArgumentParser(description='omxplayer をTVリモコンと連動させて起動します。') | |
parser.add_argument('URL', metavar='URL', type=str, nargs=1,help='movie path or url') | |
parser.add_argument('omxplayer_options', metavar='options',nargs=argparse.REMAINDER,help='options pass to omxplayer') | |
args = parser.parse_args() | |
main(args) | |
except KeyboardInterrupt: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment