Changeset 2493
- Timestamp:
- 2006-09-17 13:52:53 (2 years ago)
- Files:
-
- scripts/trunk/annotool.py (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
scripts/trunk/annotool.py
r2491 r2493 89 89 90 90 def open_file(self, accel_group, window, keyval, modifier): 91 print "Ctrl - O"91 if self.show_keystrokes: print "Ctrl - O" 92 92 dialog = gtk.FileChooserDialog("Open",self.window,gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) 93 93 dialog.set_default_response(gtk.RESPONSE_OK) … … 114 114 115 115 def import_file(self, accel_group, window, keyval, modifier): 116 print "Ctrl - I"116 if self.show_keystrokes: print "Ctrl - I" 117 117 dialog = gtk.FileChooserDialog("Import",self.window,gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) 118 118 dialog.set_default_response(gtk.RESPONSE_OK) … … 147 147 148 148 def save_file(self, accel_group, window, keyval, modifier): 149 print "Ctrl - S"149 if self.show_keystrokes: print "Ctrl - S" 150 150 dialog = gtk.FileChooserDialog("Save",self.window,gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK)) 151 151 dialog.set_default_response(gtk.RESPONSE_OK) … … 184 184 # Text overlay functions. 185 185 def toggle_track(self, accel_group, window, keyval, modifier): 186 print "Ctrl - T"186 if self.show_keystrokes: print "Ctrl - T" 187 187 keys = self.times.keys() 188 188 new_track = keys.index(self.current_track)+1 … … 192 192 # Navigation functions. 193 193 def restart_clip(self, accel_group, window, keyval, modifier): 194 print "Ctrl - R"194 if self.show_keystrokes: print "Ctrl - R" 195 195 self.seek(self.start_time.get_text()) 196 196 197 197 def current_clip(self,current_time): 198 x = 0199 while (x)<len(self.times[self.current_track]['start']) and current_time>=to_ms(self.times[self.current_track]['start'][x]):200 x += 1201 return x-1198 x = 0 199 while x<len(self.times[self.current_track]['start']) and current_time>=to_ms(self.times[self.current_track]['start'][x]): 200 x += 1 201 return x-1 202 202 203 203 def previous_clip(self, accel_group, window, keyval, modifier): 204 print "Ctrl - B"204 if self.show_keystrokes: print "Ctrl - B" 205 205 clip_now = self.current_clip(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND) 206 206 if clip_now > 0: … … 209 209 #Highlight chapter entry. 210 210 self.text.remove_tag(self.tag_current, self.text.get_start_iter(),self.text.get_end_iter()) 211 mark = self.text.get_mark(self. times[self.current_track]['start'][clip_now-1])211 mark = self.text.get_mark(self.current_track + "-" + self.times[self.current_track]['start'][clip_now-1]) 212 212 start_iter = self.text.get_iter_at_mark(mark) 213 213 if not start_iter.begins_tag(self.tag_clip): … … 231 231 232 232 def next_clip(self, accel_group, window, keyval, modifier): 233 print "Ctrl - N"233 if self.show_keystrokes: print "Ctrl - N" 234 234 #Only seek forward clips if more clips exist. 235 if len(self.times[self.current_track]['start'])>1 and self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND<to_ms(self.times[self.current_track]['start'][-1]): 236 x = 2 237 while x<len(self.times[self.current_track]['start']) and self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND<to_ms(self.times[self.current_track]['start'][-x]): 238 x += 1 239 if x > 3: 240 self.seek(self.times[self.current_track]['start'][-(x-1)], self.times[self.current_track]['start'][-(x-2)]) 241 else: 242 self.seek(self.times[self.current_track]['start'][-(x-1)]) 243 self.mode = 'transcribe' 244 235 clip_now = self.current_clip(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND) 236 if clip_now < len(self.times[self.current_track]['start'])-1: 237 if clip_now < len(self.times[self.current_track]['start'])-2: 238 self.seek(self.times[self.current_track]['start'][clip_now+1], self.times[self.current_track]['start'][clip_now+2]) 239 self.start_time.set_text(self.times[self.current_track]['start'][clip_now+1]) 240 self.end_time.set_text(self.times[self.current_track]['start'][clip_now+2]) 241 self.mode = 'review' 242 elif to_ms(self.times[self.current_track]['end'][-1]) > self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND: 243 self.seek(self.times[self.current_track]['start'][clip_now+1],self.times[self.current_track]['end'][-1]) 244 self.start_time.set_text(self.times[self.current_track]['start'][clip_now+1]) 245 self.end_time.set_text(self.times[self.current_track]['end'][-1]) 245 246 #Highlight chapter entry. 246 247 self.text.remove_tag(self.tag_current, self.text.get_start_iter(),self.text.get_end_iter()) 247 mark = self.text.get_mark(self. times[self.current_track]['start'][-(x-1)])248 mark = self.text.get_mark(self.current_track + "-" + self.times[self.current_track]['start'][clip_now+1]) 248 249 start_iter = self.text.get_iter_at_mark(mark) 249 250 if not start_iter.begins_tag(self.tag_clip): … … 256 257 self.textView.scroll_to_mark(mark,0.1) 257 258 258 self.mode = 'review'259 260 self.start_time.set_text(self.times[self.current_track]['start'][-(x-1)])261 self.end_time.set_text(self.times[self.current_track]['start'][-(x-2)])262 259 if self.pipeline_duration==10: 263 260 self.pipeline_duration = self.pipeline.query_duration(gst.FORMAT_TIME)[0]/gst.MSECOND 264 self.progress.props.fraction = float(to_ms(self.times[self.current_track]['start'][-(x-1)]))/self.pipeline_duration 265 else: 266 print self.current_track + " has no more clips." 261 self.progress.props.fraction = float(to_ms(self.times[self.current_track]['start'][clip_now+1]))/self.pipeline_duration 262 elif self.end_time.get_text()!="xx:xx:xx.xxx": 263 self.seek(self.end_time.get_text()) 264 self.text.remove_tag(self.tag_current, self.text.get_start_iter(),self.text.get_end_iter()) 265 self.mode = 'transcribe' 267 266 268 267 def just_play(self, accel_group, window, keyval, modifier): 269 print "Ctrl - P"268 if self.show_keystrokes: print "Ctrl - P" 270 269 if (self.pipeline.get_state()[1] != gst.STATE_PLAYING): 271 270 self.pipeline.set_state(gst.STATE_PLAYING); 271 self.progress.props.text = self.current_file + ": Playing." 272 272 273 273 def seek(self,start,end=None): 274 if end==None: 275 return self.pipeline.seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, gst.SEEK_TYPE_SET, gst.MSECOND*to_ms(start), gst.SEEK_TYPE_NONE, 0) 274 if start==end: 275 if self.debug: print "Error: Trying to seek to two identical times at " + start + "!" 276 return 277 elif end==None: 278 return self.pipeline.seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, gst.SEEK_TYPE_SET, gst.MSECOND*to_ms(start), gst.SEEK_TYPE_SET, self.pipeline_duration*gst.MSECOND) 276 279 else: 277 280 return self.pipeline.seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, gst.SEEK_TYPE_SET, gst.MSECOND*to_ms(start), gst.SEEK_TYPE_SET, gst.MSECOND*to_ms(end)) 278 281 279 def enter_activated(self, widget, data=None):282 def enter_activated(self, accel_group, window, keyval, modifier): 280 283 #Handles clip to text buffer, formatting and pipeline control. 281 print "Enter"284 if self.show_keystrokes: print "Enter" 282 285 if (self.pipeline.get_state()[1] == gst.STATE_PLAYING): 283 286 self.pipeline.set_state(gst.STATE_PAUSED); 284 self.textView.props.can_focus = True285 287 current_time = self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND 286 288 self.end_time.set_text(format_time(current_time)) … … 289 291 self.pipeline_duration = self.pipeline.query_duration(gst.FORMAT_TIME)[0]/gst.MSECOND 290 292 self.progress.props.fraction = float(current_time)/self.pipeline_duration 291 elif self.pipeline.get_state()[1] == gst.STATE_PAUSED and not self.mode=='review':293 elif self.pipeline.get_state()[1] == gst.STATE_PAUSED and self.mode=='transcribe': 292 294 text = self.clip_text.get_text() 293 if text[:1] == ")" or (text.find(":")==-1 and self.last_type_def==")") and self.start_time.get_text() != 'xx:xx:xx.xxx':295 if text[:1] == ")" or (text.find(":")==-1 and text != "" and self.last_type_def==")") and self.start_time.get_text() != 'xx:xx:xx.xxx': 294 296 #Chapter deocration. 295 297 self.text.insert(self.text.get_end_iter(),"\n\nChapter " + str(len(self.times["chapter"])+1) + ": ") 296 298 #Mark handling. 297 299 self.times["chapter"]['start'].append(self.start_time.get_text()) 298 mark = self.text.create_mark(self.start_time.get_text(),self.text.get_end_iter(),True) 300 #Add mark only if last end time does not equal this start time. 301 if len(self.times["chapter"]['end'])>0 and self.times["chapter"]['end'][-1]!=self.start_time.get_text(): 302 mark = self.text.create_mark("chapter-" + self.start_time.get_text(),self.text.get_end_iter(),True) 303 mark.set_visible(self.visible_marks) 304 #Insert text. 305 self.text.insert_with_tags(self.text.get_end_iter(),text.strip().strip(")"), self.tag_clip) 306 self.last_type_def = ")" 307 #Insert end mark. 308 self.times["chapter"]['end'].append(self.end_time.get_text()) 309 mark = self.text.create_mark("chapter-" + self.end_time.get_text(),self.text.get_end_iter(),True) 299 310 mark.set_visible(self.visible_marks) 300 #Insert text. 301 self.text.insert(self.text.get_end_iter(),text.strip().strip(")")) 302 self.last_type_def = ")" 303 elif text.find(":")!=-1 or self.last_type_def.endswith(":") and self.start_time.get_text() != 'xx:xx:xx.xxx': 311 elif text != "" and (text.find(":")!=-1 or self.last_type_def.endswith(":")) and self.start_time.get_text() != 'xx:xx:xx.xxx': 304 312 # If using the last type def make sure doesn't have ';' instead of intended ':' 305 313 if text.find(":")==-1 and text.find(";")!=-1: … … 321 329 #Mark handling. 322 330 self.times["subtitle"]['start'].append(self.start_time.get_text()) 323 mark = self.text.create_mark(self.start_time.get_text(),self.text.get_end_iter(),True) 324 mark.set_visible(self.visible_marks) 331 if len(self.times["subtitle"]['end'])>0 and self.times["subtitle"]['end'][-1]!=self.start_time.get_text(): 332 mark = self.text.create_mark("subtitle-" + self.start_time.get_text(),self.text.get_end_iter(),True) 333 mark.set_visible(self.visible_marks) 325 334 #Insert and tag speaker string. (will be invisible) 326 335 self.text.insert_with_tags(self.text.get_end_iter(),self.last_type_def.strip(":"),self.tag_speaker) 327 336 #Insert text. 328 self.text.insert(self.text.get_end_iter(),text[text.find(":")+1:].strip()) 329 if text != "" and self.start_time.get_text() != 'xx:xx:xx.xxx': 330 try: 331 #Underline contents. 332 clip_iter = self.text.get_iter_at_mark(mark) 333 if not clip_iter.starts_word(): 334 clip_iter.forward_word_end() 335 clip_iter.backward_word_start() 336 self.text.apply_tag(self.tag_clip,clip_iter,self.text.get_end_iter()) 337 except: 338 pass 337 self.text.insert_with_tags(self.text.get_end_iter(),text[text.find(":")+1:].strip(),self.tag_clip) 338 #Write end mark. 339 self.times["subtitle"]['end'].append(self.end_time.get_text()) 340 mark = self.text.create_mark("subtitle-" + self.end_time.get_text(),self.text.get_end_iter(),True) 341 mark.set_visible(self.visible_marks) 339 342 self.clip_text.set_text("") 340 343 self.start_time.set_text(format_time(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND)) 341 344 self.end_time.set_text("xx:xx:xx.xxx") 342 self.textView.props.can_focus = False343 345 self.scroll_to_bottom() 344 346 self.pipeline.set_state(gst.STATE_PLAYING); … … 347 349 self.start_time.set_text(format_time(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND)) 348 350 self.end_time.set_text("xx:xx:xx.xxx") 349 self.textView.props.can_focus = False350 351 self.pipeline.set_state(gst.STATE_PLAYING); 351 352 self.progress.props.text = self.current_file + ": Playing." … … 365 366 times.sort(lambda x,y: cmp(to_ms(x), to_ms(y))) 366 367 for time in times: 367 iter = self.text.get_iter_at_mark(self.text.get_mark( time))368 iter = self.text.get_iter_at_mark(self.text.get_mark(key + "-" + time)) 368 369 369 370 #Checks that the next mark is not at the same location. 370 371 try: 371 count = iter.get_marks().count(self.text.get_mark( times[x]))372 count = iter.get_marks().count(self.text.get_mark(key + "-" + times[x])) 372 373 except: 373 374 count = 0 … … 377 378 tmp_iter = iter.copy() 378 379 tmp_iter.forward_to_tag_toggle(self.tag_clip) 379 tmp_mark = self.text.get_mark( times[x])380 tmp_mark = self.text.get_mark(key + "-" + times[x]) 380 381 con = self.text.get_iter_at_mark(tmp_mark).compare(tmp_iter) 381 382 if con>0 or (con==0 and tmp_iter.ends_tag(self.tag_clip)): … … 406 407 last_iter.forward_to_tag_toggle(None) 407 408 desc = self.text.get_text(iter,last_iter) 409 if desc != "/placeholder/": 410 desc = desc.strip("/") 408 411 if len(times)>x: 409 412 clip = [time,times[x],desc] … … 519 522 #Make sure last_time has a record for the track. 520 523 if not last_time.has_key(track): 521 last_time[track]=None 522 523 # If a gap exists between previous end and current start add a mark to end previous clip. 524 if last_time[track] != start_time and last_time[track]!=None: 525 if track.startswith('subtitle'): #FIXME not nice with multiple subtitle tracks. 526 self.times["subtitle"]['end'].append(last_time[track]) 527 else: 528 self.times["chapter"]['end'].append(last_time[track]) 529 mark = self.text.create_mark(last_time[track],self.text.get_end_iter(),True) 530 mark.set_visible(self.visible_marks) 531 ended_previous = True 532 else: 533 ended_previous = False 524 last_time[track]=0 534 525 535 526 # Add current entry. … … 551 542 self.times["chapter"]['start'].append(start_time) 552 543 self.last_type_def = ')' 553 mark = self.text.create_mark(start_time,self.text.get_end_iter(),True) 554 mark.set_visible(self.visible_marks) 544 545 if last_time[track] != start_time: #Avoid duplicate mark if same mark time ended last clip. 546 if track.startswith('subtitle'): 547 mark = self.text.create_mark('subtitle-' + start_time,self.text.get_end_iter(),True) 548 else: 549 mark = self.text.create_mark('chapter-' + start_time,self.text.get_end_iter(),True) 550 mark.set_visible(self.visible_marks) 555 551 556 552 #Insert and tag speaker string. (will be invisible) 557 self.text.insert_with_tags(self.text.get_end_iter(),title,self.tag_speaker) 558 553 self.text.insert_with_tags(self.text.get_end_iter(),title,self.tag_speaker) 559 554 #Insert text. 560 555 self.text.insert_with_tags(self.text.get_end_iter(),desc.strip(),self.tag_clip) 556 if end_time != None: 557 if track.startswith('subtitle'): 558 self.times["subtitle"]['end'].append(end_time) 559 mark = self.text.create_mark("subtitle-" + end_time,self.text.get_end_iter(),True) 560 else: 561 self.times["chapter"]['end'].append(end_time) 562 mark = self.text.create_mark("chapter-" + end_time,self.text.get_end_iter(),True) 563 mark.set_visible(self.visible_marks) 564 ended_previous = True 565 else: 566 ended_previous = False 561 567 last_time[track] = end_time 562 568 elif not ended_previous: 563 569 if track.startswith('subtitle'): 564 570 self.times["subtitle"]['end'].append(start_time) 571 mark = self.text.create_mark("subtitle-" + start_time,self.text.get_end_iter(),True) 565 572 else: 566 573 self.times["chapter"]['end'].append(start_time) 567 mark = self.text.create_mark(start_time,self.text.get_end_iter(),True)574 mark = self.text.create_mark("chapter-" + start_time,self.text.get_end_iter(),True) 568 575 mark.set_visible(self.visible_marks) 569 576 x += 1 570 577 self.progress.props.fraction = x/total_clips 571 578 self.pipeline.get_state() # Let pipeline catch up to script. 572 if end_time!="": 573 self.seek(end_time) 574 self.start_time.set_text(end_time) 575 else: 576 self.seek(start_time) 577 self.start_time.set_text(start_time) 579 self.pipeline_duration = self.pipeline.query_duration(gst.FORMAT_TIME)[0]/gst.MSECOND 580 if (end_time!="" and to_ms(end_time) < self.pipeline_duration-3*60*1000) or to_ms(start_time) < self.pipeline_duration-4*60*1000: #If last clip is towards end, skip and play from the beginning. If not go to the last clip. 581 if end_time!="": 582 self.seek(end_time) 583 self.start_time.set_text(end_time) 584 else: 585 self.seek(start_time) 586 self.start_time.set_text(start_time) 578 587 f.close() 579 588 return … … 592 601 start_time = format_time(int(node.getAttribute('clipBegin'))/29.97*1000) 593 602 end_time = format_time(int(node.getAttribute('clipEnd'))/29.97*1000) 594 603 595 604 # If a gap exists between previous end and current start add a mark to end previous clip. 596 if last_time != start_time and last_time!=None:597 self.times["chapter"]["end"].append(last_time)598 mark = self.text.create_mark( last_time,self.text.get_end_iter(),True)605 self.times["chapter"]["start"].append(start_time) 606 if last_time!=start_time: 607 mark = self.text.create_mark("chapter-" + start_time,self.text.get_end_iter(),True) 599 608 mark.set_visible(self.visible_marks) 600 ended_previous = True601 else:602 ended_previous = False603 609 604 610 # Add current entry. 605 611 self.text.insert(self.text.get_end_iter(),"\n\nChapter " + str(x+1) + ": ") 606 self.times["chapter"]["start"].append(start_time)607 612 self.last_type_def = ')' 608 mark = self.text.create_mark(start_time,self.text.get_end_iter(),True) 613 self.text.insert_with_tags(self.text.get_end_iter(),'/placeholder/',self.tag_clip) 614 615 #Add end mark. 616 self.times["chapter"]["end"].append(end_time) 617 mark = self.text.create_mark("chapter-" + end_time,self.text.get_end_iter(),True) 609 618 mark.set_visible(self.visible_marks) 610 611 #Insert text.612 self.text.insert_with_tags(self.text.get_end_iter(),'/placeholder/',self.tag_clip)613 619 last_time = end_time 614 620 x += 1 … … 643 649 self.accels.connect_group(gtk.gdk.keyval_from_name("p"),gtk.gdk.CONTROL_MASK,gtk.ACCEL_VISIBLE,self.just_play) # ctrl - p 644 650 self.accels.connect_group(gtk.gdk.keyval_from_name("i"),gtk.gdk.CONTROL_MASK,gtk.ACCEL_VISIBLE,self.import_file) # ctrl - i 651 self.accels.connect_group(gtk.gdk.keyval_from_name("Return"),0,gtk.ACCEL_VISIBLE,self.enter_activated) # enter 645 652 self.window.add_accel_group(self.accels) 646 653 … … 680 687 681 688 self.clip_text = gtk.Entry() 682 self.clip_text.connect("activate",self.enter_activated)683 689 self.hbox.pack_start(self.start_time, False, False) 684 690 self.hbox.pack_start(self.end_time, False, False) … … 726 732 self.pipeline_duration = 1000 #Something non-zero to avoid non-zero errors. 727 733 self.mode = 'transcribe' 734 self.show_keystrokes = False 735 self.debug = False 728 736 729 737 self.window.show_all()