Changeset 2493

Show
Ignore:
Timestamp:
2006-09-17 13:52:53 (2 years ago)
Author:
scott
Message:

Information storage technique changed. Created better forward/back plus smooth transition between transcribing and reviewing transcriptions.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • scripts/trunk/annotool.py

    r2491 r2493  
    8989      
    9090  def open_file(self, accel_group, window, keyval, modifier): 
    91     print "Ctrl - O" 
     91    if self.show_keystrokes: print "Ctrl - O" 
    9292    dialog = gtk.FileChooserDialog("Open",self.window,gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) 
    9393    dialog.set_default_response(gtk.RESPONSE_OK) 
     
    114114     
    115115  def import_file(self, accel_group, window, keyval, modifier): 
    116     print "Ctrl - I" 
     116    if self.show_keystrokes: print "Ctrl - I" 
    117117    dialog = gtk.FileChooserDialog("Import",self.window,gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) 
    118118    dialog.set_default_response(gtk.RESPONSE_OK) 
     
    147147     
    148148  def save_file(self, accel_group, window, keyval, modifier): 
    149     print "Ctrl - S" 
     149    if self.show_keystrokes: print "Ctrl - S" 
    150150    dialog = gtk.FileChooserDialog("Save",self.window,gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK)) 
    151151    dialog.set_default_response(gtk.RESPONSE_OK) 
     
    184184  # Text overlay functions. 
    185185  def toggle_track(self, accel_group, window, keyval, modifier): 
    186     print "Ctrl - T" 
     186    if self.show_keystrokes: print "Ctrl - T" 
    187187    keys = self.times.keys() 
    188188    new_track = keys.index(self.current_track)+1 
     
    192192  # Navigation functions. 
    193193  def restart_clip(self, accel_group, window, keyval, modifier): 
    194     print "Ctrl - R" 
     194    if self.show_keystrokes: print "Ctrl - R" 
    195195    self.seek(self.start_time.get_text()) 
    196196     
    197197  def current_clip(self,current_time): 
    198       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 
     198    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 
    202202      
    203203  def previous_clip(self, accel_group, window, keyval, modifier): 
    204     print "Ctrl - B" 
     204    if self.show_keystrokes: print "Ctrl - B" 
    205205    clip_now = self.current_clip(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND) 
    206206    if clip_now > 0: 
     
    209209      #Highlight chapter entry. 
    210210      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]) 
    212212      start_iter = self.text.get_iter_at_mark(mark) 
    213213      if not start_iter.begins_tag(self.tag_clip): 
     
    231231     
    232232  def next_clip(self, accel_group, window, keyval, modifier): 
    233     print "Ctrl - N" 
     233    if self.show_keystrokes: print "Ctrl - N" 
    234234    #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]) 
    245246      #Highlight chapter entry. 
    246247      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]) 
    248249      start_iter = self.text.get_iter_at_mark(mark) 
    249250      if not start_iter.begins_tag(self.tag_clip): 
     
    256257      self.textView.scroll_to_mark(mark,0.1) 
    257258       
    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)]) 
    262259      if self.pipeline_duration==10: 
    263260        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' 
    267266   
    268267  def just_play(self, accel_group, window, keyval, modifier): 
    269     print "Ctrl - P" 
     268    if self.show_keystrokes: print "Ctrl - P" 
    270269    if (self.pipeline.get_state()[1] != gst.STATE_PLAYING): 
    271270      self.pipeline.set_state(gst.STATE_PLAYING); 
     271      self.progress.props.text = self.current_file + ": Playing." 
    272272       
    273273  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) 
    276279    else: 
    277280      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)) 
    278281       
    279   def enter_activated(self, widget, data=None): 
     282  def enter_activated(self, accel_group, window, keyval, modifier): 
    280283    #Handles clip to text buffer, formatting and pipeline control. 
    281     print "Enter" 
     284    if self.show_keystrokes: print "Enter" 
    282285    if (self.pipeline.get_state()[1] == gst.STATE_PLAYING): 
    283286      self.pipeline.set_state(gst.STATE_PAUSED); 
    284       self.textView.props.can_focus = True 
    285287      current_time = self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND 
    286288      self.end_time.set_text(format_time(current_time)) 
     
    289291        self.pipeline_duration = self.pipeline.query_duration(gst.FORMAT_TIME)[0]/gst.MSECOND 
    290292      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': 
    292294      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': 
    294296        #Chapter deocration. 
    295297        self.text.insert(self.text.get_end_iter(),"\n\nChapter " + str(len(self.times["chapter"])+1) + ": ") 
    296298        #Mark handling. 
    297299        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) 
    299310        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': 
    304312        # If using the last type def make sure doesn't have ';' instead of intended ':' 
    305313        if text.find(":")==-1 and text.find(";")!=-1: 
     
    321329        #Mark handling. 
    322330        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) 
    325334        #Insert and tag speaker string.  (will be invisible) 
    326335        self.text.insert_with_tags(self.text.get_end_iter(),self.last_type_def.strip(":"),self.tag_speaker) 
    327336        #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) 
    339342      self.clip_text.set_text("") 
    340343      self.start_time.set_text(format_time(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND)) 
    341344      self.end_time.set_text("xx:xx:xx.xxx") 
    342       self.textView.props.can_focus = False 
    343345      self.scroll_to_bottom() 
    344346      self.pipeline.set_state(gst.STATE_PLAYING); 
     
    347349      self.start_time.set_text(format_time(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND)) 
    348350      self.end_time.set_text("xx:xx:xx.xxx") 
    349       self.textView.props.can_focus = False 
    350351      self.pipeline.set_state(gst.STATE_PLAYING); 
    351352      self.progress.props.text = self.current_file + ": Playing." 
     
    365366      times.sort(lambda x,y: cmp(to_ms(x), to_ms(y))) 
    366367      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)) 
    368369         
    369370        #Checks that the next mark is not at the same location. 
    370371        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])) 
    372373        except: 
    373374          count = 0 
     
    377378          tmp_iter = iter.copy() 
    378379          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]) 
    380381          con = self.text.get_iter_at_mark(tmp_mark).compare(tmp_iter) 
    381382          if con>0 or (con==0 and tmp_iter.ends_tag(self.tag_clip)): 
     
    406407          last_iter.forward_to_tag_toggle(None) 
    407408          desc = self.text.get_text(iter,last_iter) 
     409          if desc != "/placeholder/": 
     410            desc = desc.strip("/") 
    408411          if len(times)>x: 
    409412            clip = [time,times[x],desc] 
     
    519522      #Make sure last_time has a record for the track. 
    520523      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 
    534525         
    535526      # Add current entry. 
     
    551542          self.times["chapter"]['start'].append(start_time) 
    552543          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) 
    555551         
    556552        #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)     
    559554        #Insert text. 
    560555        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 
    561567        last_time[track] = end_time 
    562568      elif not ended_previous: 
    563569        if track.startswith('subtitle'):  
    564570          self.times["subtitle"]['end'].append(start_time) 
     571          mark = self.text.create_mark("subtitle-" + start_time,self.text.get_end_iter(),True) 
    565572        else: 
    566573          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) 
    568575        mark.set_visible(self.visible_marks) 
    569576      x += 1 
    570577      self.progress.props.fraction = x/total_clips 
    571578    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) 
    578587    f.close() 
    579588    return  
     
    592601      start_time = format_time(int(node.getAttribute('clipBegin'))/29.97*1000) 
    593602      end_time = format_time(int(node.getAttribute('clipEnd'))/29.97*1000) 
    594          
     603       
    595604      # 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) 
    599608        mark.set_visible(self.visible_marks) 
    600         ended_previous = True 
    601       else: 
    602         ended_previous = False 
    603609         
    604610      # Add current entry. 
    605611      self.text.insert(self.text.get_end_iter(),"\n\nChapter " + str(x+1) + ": ") 
    606       self.times["chapter"]["start"].append(start_time) 
    607612      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) 
    609618      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) 
    613619      last_time = end_time 
    614620      x += 1 
     
    643649    self.accels.connect_group(gtk.gdk.keyval_from_name("p"),gtk.gdk.CONTROL_MASK,gtk.ACCEL_VISIBLE,self.just_play) # ctrl - p 
    644650    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 
    645652    self.window.add_accel_group(self.accels) 
    646653     
     
    680687     
    681688    self.clip_text = gtk.Entry() 
    682     self.clip_text.connect("activate",self.enter_activated) 
    683689    self.hbox.pack_start(self.start_time, False, False) 
    684690    self.hbox.pack_start(self.end_time, False, False) 
     
    726732    self.pipeline_duration = 1000 #Something non-zero to avoid non-zero errors. 
    727733    self.mode = 'transcribe' 
     734    self.show_keystrokes = False 
     735    self.debug = False 
    728736     
    729737    self.window.show_all()