gremlin/gremlin.in: Add function for linking a sequence of elements.
[autoys] / gremlin / gremlin.in
index 6e88259..79c0ea9 100644 (file)
@@ -41,11 +41,11 @@ import shutil as SH
 import optparse as OP
 import threading as TH
 import shlex as L
-from math import sqrt
+from math import sqrt, ceil
 from contextlib import contextmanager
 
 ## eyeD3 tag fettling.
-import eyeD3 as E3
+import eyed3 as E3
 
 ## Gstreamer.  It picks up command-line arguments -- most notably `--help' --
 ## and processes them itself.  Of course, its help is completely wrong.  This
@@ -247,7 +247,7 @@ class ProgressEyecandy (object):
     ## Work out -- well, guess -- the time remaining.
     if cur:
       t = T.time()
-      eta = me._fmt_time((t - me._start)*(max - cur)/cur)
+      eta = me._fmt_time(ceil((t - me._start)*(max - cur)/cur))
     else:
       eta = '???'
 
@@ -411,6 +411,8 @@ class FormatParser (P.ParserElement):
   named format and its superclasses.
   """
 
+  name = 'format-spec'
+
   ## We cache the parser elements we generate to avoid enormous consing.
   CACHE = {}
 
@@ -712,6 +714,10 @@ def make_element(factory, name = None, **props):
   elt.set_properties(**props)
   return elt
 
+def link_elements(elts):
+  "Link the elements ELTS together, in order."
+  GS.element_link_many(*elts)
+
 class GStreamerProgressEyecandy (ProgressEyecandy):
   """
   Provide amusement while GStreamer is busy doing something.
@@ -825,7 +831,7 @@ class AudioIdentifier (object):
         elt.link_pads(pad.get_name(), sink, 'sink')
     dpaid = decoder.connect('pad-added', decoder_pad_arrived)
     me._pipe.add(source, decoder, sink)
-    GS.element_link_many(source, decoder)
+    link_elements([source, decoder])
 
     ## Arrange to collect tags from the pipeline's bus as they're reported.
     ## If we reuse the pipeline later, we'll want different bus-message
@@ -989,7 +995,7 @@ class AudioFormat (BaseFormat):
     elts = me.encoder_chain()
     bin = GS.Bin()
     bin.add(*elts)
-    GS.element_link_many(*elts)
+    link_elements(elts)
     bin.add_pad(GS.GhostPad('sink', elts[0].get_pad('sink')))
     bin.add_pad(GS.GhostPad('src', elts[-1].get_pad('src')))
     return bin
@@ -1026,8 +1032,8 @@ class AudioFormat (BaseFormat):
     encoder = me.encoder()
     sink = make_element('filesink', 'sink', location = new)
     pipe.add(source, decoder, convert, encoder, sink)
-    GS.element_link_many(source, decoder)
-    GS.element_link_many(convert, encoder, sink)
+    link_elements([source, decoder])
+    link_elements([convert, encoder, sink])
 
     ## Some decoders (e.g., the AC3 decoder) include channel-position
     ## indicators in their output caps.  The Vorbis encoder interferes with
@@ -1107,8 +1113,8 @@ class OggVorbisFormat (AudioFormat):
       for q, br in me.QMAP:
         if br >= me.bitrate:
           break
-        else:
-          raise ValueError, 'no suitable quality setting found'
+      else:
+        raise ValueError, 'no suitable quality setting found'
       encprops['quality'] = q/10.0
     return [make_element('vorbisenc', **encprops),
             make_element('oggmux')]
@@ -1136,13 +1142,16 @@ class MP3Format (AudioFormat):
     GStreamer produces ID3v2 tags, but not ID3v1.  This seems unnecessarily
     unkind to stupid players.
     """
-    tag = E3.Tag()
-    tag.link(path)
-    tag.setTextEncoding(E3.UTF_8_ENCODING)
-    try:
-      tag.update(E3.ID3_V1_1)
-    except (UnicodeEncodeError, E3.tag.GenreException):
-      pass
+    f = E3.load(path)
+    if f is None: return
+    t = f.tag
+    if t is None: return
+    for v in [E3.id3.ID3_V2_3, E3.id3.ID3_V1]:
+      try: f.tag.save(version = v)
+      except (UnicodeEncodeError,
+              E3.id3.GenreException,
+              E3.id3.TagException):
+        pass
 
 defformat('mp3', MP3Format)