]> git.plutz.net Git - viper/blob - chunk.py
implemeted most basic cropping controls
[viper] / chunk.py
1 #encoding: utf-8
2 #Copyright 2009 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 from flic import *
20
21 class Chunk(Flic):
22     """
23     This object contains a single piece of viedeo from one file.
24
25     The video can be the whole video stored in the file (which is the
26     default) or a part of this video. The file is never affected due to
27     operations on this object and temporary files are not created.
28
29     Interesting object properties:
30     videoprops   - a dictionary containing video properties as reported from mplayer
31                    keys are for example ID_VIDEO_WIDTH or ID_LENGTH
32                    run 'mplayer -identify videofile' to get an idea of the values
33                    stored here
34     """
35
36     def __init__(self, file, start = None, frames = None):
37         """
38         Constructor
39
40         file (string) - the name of a video file
41         start (float) - the second which marks the start of the chunk
42                         in video compression formats which are based on storing
43                         delta frames this can only be key frame precise
44         frames (int)  - the number of frames the chunk consists of 
45                         (by default all frames in the file)
46         """
47         Flic.__init__(self)
48         self.videofile = ''
49         self.start = 0
50         self.frames = 0
51         self.videoprops = {}
52
53         self.videofile = file
54         self.reset()
55         if start != None:
56             self.start = start
57         if frames != None:
58             self.frames = frames
59
60     def option(self, file = None, start = None, frames = None):
61         """
62         Change object properties
63
64         file (string) - the name of a video file
65                         (changing this will reinitialize the object)
66         start (float) - the second which marks the start of the chunk
67         frames (int)  - the number of frames the chunk consists of 
68         """
69
70         if file != None:
71             rem_start = self.start
72             rem_frames = self.frames
73             self.videofile = file
74             self.reset()
75             self.start = rem_start
76             self.frames = rem_frames
77         if start != None:
78             self.start = start
79         if frames != None:
80             self.frames = frames
81
82     def reset(self):
83         """
84         Get video properties
85
86         this method is for internal use, calling it will stop the video and reset
87         the 'start' and 'frame' properties
88         """
89         self.stop()
90
91         self.slave = os.popen('mplayer -frames 0 -identify "'+self.videofile+'"')
92         line = self.slave.readline()
93         while line != '':
94             if line.find('ID_') == 0:
95                 line = line.split('\n')[0]
96                 [key, value] = line.split('=', 1)
97                 self.videoprops[key] = value
98             line = self.slave.readline()
99         self.slave.close()
100         self.slave = None
101
102         self.start = 0
103         self.frames = int(float(self.videoprops['ID_VIDEO_FPS']) *
104                           float(self.videoprops['ID_LENGTH'])
105                               )
106         self.width = int(self.videoprops['ID_VIDEO_WIDTH'])
107         self.height = int(self.videoprops['ID_VIDEO_HEIGHT'])
108
109     def slave_video(self):
110         """
111         Return a list of mplayer command line arguments,
112         which specifies the video to play.
113
114         In this case:
115         ['videofile', '-ss', start, '-frames', frames]
116         """
117   
118         return [self.videofile,
119                 '-ss', str(self.start),
120                 '-frames', str(self.frames)
121                 ]
122