Guido van Rossum | 9ee7e15 | 1992-08-25 12:29:30 +0000 | [diff] [blame^] | 1 | #! /ufs/guido/bin/sgi/python |
| 2 | |
| 3 | # Edit CMIF movies interactively -- copy one or more files to an output file |
| 4 | |
| 5 | |
| 6 | # Possibilities: |
| 7 | # |
| 8 | # - convert between formats (grey, rgb, rgb8, ...) |
| 9 | # - change size |
| 10 | # - cut out a given area of the image |
| 11 | # - change time base (a la Vtime) |
| 12 | # - skip stretches of frames |
| 13 | |
| 14 | |
| 15 | import sys |
| 16 | import os |
| 17 | import gl, GL, DEVICE |
| 18 | import fl, FL |
| 19 | import flp |
| 20 | import Viewer |
| 21 | import getopt |
| 22 | import string |
| 23 | |
| 24 | |
| 25 | def main(): |
| 26 | qsize = 20 |
| 27 | opts, args = getopt.getopt(sys.argv[1:], 'q:') |
| 28 | for o, a in opts: |
| 29 | if o == '-q': |
| 30 | qsize = string.atoi(a) |
| 31 | ed = Editor().init(qsize) |
| 32 | if args[0:]: |
| 33 | ed.open_input(args[0]) |
| 34 | if args[1:]: |
| 35 | ed.open_output(args[1]) |
| 36 | while 1: |
| 37 | dummy = fl.do_forms() |
| 38 | |
| 39 | |
| 40 | class Editor: |
| 41 | |
| 42 | def init(self, qsize): |
| 43 | self.qsize = qsize |
| 44 | self.vin = None |
| 45 | self.vout = None |
| 46 | self.ifile = '' |
| 47 | self.ofile = '' |
| 48 | formdef = flp.parse_form('VeditForm', 'form') |
| 49 | flp.create_full_form(self, formdef) |
| 50 | self.form.show_form(FL.PLACE_SIZE, FL.TRUE, 'Vedit') |
| 51 | fl.set_event_call_back(self.do_event) |
| 52 | return self |
| 53 | |
| 54 | def do_event(self, (dev, val)): |
| 55 | if dev == DEVICE.REDRAW: |
| 56 | if self.vin: |
| 57 | self.vin.redraw(val) |
| 58 | if self.vout: |
| 59 | self.vout.redraw(val) |
| 60 | |
| 61 | |
| 62 | def iocheck(self): |
| 63 | self.msg('') |
| 64 | if self.vin == None and self.vout == None: |
| 65 | self.err('Please open input and output files first') |
| 66 | return 0 |
| 67 | return self.icheck() and self.ocheck() |
| 68 | |
| 69 | def icheck(self): |
| 70 | self.msg('') |
| 71 | if self.vin == None: |
| 72 | self.err('Please open an input file first') |
| 73 | return 0 |
| 74 | return 1 |
| 75 | |
| 76 | def ocheck(self): |
| 77 | self.msg('') |
| 78 | if self.vout == None: |
| 79 | self.err('Please open an output file first') |
| 80 | return 0 |
| 81 | return 1 |
| 82 | |
| 83 | |
| 84 | def cb_in_new(self, args): |
| 85 | self.msg('') |
| 86 | hd, tl = os.path.split(self.ifile) |
| 87 | filename = fl.file_selector('Input video file', hd, '', tl) |
| 88 | if not filename: return |
| 89 | self.open_input(filename) |
| 90 | |
| 91 | def cb_in_close(self, args): |
| 92 | self.msg('') |
| 93 | self.close_input() |
| 94 | |
| 95 | def cb_in_skip(self, args): |
| 96 | if not self.icheck(): return |
| 97 | if not self.vin.get(): self.err('End of input file') |
| 98 | self.ishow() |
| 99 | |
| 100 | def cb_in_back(self, args): |
| 101 | if not self.icheck(): return |
| 102 | if not self.vin.backup(): self.err('Input buffer exhausted') |
| 103 | self.ishow() |
| 104 | |
| 105 | def cb_in_rewind(self, args): |
| 106 | if not self.icheck(): return |
| 107 | self.vin.rewind() |
| 108 | self.ishow() |
| 109 | |
| 110 | |
| 111 | def cb_copy(self, args): |
| 112 | if not self.iocheck(): return |
| 113 | data = self.vin.get() |
| 114 | if data: |
| 115 | if self.vout.getinfo() <> self.vin.getinfo(): |
| 116 | print 'Copying info...' |
| 117 | self.vout.setinfo(self.vin.getinfo()) |
| 118 | self.vout.put(data) |
| 119 | self.oshow() |
| 120 | self.ishow() |
| 121 | |
| 122 | def cb_uncopy(self, args): |
| 123 | if not self.iocheck(): return |
| 124 | if not self.vout.backup(): |
| 125 | self.err('Output buffer exhausted') |
| 126 | return |
| 127 | self.oshow() |
| 128 | if not self.vin.backup(): |
| 129 | self.err('Input buffer exhausted') |
| 130 | return |
| 131 | self.ishow() |
| 132 | |
| 133 | |
| 134 | def cb_out_new(self, args): |
| 135 | self.msg('') |
| 136 | hd, tl = os.path.split(self.ofile) |
| 137 | filename = fl.file_selector('Output video file', hd, '', tl) |
| 138 | if not filename: return |
| 139 | self.open_output(filename) |
| 140 | |
| 141 | def cb_out_close(self, args): |
| 142 | self.msg('') |
| 143 | self.close_output() |
| 144 | |
| 145 | def cb_out_skip(self, arg): |
| 146 | if not self.ocheck(): return |
| 147 | if not self.vout.forward(): self.err('Output buffer exhausted') |
| 148 | self.oshow() |
| 149 | |
| 150 | def cb_out_back(self, args): |
| 151 | if not self.ocheck(): return |
| 152 | if not self.vout.backup(): self.err('Output buffer exhausted') |
| 153 | self.oshow() |
| 154 | |
| 155 | def cb_out_rewind(self, args): |
| 156 | if not self.ocheck(): return |
| 157 | self.vout.rewind() |
| 158 | self.oshow() |
| 159 | |
| 160 | |
| 161 | def cb_quit(self, args): |
| 162 | self.close_input() |
| 163 | self.close_output() |
| 164 | sys.exit(0) |
| 165 | |
| 166 | |
| 167 | def open_input(self, filename): |
| 168 | self.ifile = filename |
| 169 | basename = os.path.split(filename)[1] |
| 170 | title = 'in: ' + basename |
| 171 | try: |
| 172 | vin = Viewer.InputViewer().init(filename, \ |
| 173 | title, self.qsize) |
| 174 | except: |
| 175 | self.err('Can\'t open input file', filename) |
| 176 | return |
| 177 | self.close_input() |
| 178 | self.vin = vin |
| 179 | self.in_file.label = basename |
| 180 | self.ishow() |
| 181 | |
| 182 | def close_input(self): |
| 183 | if self.vin: |
| 184 | self.msg('Closing input file...') |
| 185 | self.vin.close() |
| 186 | self.msg('') |
| 187 | self.vin = None |
| 188 | self.in_file.label = '(none)' |
| 189 | self.format('in') |
| 190 | |
| 191 | def ishow(self): |
| 192 | self.vin.show() |
| 193 | self.format('in') |
| 194 | |
| 195 | def open_output(self, filename): |
| 196 | self.ofile = filename |
| 197 | basename = os.path.split(filename)[1] |
| 198 | title = 'out: ' + basename |
| 199 | try: |
| 200 | vout = Viewer.OutputViewer().init(filename, \ |
| 201 | title, self.qsize) |
| 202 | except: |
| 203 | self.err('Can\'t open output file', filename) |
| 204 | return |
| 205 | self.close_output() |
| 206 | self.vout = vout |
| 207 | self.out_file.label = basename |
| 208 | if self.vin: |
| 209 | self.vout.setinfo(self.vin.getinfo()) |
| 210 | self.oshow() |
| 211 | |
| 212 | def close_output(self): |
| 213 | if self.vout: |
| 214 | self.msg('Closing output file...') |
| 215 | self.vout.close() |
| 216 | self.msg('') |
| 217 | self.vout = None |
| 218 | self.out_file.label = '(none)' |
| 219 | self.format('out') |
| 220 | |
| 221 | def oshow(self): |
| 222 | self.vout.show() |
| 223 | self.format('out') |
| 224 | |
| 225 | |
| 226 | def msg(self, *args): |
| 227 | str = string.strip(string.join(args)) |
| 228 | self.msg_area.label = str |
| 229 | |
| 230 | def err(self, *args): |
| 231 | gl.ringbell() |
| 232 | apply(self.msg, args) |
| 233 | |
| 234 | def format(self, io): |
| 235 | v = getattr(self, 'v' + io) |
| 236 | if v == None: |
| 237 | left = right = pos = 0 |
| 238 | else: |
| 239 | left, right = v.qsizes() |
| 240 | pos = v.tell() |
| 241 | left = pos - left |
| 242 | right = pos + right |
| 243 | getattr(self, io + '_info1').label = `left` |
| 244 | getattr(self, io + '_info2').label = `pos` |
| 245 | getattr(self, io + '_info3').label = `right` |
| 246 | |
| 247 | main() |