]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/footer.py
f0e30082035132ff1e97091260d5af2f9318888c
[vlc] / bindings / python-ctypes / footer.py
1 ### Start of footer.py ###
2
3 class MediaEvent(ctypes.Structure):
4     _fields_ = [
5         ('media_name', ctypes.c_char_p),
6         ('instance_name', ctypes.c_char_p),
7         ]
8
9 class EventUnion(ctypes.Union):
10     _fields_ = [
11         ('meta_type', ctypes.c_uint),
12         ('new_child', ctypes.c_uint),
13         ('new_duration', ctypes.c_longlong),
14         ('new_status', ctypes.c_int),
15         ('media', ctypes.c_void_p),
16         ('new_state', ctypes.c_uint),
17         # Media instance
18         ('new_position', ctypes.c_float),
19         ('new_time', ctypes.c_longlong),
20         ('new_title', ctypes.c_int),
21         ('new_seekable', ctypes.c_longlong),
22         ('new_pausable', ctypes.c_longlong),
23         # FIXME: Skipped MediaList and MediaListView...
24         ('filename', ctypes.c_char_p),
25         ('new_length', ctypes.c_longlong),
26         ('media_event', MediaEvent),
27         ]
28
29 class Event(ctypes.Structure):
30     _fields_ = [
31         ('type', EventType),
32         ('object', ctypes.c_void_p),
33         ('u', EventUnion),
34         ]
35
36 # Decorator for callback methods
37 callbackmethod=ctypes.CFUNCTYPE(None, Event, ctypes.c_void_p)
38
39 # Example callback method
40 @callbackmethod
41 def debug_callback(event, data):
42     print "Debug callback method"
43     print "Event:", event.type
44     print "Data", data
45
46 if __name__ == '__main__':
47     import sys
48     try:
49         from msvcrt import getch
50     except ImportError:
51         def getch():
52             import tty
53             import termios
54             fd=sys.stdin.fileno()
55             old_settings=termios.tcgetattr(fd)
56             try:
57                 tty.setraw(fd)
58                 ch=sys.stdin.read(1)
59             finally:
60                 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
61             return ch
62
63     @callbackmethod
64     def end_callback(event, data):
65         print "End of stream"
66         sys.exit(0)
67
68     if sys.argv[1:]:
69         if sys.platform == 'win32' and plugin_path is not None:
70             i=Instance('--plugin-path', plugin_path)
71         else:
72             i=Instance()
73         m=i.media_new(sys.argv[1])
74         p=i.media_player_new()
75         p.set_media(m)
76         p.play()
77
78         e=p.event_manager()
79         e.event_attach(EventType.MediaPlayerEndReached, end_callback, None)
80
81         def print_info():
82             """Print information about the media."""
83             m=p.get_media()
84             print "State:", p.get_state()
85             print "Media:", m.get_mrl()
86             try:
87                 print "Current time:", p.get_time(), "/", m.get_duration()
88                 print "Position:", p.get_position()
89                 print "FPS:", p.get_fps()
90                 print "Rate:", p.get_rate()
91                 print "Video size: (%d, %d)" % (p.video_get_width(), p.video_get_height())
92             except Exception:
93                 pass
94
95         def forward():
96             """Go forward 1s"""
97             p.set_time(p.get_time() + 1000)
98
99         def one_frame_forward():
100             """Go forward one frame"""
101             p.set_time(p.get_time() + long(1000 / (p.get_fps() or 25)))
102
103         def one_frame_backward():
104             """Go backward one frame"""
105             p.set_time(p.get_time() - long(1000 / (p.get_fps() or 25)))
106
107         def backward():
108             """Go backward 1s"""
109             p.set_time(p.get_time() - 1000)
110
111         def print_help():
112             """Print help
113             """
114             print "Commands:"
115             for k, m in keybindings.iteritems():
116                 print "  %s: %s" % (k, (m.__doc__ or m.__name__).splitlines()[0])
117             print " 1-9: go to the given fraction of the movie"
118
119         def quit():
120             """Exit."""
121             sys.exit(0)
122
123         keybindings={
124             'f': p.toggle_fullscreen,
125             ' ': p.pause,
126             '+': forward,
127             '-': backward,
128             '.': one_frame_forward,
129             ',': one_frame_backward,
130             '?': print_help,
131             'i': print_info,
132             'q': quit,
133             }
134
135         print "Press q to quit, ? to get help."
136         while True:
137             k=getch()
138             o=ord(k)
139             method=keybindings.get(k, None)
140             if method is not None:
141                 method()
142             elif o >= 49 and o <= 57:
143                 # Numeric value. Jump to a fraction of the movie.
144                 v=0.1*(o-48)
145                 p.set_position(v)
146
147