Advertisement
agf

mp4ize.windows.patch

agf
Jul 24th, 2011
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.36 KB | None | 0 0
  1. --- mp4ize_orig.py  2011-07-24 07:08:42 -0400
  2. +++ mp4ize.py   2011-07-24 09:16:36 -0400
  3. @@ -110,7 +110,6 @@
  4.  
  5.  import os, os.path, sys, socket, time, argparse, shlex, email, mailbox
  6.  from subprocess import *
  7. -import fcntl, select
  8.  
  9.  ##############################################################################
  10.  
  11. @@ -234,7 +233,7 @@
  12.          pad = int((user_height - float(height)) / 2.0)
  13.          pad -= (pad % 2)
  14.          pad = abs(pad)
  15. -        padarg1, padarg2 = "padtop", "padbottom"
  16. +        padstr = '-vf "pad=%d:%d:%d:%d:black"'%(width,height + 2 * pad, 0, pad)
  17.          
  18.          #print aspect, user_width, user_height, width, height, pad
  19.          #print ''
  20. @@ -246,10 +245,10 @@
  21.  #                 sys.exit(1)            
  22.              os.remove(outfile)
  23.              
  24. -        cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} -{6} {7}' + \
  25. -            ' -{8} {7} -ab {9} -b {10} "{11}"').format(
  26. +        cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} {6}' + \
  27. +            ' -ab {7} -b {8} "{9}"').format(
  28.              DEFAULT_BIN, f, DEFAULT_ARGS, DEFAULT_BUFSIZE,
  29. -            width, height, padarg1, pad, padarg2,
  30. +            width, height, padstr,
  31.              options.audio, options.video, outfile)
  32.          print cmd
  33.          print ''
  34. @@ -262,10 +261,10 @@
  35.                  # lets try again while forcing an aspect ratio
  36.                  print 'Invalid pixel aspect ratio, ' + \
  37.                      'running again with source ratio'
  38. -                cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} -{6} {7}' + \
  39. -                    ' -{8} {7} -aspect {12} -ab {9} -b {10} "{11}"').format(
  40. +                cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} {6}' + \
  41. +                    ' -aspect {10} -ab {7} -b {8} "{9}"').format(
  42.                       DEFAULT_BIN, f, DEFAULT_ARGS, DEFAULT_BUFSIZE,
  43. -                     width, height, padarg1, pad, padarg2,
  44. +                     width, height, padstr,
  45.                       options.audio, options.video, outfile, aspect)
  46.                  print cmd
  47.                  print ''
  48. @@ -282,7 +281,8 @@
  49.                  print 'Error: Video encoding failed'
  50.  
  51.          duration = float(duration)
  52. -        time_f = float(time_f)
  53. +        time_f = time_f.split(':')
  54. +        time_f = float(time_f[0]) * 60 * 60 + float(time_f[1]) * 60 + float(time_f[2])
  55.          print "source duration: %s" % duration
  56.          print "destination duration: %s" % time_f
  57.          if ((time_f <= duration * 1.01) and (time_f >= duration * 0.99)):
  58. @@ -296,33 +296,62 @@
  59.      result = ''
  60.      time_f = 0
  61.      args = shlex.split(cmd)
  62. -    p = Popen (args, stdout=PIPE, stderr=PIPE, shell=False)
  63.  
  64. -    fcntl.fcntl(
  65. -        p.stderr.fileno(),
  66. -        fcntl.F_SETFL,
  67. -        fcntl.fcntl(p.stderr.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK,
  68. -    )
  69. +    if len(args) > 4:
  70. +        from threading  import Thread
  71. +        from Queue import Queue, Empty
  72. +        from functools import partial
  73. +        def enqueue_output(err, queue):
  74. +            pread = partial(err.read, 100)
  75. +            extra = ''
  76. +            for blanks in range(5):
  77. +                for line in iter(pread, ''):
  78. +                    if '\r' in line:
  79. +                        parts = line.split('\r')
  80. +                        line, extra_new = parts[0], '\r' + '\r'.join(parts[1:])
  81. +                    else:
  82. +                        extra_new = ''
  83. +                    q.put(extra + line)
  84. +                    extra = extra_new
  85. +                time.sleep(.2)
  86. +            err.close()
  87. +
  88. +    p = Popen (args, stderr=PIPE)
  89. +
  90. +    if len(args) > 4:
  91. +        q = Queue()
  92. +        t = Thread(target=enqueue_output, args=(p.stderr, q))
  93. +        t.daemon = True # thread dies with the program
  94. +        t.start()
  95. +
  96.      while True:
  97.          # wait for I/O completion
  98. -        readx = select.select([p.stderr.fileno()], [], [])[0]
  99. -        if readx:
  100. -            chunk = p.stderr.read()
  101. -            if chunk == '':
  102. -                break
  103. +        result = ''
  104. +        if len(args) > 4:
  105. +            try:
  106. +                chunk = q.get_nowait() # or q.get(timeout=.1)
  107. +                result += chunk
  108. +            except Empty: chunk = ''
  109. +        else:
  110. +            stdout, chunk = p.communicate()
  111.              result = chunk                
  112. +        if p.returncode is not None or (len(args) > 4 and not t.isAlive()):
  113. +            break
  114. +        if chunk:
  115.              tmp_time_f = verify_output(chunk)
  116.              if tmp_time_f:
  117.                  time_f = tmp_time_f
  118. +                timef = tmp_time_f.split(':')
  119. +                timef = float(timef[0]) * 60 * 60 + float(timef[1]) * 60 + float(timef[2])
  120.                  if duration:
  121. -                    percent = (float(time_f)/float(duration))*100
  122. -                    sys.stderr.write('  %2d%% Completed [position %ss] \r'
  123. -                         % (percent, time_f))                    
  124. +                    percent = (timef/float(duration))*100
  125. +                    sys.stderr.write('  %2d%% Completed [position %ss] \r' % (percent, time_f))
  126.                  else:
  127.                      sys.stderr.write(' time=%s \r' % time_f)
  128. +        sys.stdout.flush()
  129. +        sys.stderr.flush()
  130.          time.sleep(.1)
  131. -    p.wait()
  132. -    return p.returncode, result
  133. +    return p.returncode, (result.split('\r')[-1] if len(args) > 4 else result)
  134.      
  135.  def verify_output(output):
  136.      time_f = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement