Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- mp4ize_orig.py 2011-07-24 07:08:42 -0400
- +++ mp4ize.py 2011-07-24 09:16:36 -0400
- @@ -110,7 +110,6 @@
- import os, os.path, sys, socket, time, argparse, shlex, email, mailbox
- from subprocess import *
- -import fcntl, select
- ##############################################################################
- @@ -234,7 +233,7 @@
- pad = int((user_height - float(height)) / 2.0)
- pad -= (pad % 2)
- pad = abs(pad)
- - padarg1, padarg2 = "padtop", "padbottom"
- + padstr = '-vf "pad=%d:%d:%d:%d:black"'%(width,height + 2 * pad, 0, pad)
- #print aspect, user_width, user_height, width, height, pad
- #print ''
- @@ -246,10 +245,10 @@
- # sys.exit(1)
- os.remove(outfile)
- - cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} -{6} {7}' + \
- - ' -{8} {7} -ab {9} -b {10} "{11}"').format(
- + cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} {6}' + \
- + ' -ab {7} -b {8} "{9}"').format(
- DEFAULT_BIN, f, DEFAULT_ARGS, DEFAULT_BUFSIZE,
- - width, height, padarg1, pad, padarg2,
- + width, height, padstr,
- options.audio, options.video, outfile)
- print cmd
- print ''
- @@ -262,10 +261,10 @@
- # lets try again while forcing an aspect ratio
- print 'Invalid pixel aspect ratio, ' + \
- 'running again with source ratio'
- - cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} -{6} {7}' + \
- - ' -{8} {7} -aspect {12} -ab {9} -b {10} "{11}"').format(
- + cmd = ('{0} -i "{1}" {2} -bufsize {3} -s {4}x{5} {6}' + \
- + ' -aspect {10} -ab {7} -b {8} "{9}"').format(
- DEFAULT_BIN, f, DEFAULT_ARGS, DEFAULT_BUFSIZE,
- - width, height, padarg1, pad, padarg2,
- + width, height, padstr,
- options.audio, options.video, outfile, aspect)
- print cmd
- print ''
- @@ -282,7 +281,8 @@
- print 'Error: Video encoding failed'
- duration = float(duration)
- - time_f = float(time_f)
- + time_f = time_f.split(':')
- + time_f = float(time_f[0]) * 60 * 60 + float(time_f[1]) * 60 + float(time_f[2])
- print "source duration: %s" % duration
- print "destination duration: %s" % time_f
- if ((time_f <= duration * 1.01) and (time_f >= duration * 0.99)):
- @@ -296,33 +296,62 @@
- result = ''
- time_f = 0
- args = shlex.split(cmd)
- - p = Popen (args, stdout=PIPE, stderr=PIPE, shell=False)
- - fcntl.fcntl(
- - p.stderr.fileno(),
- - fcntl.F_SETFL,
- - fcntl.fcntl(p.stderr.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK,
- - )
- + if len(args) > 4:
- + from threading import Thread
- + from Queue import Queue, Empty
- + from functools import partial
- + def enqueue_output(err, queue):
- + pread = partial(err.read, 100)
- + extra = ''
- + for blanks in range(5):
- + for line in iter(pread, ''):
- + if '\r' in line:
- + parts = line.split('\r')
- + line, extra_new = parts[0], '\r' + '\r'.join(parts[1:])
- + else:
- + extra_new = ''
- + q.put(extra + line)
- + extra = extra_new
- + time.sleep(.2)
- + err.close()
- +
- + p = Popen (args, stderr=PIPE)
- +
- + if len(args) > 4:
- + q = Queue()
- + t = Thread(target=enqueue_output, args=(p.stderr, q))
- + t.daemon = True # thread dies with the program
- + t.start()
- +
- while True:
- # wait for I/O completion
- - readx = select.select([p.stderr.fileno()], [], [])[0]
- - if readx:
- - chunk = p.stderr.read()
- - if chunk == '':
- - break
- + result = ''
- + if len(args) > 4:
- + try:
- + chunk = q.get_nowait() # or q.get(timeout=.1)
- + result += chunk
- + except Empty: chunk = ''
- + else:
- + stdout, chunk = p.communicate()
- result = chunk
- + if p.returncode is not None or (len(args) > 4 and not t.isAlive()):
- + break
- + if chunk:
- tmp_time_f = verify_output(chunk)
- if tmp_time_f:
- time_f = tmp_time_f
- + timef = tmp_time_f.split(':')
- + timef = float(timef[0]) * 60 * 60 + float(timef[1]) * 60 + float(timef[2])
- if duration:
- - percent = (float(time_f)/float(duration))*100
- - sys.stderr.write(' %2d%% Completed [position %ss] \r'
- - % (percent, time_f))
- + percent = (timef/float(duration))*100
- + sys.stderr.write(' %2d%% Completed [position %ss] \r' % (percent, time_f))
- else:
- sys.stderr.write(' time=%s \r' % time_f)
- + sys.stdout.flush()
- + sys.stderr.flush()
- time.sleep(.1)
- - p.wait()
- - return p.returncode, result
- + return p.returncode, (result.split('\r')[-1] if len(args) > 4 else result)
- def verify_output(output):
- time_f = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement