]> git.plutz.net Git - viper/blob - flic.py
implemeted most basic cropping controls
[viper] / flic.py
1 #encoding: utf-8
2 #Copyright 2009 - 2010 Paul Hänsch
3
4 #This file is part of Viper.
5
6 #Viper is free software: you can redistribute it and/or modify
7 #it under the terms of the GNU General Public License as published by
8 #the Free Software Foundation, either version 3 of the License, or
9 #(at your option) any later version.
10
11 #Viper is distributed in the hope that it will be useful,
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #GNU General Public License for more details.
15
16 #You should have received a copy of the GNU General Public License
17 #along with Viper.  If not, see <http://www.gnu.org/licenses/>
18
19 import os
20
21 class Flic:
22     """
23     This class provides basic video playback and filter capability.
24
25     The class is meant to be inheritet by higher level video widgets.
26     Properties are:
27     slave (int)       - pid of the enslaved mplayer
28     filters (list of  - a list of filters, you should use add_filter() and
29              strings)   del_filter() to access it
30     width (int)       - the inheriting class must set the video width
31     height (int)      - the inheriting class must set the video height
32     """
33
34     def __init__(self):
35         self.slave = None
36         self.filters = []
37         self.width = 0
38         self.height = 0
39
40     def stop(self):
41         """
42         Stop playback of the video
43         """
44         if self.slave != None:
45             try:
46                 os.kill(self.slave, 15)
47             except:
48                 print 'video slave has been terminated unexpectedly'
49         self.slave = None
50
51     def play(self, wid = None, width = None, height = None):
52         """
53         Start playback of the video
54
55         The video will be looped until stop() is called or hell freezes over
56         wid -  X11 window id of a window which shall serve as video frame
57                if 'wid' is omitted the play() will open a new window
58         """
59
60         if width == None:
61             width = int(self.width)
62         if height == None:
63             height = int(self.height)
64
65         self.stop()
66         if wid == None:
67             args = ['', '-really-quiet', '-fixed-vo', '-loop', '0',
68                     '-nosound', '-framedrop', '-slave', '-vo', 'x11',
69                     '-x', str(width), '-y', str(height)]
70             args.extend(self.slave_subcommand())
71         else:
72             args = ['', '-wid', str(wid), '-really-quiet', '-fixed-vo',
73                     '-loop', '0', '-nosound', '-framedrop', '-slave',
74                     '-x', str(width), '-y', str(height), '-vo', 'x11']
75             args.extend(self.slave_subcommand())
76
77         self.slave = os.spawnvp(os.P_NOWAIT, 'mplayer', args)
78                                    
79     def slave_subcommand(self):
80         """
81         Return a list of mplayer command line arguments to play the chunk
82
83         The argument list will have the form
84         ['videofile', '-ss', start, '-frames', frames, filters... ]
85         the list can easily be appended to a mplayer command line
86         """
87         ret = [self.slave_video()[0]]
88         for filter in self.filters:
89             ret.extend(filter)
90         if self.slave_video().__len__() > 1:
91             ret.extend(self.slave_video()[1:])
92         return ret
93                 
94
95     def slave_video(self):
96         """
97         Return a list of mplayer command line arguments,
98         which specifies the video to play.
99
100         This can be a filename or an URL. The method is meant
101         to be overridden as it only returns an empty string.
102         """
103         return ''
104
105     def add_filter(self, filterdef):
106         """
107         Apply a videofilter to the chunk.
108
109         Filterdef is a list of mplayer options,
110         i.e. ['-vf-add', 'expand=-60:0'].
111         Several filters can be applied to one chunk.
112         """
113         self.stop()
114         self.filters.append(filterdef)
115         self.play()
116
117     def del_filter(self, filterdef):
118         """
119         Opposite to add_filter.
120
121         Removes a given filter from the list
122         """
123         self.stop()
124         self.filters.remove(filterdef)
125         self.play()
126
127     def filter_eq(self, f1, f2):
128         """
129         Check if two filter lines are equal
130         """
131         if str(f1) == str(f2): return True
132         else: return False