Skip to content

Instantly share code, notes, and snippets.

@yourcelf
Created May 18, 2012 22:59
Show Gist options
  • Save yourcelf/2728011 to your computer and use it in GitHub Desktop.
Save yourcelf/2728011 to your computer and use it in GitHub Desktop.
Command line wrapper for mplayer to make rifftrax syncing easier
#!/usr/bin/env python
import sys
import time
import subprocess
def main(vid, aud):
try:
vidproc = subprocess.Popen(["mplayer", vid], stdin=subprocess.PIPE)
audproc = subprocess.Popen(["mplayer", aud], stdin=subprocess.PIPE)
while True:
key = getch()
if ord(key) == 3:
raise KeyboardInterrupt
elif key == 'd':
print "Delaying video..."
vidproc.stdin.write(' ')
time.sleep(0.1)
vidproc.stdin.write(' ')
elif key == 'a':
print "Delaying audio..."
audproc.stdin.write(' ')
time.sleep(0.1)
audproc.stdin.write(' ')
else:
vidproc.stdin.write(key)
audproc.stdin.write(key)
except KeyboardInterrupt:
pass
finally:
vidproc.kill()
audproc.kill()
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])
@yourcelf
Copy link
Author

Usage:

./mplayer-rifftrax.py video_file audio_file

Once playing, the usual mplayer keyboard commands are sent to both video and audio players.

Additionally, press 'd' to delay the video by 0.1 seconds, and 'a' to delay the audio by 0.1 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment