Changeset 2447

Show
Ignore:
Timestamp:
2006-09-06 21:44:23 (2 years ago)
Author:
scott
Message:

Added progress bar and solved import inefficiency in the process. The progress bar stays though. Introduced a bug causing the seek at the end of the import to fail.

Files:

Legend:

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

    r2418 r2447  
    109109      self.playbin.props.uri = "file://" + self.current_file 
    110110      self.pipeline.set_state(gst.STATE_PAUSED); 
     111      self.pipeline_duration = 10 
    111112    dialog.destroy() 
    112113     
     
    122123     
    123124    response = dialog.run() 
     125    filename = None 
    124126    if response == gtk.RESPONSE_OK: 
     127      filename = dialog.get_filename() 
     128    dialog.destroy() 
     129    if filename != None: 
     130      self.progress.props.text = "Importing " + filename 
    125131      self.text.delete(self.text.get_start_iter(),self.text.get_end_iter()) 
    126132      self.times = {"chapter":[],"subtitle":[]} 
    127133      self.start_time.set_text("xx:xx:xx.xxx") 
    128134      self.end_time.set_text("xx:xx:xx.xxx") 
    129       self.import_cmml(dialog.get_filename()) 
    130     dialog.destroy() 
     135      self.import_cmml(filename) 
    131136     
    132137  def save_file(self, accel_group, window, keyval, modifier): 
     
    191196      self.pipeline.set_state(gst.STATE_PAUSED); 
    192197      self.textView.props.can_focus = True 
    193       self.end_time.set_text(format_time(self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND)) 
     198      current_time = self.pipeline.query_position(gst.FORMAT_TIME)[0]/gst.MSECOND 
     199      self.end_time.set_text(format_time(current_time)) 
     200      self.progress.props.text = self.current_file + ": Paused." 
     201      if self.pipeline_duration==10: 
     202        self.pipeline_duration = self.pipeline.query_duration(gst.FORMAT_TIME)[0]/gst.MSECOND 
     203      self.progress.props.fraction = float(current_time)/self.pipeline_duration 
    194204    elif (self.pipeline.get_state()[1] == gst.STATE_PAUSED): 
    195205      text = self.clip_text.get_text() 
     
    236246      self.textView.props.can_focus = False 
    237247      self.pipeline.set_state(gst.STATE_PLAYING); 
     248      self.progress.props.text = self.current_file + ": Playing." 
    238249    return 
    239250     
    240251  def scroll_to_bottom(self, widget): 
    241     self.sw.props.vadjustment.value = self.sw.props.vadjustment.upper 
     252    self.textView.scroll_to_mark(self.text.get_mark("insert"),0.1) 
    242253     
    243254  def export_buffer(self): 
     
    350361     
    351362  def import_cmml(self, filename): 
    352     import xml.dom.pulldom 
     363    import xml.dom.minidom 
    353364    f = open(filename,"r") 
    354     doc = xml.dom.pulldom.parse(f) 
     365    doc = xml.dom.minidom.parse(f) 
    355366    last_speaker = '' 
    356367    last_time = {} 
    357368    last_title = '' 
    358     print "Please be patient, the textbuffer slows on large/long files." 
    359     for event, node in doc: 
    360       if event=='START_ELEMENT' and node.nodeName=='import': 
    361         doc.expandNode(node) 
    362         self.pipeline.set_state(gst.STATE_NULL) 
    363         self.current_file = node.getAttribute('src') 
    364         self.playbin.props.uri = "file://" + self.current_file 
    365         self.pipeline.set_state(gst.STATE_PAUSED); 
    366       elif event=='START_ELEMENT' and node.nodeName=='clip': 
    367         doc.expandNode(node) 
    368         # load data 
    369         start_time = node.getAttribute('start') 
    370         end_time = node.getAttribute('end') 
    371         track = node.getAttribute('track') 
    372         title = node.getAttribute('title') 
     369    file_imports = doc.getElementsByTagName('import') 
     370    for node in file_imports: 
     371      self.pipeline.set_state(gst.STATE_NULL) 
     372      self.current_file = node.getAttribute('src') 
     373      self.playbin.props.uri = "file://" + self.current_file 
     374      self.pipeline.set_state(gst.STATE_PAUSED); 
     375      self.pipeline_duration = 10 
     376    clips = doc.getElementsByTagName('clip') 
     377    self.progress.props.fraction = 0 
     378    total_clips = len(clips) 
     379    x=0 
     380    for node in clips: 
     381      # load data 
     382      start_time = node.getAttribute('start') 
     383      end_time = node.getAttribute('end') 
     384      track = node.getAttribute('track') 
     385      title = node.getAttribute('title') 
     386       
     387      #don't force child nodes :-) 
     388      if node.getElementsByTagName('desc')[0].firstChild != None: 
     389        desc = node.getElementsByTagName('desc')[0].firstChild.nodeValue 
     390       
     391      #Make sure last_time has a record for the track. 
     392      if not last_time.has_key(track): 
     393        last_time[track]=None 
    373394         
    374         #don't force child nodes :-) 
    375         if node.getElementsByTagName('desc')[0].firstChild != None: 
    376           desc = node.getElementsByTagName('desc')[0].firstChild.nodeValue 
     395      # If a gap exists between previous end and current start add a mark to end previous clip. 
     396      if last_time[track] != start_time and last_time[track]!=None: 
     397        if track.startswith('subtitle'):  #FIXME not nice with multiple subtitle tracks. 
     398          self.times["subtitle"].append(last_time[track]) 
     399        else: 
     400          self.times["chapter"].append(last_time[track]) 
     401        mark = self.text.create_mark(last_time[track],self.text.get_end_iter(),True) 
     402        mark.set_visible(self.visible_marks) 
     403        ended_previous = True 
     404      else: 
     405        ended_previous = False 
    377406         
    378         #Make sure last_time has a record for the track. 
    379         if not last_time.has_key(track): 
    380           last_time[track]=None 
     407      # Add current entry. 
     408      if desc != '': 
     409        if track.startswith('subtitle') and not title==last_speaker: 
     410          self.text.insert(self.text.get_end_iter(),"\n\n"+title+":\n") 
     411          last_speaker = title 
     412        elif track.startswith('subtitle'): 
     413          #Handles spaces between clips. FIXME 
     414          self.text.insert(self.text.get_end_iter()," ") 
     415        else: 
     416          self.text.insert(self.text.get_end_iter(),"\n\nChapter " + str(len(self.times["chapter"])+1) + ": ") 
    381417           
    382         # If a gap exists between previous end and current start add a mark to end previous clip. 
    383         if last_time[track] != start_time and last_time[track]!=None: 
    384           if track.startswith('subtitle'):  #FIXME not nice with multiple subtitle tracks. 
    385             self.times["subtitle"].append(last_time[track]) 
    386           else: 
    387             self.times["chapter"].append(last_time[track]) 
    388           mark = self.text.create_mark(last_time[track],self.text.get_end_iter(),True) 
    389           mark.set_visible(self.visible_marks) 
    390           ended_previous = True 
    391         else: 
    392           ended_previous = False 
    393            
    394         # Add current entry. 
    395         if desc != '': 
    396           if track.startswith('subtitle') and not title==last_speaker: 
    397             self.text.insert(self.text.get_end_iter(),"\n\n"+title+":\n") 
    398             last_speaker = title 
    399           elif track.startswith('subtitle'): 
    400             #Handles spaces between clips. FIXME 
    401             self.text.insert(self.text.get_end_iter()," ") 
    402           else: 
    403             self.text.insert(self.text.get_end_iter(),"\n\nChapter " + str(len(self.times["chapter"])+1) + ": ") 
    404              
    405           #Create start mark. 
    406           if track.startswith('subtitle'):  
    407             self.times["subtitle"].append(start_time) 
    408             self.last_type_def = title + ":" 
    409           else: 
    410             self.times["chapter"].append(start_time) 
    411             self.last_type_def = ')' 
    412           mark = self.text.create_mark(start_time,self.text.get_end_iter(),True) 
    413           mark.set_visible(self.visible_marks) 
    414            
    415           #Insert and tag speaker string.  (will be invisible) 
    416           self.text.insert_with_tags(self.text.get_end_iter(),title,self.tag_speaker) 
    417            
    418           #Insert text. 
    419           self.text.insert(self.text.get_end_iter(),desc.strip()) 
    420           #Change mark gravity. 
    421           ##Cannot change mark gravity?  Whats with that? FIXME 
    422           #Underline contents. 
    423           clip_iter = self.text.get_iter_at_mark(mark) 
    424           if not clip_iter.starts_word(): 
    425             clip_iter.forward_word_end() 
    426             clip_iter.backward_word_start() 
    427           self.text.apply_tag(self.tag_clip,clip_iter,self.text.get_end_iter()) 
    428           last_time[track] = end_time 
    429         elif not ended_previous: 
    430           if track.startswith('subtitle'):  
    431             self.times["subtitle"].append(start_time) 
    432           else: 
    433             self.times["chapter"].append(start_time) 
    434           mark = self.text.create_mark(start_time,self.text.get_end_iter(),True) 
    435           mark.set_visible(self.visible_marks) 
     418        #Create start mark. 
     419        if track.startswith('subtitle'):  
     420          self.times["subtitle"].append(start_time) 
     421          self.last_type_def = title + ":" 
     422        else: 
     423          self.times["chapter"].append(start_time) 
     424          self.last_type_def = ')' 
     425        mark = self.text.create_mark(start_time,self.text.get_end_iter(),True) 
     426        mark.set_visible(self.visible_marks) 
     427         
     428        #Insert and tag speaker string.  (will be invisible) 
     429        self.text.insert_with_tags(self.text.get_end_iter(),title,self.tag_speaker) 
     430         
     431        #Insert text. 
     432        self.text.insert(self.text.get_end_iter(),desc.strip()) 
     433        #Change mark gravity. 
     434        ##Cannot change mark gravity?  Whats with that? FIXME 
     435        #Underline contents. 
     436        clip_iter = self.text.get_iter_at_mark(mark) 
     437        if not clip_iter.starts_word(): 
     438          clip_iter.forward_word_end() 
     439          clip_iter.backward_word_start() 
     440        self.text.apply_tag(self.tag_clip,clip_iter,self.text.get_end_iter()) 
     441        last_time[track] = end_time 
     442      elif not ended_previous: 
     443        if track.startswith('subtitle'):  
     444          self.times["subtitle"].append(start_time) 
     445        else: 
     446          self.times["chapter"].append(start_time) 
     447        mark = self.text.create_mark(start_time,self.text.get_end_iter(),True) 
     448        mark.set_visible(self.visible_marks) 
     449      x += 1 
     450      self.progress.props.fraction = x/total_clips 
    436451    if end_time!="": 
    437452      self.pipeline.seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, gst.SEEK_TYPE_SET, gst.MSECOND*to_ms(end_time), gst.SEEK_TYPE_NONE, 0) 
     
    441456      self.start_time.set_text(start_time) 
    442457    f.close() 
    443     print self.last_type_def 
    444     return 
    445          
     458    return        
    446459 
    447460  def bus_call(self, bus, message): 
     
    498511    self.end_time.set_max_length(12) 
    499512    self.end_time.set_size_request(92,-1) 
    500     self.end_time.props.can_focus = False  
     513    self.end_time.props.can_focus = False 
     514     
     515    self.progress = gtk.ProgressBar() 
     516    self.progress.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT) 
     517    self.progress.set_size_request(280, 20) 
     518    self.progress.props.text = "Nothing loaded." 
     519    self.progress.set_ellipsize(pango.ELLIPSIZE_END) 
    501520     
    502521    self.clip_text = gtk.Entry() 
     
    508527    self.vbox.pack_start(self.sw,True,True) 
    509528    self.vbox.pack_start(self.hbox,False,False) 
     529    self.vbox.pack_start(self.progress,False,False) 
    510530    self.vbox.set_spacing(6) 
    511531    self.window.add(self.vbox) 
     
    544564    self.current_track = "subtitle" 
    545565    self.visible_marks = True 
     566    self.pipeline_duration = 1000 #Something non-zero to avoid non-zero errors. 
    546567     
    547568    self.window.show_all()