From 0e73df4a0a1266c75f2a83b5b5262d7823d4e28e Mon Sep 17 00:00:00 2001 From: James Bunton Date: Fri, 8 Aug 2014 22:41:12 +1000 Subject: [PATCH] Tabs to spaces --- batch-run | 98 +++--- fix-pal-speedup | 28 +- hencode | 18 +- hencode-recursive | 50 +-- identify-dvd | 12 +- mirror-dvd | 10 +- py-encode | 830 +++++++++++++++++++++++----------------------- rip-audio | 16 +- rip-dvd | 16 +- trim-audio | 8 +- v4l-rip | 4 +- video-transform | 550 +++++++++++++++--------------- 12 files changed, 820 insertions(+), 820 deletions(-) diff --git a/batch-run b/batch-run index fa799df..43af531 100755 --- a/batch-run +++ b/batch-run @@ -10,70 +10,70 @@ import time def run(running_jobs, cmd): - p = subprocess.Popen(cmd, stdin=file(os.devnull, 'r')) - running_jobs.append(p) + p = subprocess.Popen(cmd, stdin=file(os.devnull, 'r')) + running_jobs.append(p) def wait_for_completion(max_jobs, running_jobs): - while len(running_jobs) >= max_jobs: - time.sleep(1) - for job in running_jobs: - if job.poll() is not None: - running_jobs.remove(job) - job.wait() + while len(running_jobs) >= max_jobs: + time.sleep(1) + for job in running_jobs: + if job.poll() is not None: + running_jobs.remove(job) + job.wait() def parse_file(fd): - def count_indent(s): - level = 0 - for ch in s: - if ch == "\t": - level += 1 - else: - break - return level + def count_indent(s): + level = 0 + for ch in s: + if ch == "\t": + level += 1 + else: + break + return level - for line in fd: - if not line.strip(): - continue # Ignore blank lines - level = count_indent(line) - line = line[level:] # Slice off the indentation - yield level, line - yield 0, None + for line in fd: + if not line.strip(): + continue # Ignore blank lines + level = count_indent(line) + line = line[level:] # Slice off the indentation + yield level, line + yield 0, None def batch_process(opts, lines): - running_jobs = [] - cmd = [] + running_jobs = [] + cmd = [] - for level, line in lines: - old_level = len(cmd) - 1 - if level <= old_level: - run(running_jobs, itertools.chain(*cmd)) - wait_for_completion(opts.jobs, running_jobs) + for level, line in lines: + old_level = len(cmd) - 1 + if level <= old_level: + run(running_jobs, itertools.chain(*cmd)) + wait_for_completion(opts.jobs, running_jobs) - # Delete all options that belong to groups that are indented more than - # this one - cmd = cmd[:level] - assert len(cmd) == level + # Delete all options that belong to groups that are indented more than + # this one + cmd = cmd[:level] + assert len(cmd) == level - if line: - cmd.append(shlex.split(line)) + if line: + cmd.append(shlex.split(line)) - # Wait for remaining jobs to finish - wait_for_completion(1, running_jobs) + # Wait for remaining jobs to finish + wait_for_completion(1, running_jobs) def parse_args(): - parser = optparse.OptionParser(usage="%prog batchfile1 [batchfile2] ...") - parser.add_option("-j", "--jobs", - action="store", dest="jobs", default=1, type="int", - help="The number of concurrent jobs to run") - opts, args = parser.parse_args(sys.argv[1:]) - opts.running_jobs = [] - return opts, args + parser = optparse.OptionParser(usage="%prog batchfile1 [batchfile2] ...") + parser.add_option("-j", "--jobs", + action="store", dest="jobs", default=1, type="int", + help="The number of concurrent jobs to run") + opts, args = parser.parse_args(sys.argv[1:]) + opts.running_jobs = [] + return opts, args def main(): - opts, args = parse_args() - for name in args: - batch_process(opts, parse_file(open(name))) + opts, args = parse_args() + for name in args: + batch_process(opts, parse_file(open(name))) if __name__ == "__main__": - main() + main() diff --git a/fix-pal-speedup b/fix-pal-speedup index 5e6f8d4..74681c1 100755 --- a/fix-pal-speedup +++ b/fix-pal-speedup @@ -6,8 +6,8 @@ # volume normalised then re-encoded as mp3. if [ -z "$1" -o -z "$2" ]; then - echo "Usage: $0 destdir infile [infile ...]" - exit 1 + echo "Usage: $0 destdir infile [infile ...]" + exit 1 fi FORCEFPS="24" @@ -17,20 +17,20 @@ destdir="$1" shift for infile in "$@"; do - outfile="$destdir/$(basename "$infile")" + outfile="$destdir/$(basename "$infile")" - if [ -f "$outfile" ]; then - echo "Not overwriting $outfile" - continue - fi + if [ -f "$outfile" ]; then + echo "Not overwriting $outfile" + continue + fi - set -x - tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")" - mplayer -novideo -ao pcm:file="${tmpdir}/audio.wav" -vo null "$infile" - sox "${tmpdir}/audio.wav" "${tmpdir}/audio-fixed.wav" speed "${SLOWDOWN}" gain -n - lame --preset standard "${tmpdir}/audio-fixed.wav" "${tmpdir}/audio.mp3" - mkvmerge -o "${outfile}" --default-duration "1:${FORCEFPS}fps" --no-audio "$infile" "${tmpdir}/audio.mp3" + set -x + tmpdir="$(mktemp -d "${TMPDIR:-/var/tmp}/pal-XXXXXXXX")" + mplayer -novideo -ao pcm:file="${tmpdir}/audio.wav" -vo null "$infile" + sox "${tmpdir}/audio.wav" "${tmpdir}/audio-fixed.wav" speed "${SLOWDOWN}" gain -n + lame --preset standard "${tmpdir}/audio-fixed.wav" "${tmpdir}/audio.mp3" + mkvmerge -o "${outfile}" --default-duration "1:${FORCEFPS}fps" --no-audio "$infile" "${tmpdir}/audio.mp3" - rm -rf "$tmpdir" + rm -rf "$tmpdir" done diff --git a/hencode b/hencode index d525ad7..e8af4c2 100755 --- a/hencode +++ b/hencode @@ -1,13 +1,13 @@ #!/bin/bash HandBrakeCLI \ - --markers \ - --quality 20 \ - --vfr \ - --loose-anamorphic \ - --encoder x264 \ - --encopts 'ref=16:bframes=8:b-adapt=2:direct=auto:me=umh:merange=24:subq=10:rc-lookahead=60:trellis=2' \ - --aencoder copy \ - --ab 192 \ - "$@" + --markers \ + --quality 20 \ + --vfr \ + --loose-anamorphic \ + --encoder x264 \ + --encopts 'ref=16:bframes=8:b-adapt=2:direct=auto:me=umh:merange=24:subq=10:rc-lookahead=60:trellis=2' \ + --aencoder copy \ + --ab 192 \ + "$@" diff --git a/hencode-recursive b/hencode-recursive index 8fd7314..e8481c5 100755 --- a/hencode-recursive +++ b/hencode-recursive @@ -1,8 +1,8 @@ #!/bin/bash -e if [ -z "$1" -o -z "$2" ]; then - echo "Usage: $0 sourcedir destdir" - exit 1 + echo "Usage: $0 sourcedir destdir" + exit 1 fi sourcedir="$(cd "$1" && pwd)" @@ -10,28 +10,28 @@ destdir="$(cd "$2" && pwd)" cd "$sourcedir" find . -type f | while read infile; do - if [ ! -r "$infile" ]; then - echo "Missing file $infile" - exit 1 - fi - outfile="${destdir}/$(echo "$infile" | sed 's/\.[a-zA-Z0-9]*$//').mp4" - if [ -e "$outfile" ]; then - echo "Skipping $infile" - continue - fi - mkdir -p "$(dirname "$outfile")" - HandBrakeCLI < /dev/null \ - --format mp4 \ - --encoder x264 \ - --encopts cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6 \ - --markers \ - --quality 21 \ - --deinterlace \ - --loose-anamorphic \ - --crop 24:24:24:24 \ - --aencoder faac \ - --ab 160 \ - --input "$infile" \ - --output "$outfile" + if [ ! -r "$infile" ]; then + echo "Missing file $infile" + exit 1 + fi + outfile="${destdir}/$(echo "$infile" | sed 's/\.[a-zA-Z0-9]*$//').mp4" + if [ -e "$outfile" ]; then + echo "Skipping $infile" + continue + fi + mkdir -p "$(dirname "$outfile")" + HandBrakeCLI < /dev/null \ + --format mp4 \ + --encoder x264 \ + --encopts cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6 \ + --markers \ + --quality 21 \ + --deinterlace \ + --loose-anamorphic \ + --crop 24:24:24:24 \ + --aencoder faac \ + --ab 160 \ + --input "$infile" \ + --output "$outfile" done diff --git a/identify-dvd b/identify-dvd index cf31457..6e08663 100755 --- a/identify-dvd +++ b/identify-dvd @@ -1,13 +1,13 @@ #!/bin/bash if [ -z "$1" ]; then - echo "Usage: $0 dvd-device" - exit 1 + echo "Usage: $0 dvd-device" + exit 1 fi mplayer 2> /dev/null \ - -quiet -ao null -vo null -frames 0 -identify -dvd-device "$1" dvd:// | \ - grep 'ID_DVD_TITLE_[0-9]*_LENGTH' | \ - sed -e 's/ID_DVD_TITLE_//' -e 's/_LENGTH=/ /' | \ - sort -n + -quiet -ao null -vo null -frames 0 -identify -dvd-device "$1" dvd:// | \ + grep 'ID_DVD_TITLE_[0-9]*_LENGTH' | \ + sed -e 's/ID_DVD_TITLE_//' -e 's/_LENGTH=/ /' | \ + sort -n diff --git a/mirror-dvd b/mirror-dvd index 540c994..a1e717c 100755 --- a/mirror-dvd +++ b/mirror-dvd @@ -3,8 +3,8 @@ MPLAYER="mplayer" if ! [ -b /dev/dvd ]; then - echo "Usage: $0 [dvd-device] [folder]" - exit 1 + echo "Usage: $0 [dvd-device] [folder]" + exit 1 fi dvddevice="${1:-/dev/dvd}" @@ -13,9 +13,9 @@ mkdir -p "$folder" cd "$folder" "$MPLAYER" 2> /dev/null \ - -quiet -ao null -vo null -frames 0 -identify -dvd-device "$dvddevice" dvd:// | \ - grep 'ID_DVD_TITLE_[0-9]*_LENGTH' | cut -d'_' -f4 | while read i + -quiet -ao null -vo null -frames 0 -identify -dvd-device "$dvddevice" dvd:// | \ + grep 'ID_DVD_TITLE_[0-9]*_LENGTH' | cut -d'_' -f4 | while read i do - "$MPLAYER" -dumpstream -dumpfile "movie_${i}.mpg" -dvd-device "$dvddevice" dvd://"$i" + "$MPLAYER" -dumpstream -dumpfile "movie_${i}.mpg" -dvd-device "$dvddevice" dvd://"$i" done diff --git a/py-encode b/py-encode index e92d89d..b7b4a6b 100755 --- a/py-encode +++ b/py-encode @@ -10,499 +10,499 @@ import shutil import tempfile class FatalException(Exception): - pass + pass def mkarg(arg): - if re.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg): - return arg - - if "'" not in arg: - return "'%s'" % arg - out = "\"" - for c in arg: - if c in "\\$\"`": - out += "\\" - out += c - out += "\"" - return out + if re.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg): + return arg + + if "'" not in arg: + return "'%s'" % arg + out = "\"" + for c in arg: + if c in "\\$\"`": + out += "\\" + out += c + out += "\"" + return out def midentify(source, field): - process = subprocess.Popen( - [ - "mplayer", source, - "-ao", "null", "-vo", "null", - "-frames", "0", "-identify", - ], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - for line in process.stdout: - try: - key, value = line.split("=") - except ValueError: - continue - if key == field: - return value.strip() + process = subprocess.Popen( + [ + "mplayer", source, + "-ao", "null", "-vo", "null", + "-frames", "0", "-identify", + ], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + for line in process.stdout: + try: + key, value = line.split("=") + except ValueError: + continue + if key == field: + return value.strip() def append_cmd(cmd, opt, var): - if var is not None: - cmd.append(opt) - cmd.append(str(var)) + if var is not None: + cmd.append(opt) + cmd.append(str(var)) def duplicate_opts(opts): - return optparse.Values(opts.__dict__) + return optparse.Values(opts.__dict__) def insert_mplayer_options(cmd, o): - if o.mplayer_done: - return - - do_opt = partial(append_cmd, cmd) - do_opt("-mc", o.mc) - do_opt("-ss", o.startpos) - do_opt("-endpos", o.endpos) - do_opt("-dvd-device", o.dvd) - do_opt("-chapter", o.chapter) - do_opt("-aid", o.audioid) - do_opt("-sid", o.subtitleid) - do_opt("-vf", o.vfilters) - do_opt("-af", o.afilters) - - if o.deinterlace: - cmd += ["-vf-pre", "yadif"] - if o.detelecine: - cmd += ["-vf-pre", "pullup,softskip", "-ofps", "24000/1001"] - if o.noskip: - cmd += ["-noskip"] - if o.skipkb: - cmd += ["-sb", str(o.skipkb * 1024)] + if o.mplayer_done: + return + + do_opt = partial(append_cmd, cmd) + do_opt("-mc", o.mc) + do_opt("-ss", o.startpos) + do_opt("-endpos", o.endpos) + do_opt("-dvd-device", o.dvd) + do_opt("-chapter", o.chapter) + do_opt("-aid", o.audioid) + do_opt("-sid", o.subtitleid) + do_opt("-vf", o.vfilters) + do_opt("-af", o.afilters) + + if o.deinterlace: + cmd += ["-vf-pre", "yadif"] + if o.detelecine: + cmd += ["-vf-pre", "pullup,softskip", "-ofps", "24000/1001"] + if o.noskip: + cmd += ["-noskip"] + if o.skipkb: + cmd += ["-sb", str(o.skipkb * 1024)] class Command(object): - def __init__(self, profile, opts): - self.profile = profile - self.opts = opts - self.__process = None - self.init() - - def init(self): - pass - - def check_command(self, cmd): - if self.opts.dump: - return - if subprocess.Popen(["which", cmd], stdout=open("/dev/null", "w")).wait() != 0: - raise FatalException("Command '%s' is required" % cmd) - - def check_no_file(self, path): - if os.path.exists(path): - raise FatalException("Output file '%s' exists." % path) - - def do_exec(self, args, wait=True): - if self.opts.dump: - print " ".join(map(mkarg, args)) - else: - self.__process = subprocess.Popen(args) - self.__args = args - if wait: - self.wait() - - def wait(self): - if self.__process == None: - return - if self.__process.wait() != 0: - raise FatalException("Failure executing command: %s" % self.__args) - self.__process = None + def __init__(self, profile, opts): + self.profile = profile + self.opts = opts + self.__process = None + self.init() + + def init(self): + pass + + def check_command(self, cmd): + if self.opts.dump: + return + if subprocess.Popen(["which", cmd], stdout=open("/dev/null", "w")).wait() != 0: + raise FatalException("Command '%s' is required" % cmd) + + def check_no_file(self, path): + if os.path.exists(path): + raise FatalException("Output file '%s' exists." % path) + + def do_exec(self, args, wait=True): + if self.opts.dump: + print " ".join(map(mkarg, args)) + else: + self.__process = subprocess.Popen(args) + self.__args = args + if wait: + self.wait() + + def wait(self): + if self.__process == None: + return + if self.__process.wait() != 0: + raise FatalException("Failure executing command: %s" % self.__args) + self.__process = None class MP4Box(Command): - def init(self): - self.check_command("MP4Box") - self.check_no_file(self.opts.output + ".mp4") + def init(self): + self.check_command("MP4Box") + self.check_no_file(self.opts.output + ".mp4") - def run(self): - o = self.opts - p = self.profile + def run(self): + o = self.opts + p = self.profile - if o.dump: - fps = "???" - else: - fps = midentify(p.video_tmp, "ID_VIDEO_FPS") + if o.dump: + fps = "???" + else: + fps = midentify(p.video_tmp, "ID_VIDEO_FPS") - self.do_exec([ - "MP4Box", - "-fps", fps, - "-add", p.video_tmp, - "-add", p.audio_tmp, - o.output + ".mp4" - ]) + self.do_exec([ + "MP4Box", + "-fps", fps, + "-add", p.video_tmp, + "-add", p.audio_tmp, + o.output + ".mp4" + ]) class MKVMerge(Command): - def init(self): - self.check_command("mkvmerge") - self.check_no_file(self.opts.output + ".mkv") + def init(self): + self.check_command("mkvmerge") + self.check_no_file(self.opts.output + ".mkv") - def run(self): - o = self.opts - p = self.profile + def run(self): + o = self.opts + p = self.profile - if o.dump: - fps = "???" - else: - fps = midentify(p.video_tmp, "ID_VIDEO_FPS") + if o.dump: + fps = "???" + else: + fps = midentify(p.video_tmp, "ID_VIDEO_FPS") - self.do_exec([ - "mkvmerge", - "-o", o.output + ".mkv", - "--default-duration", "0:%sfps"%fps, - p.video_tmp, - p.audio_tmp, - ]) + self.do_exec([ + "mkvmerge", + "-o", o.output + ".mkv", + "--default-duration", "0:%sfps"%fps, + p.video_tmp, + p.audio_tmp, + ]) class MencoderLossless(Command): - def init(self): - self.check_command("mencoder") - self.check_no_file("lossless.avi") - - ofut = self.opts - self.opts = duplicate_opts(ofut) - ofut.input = "lossless.avi" - ofut.mplayer_done = True - - def run(self): - fifo = False - if fifo: - os.mkfifo("lossless.avi") - o = self.opts - cmd = [] - cmd += ["mencoder", self.opts.input, "-o", "lossless.avi"] - cmd += ["-noconfig", "all"] - cmd += ["-oac", "copy", "-ovc", "lavc", "-lavcopts", "vcodec=ffv1:autoaspect"] - insert_mplayer_options(cmd, self.opts) - cmd += ["-vf-add", "harddup"] - self.do_exec(cmd, wait=not fifo) + def init(self): + self.check_command("mencoder") + self.check_no_file("lossless.avi") + + ofut = self.opts + self.opts = duplicate_opts(ofut) + ofut.input = "lossless.avi" + ofut.mplayer_done = True + + def run(self): + fifo = False + if fifo: + os.mkfifo("lossless.avi") + o = self.opts + cmd = [] + cmd += ["mencoder", self.opts.input, "-o", "lossless.avi"] + cmd += ["-noconfig", "all"] + cmd += ["-oac", "copy", "-ovc", "lavc", "-lavcopts", "vcodec=ffv1:autoaspect"] + insert_mplayer_options(cmd, self.opts) + cmd += ["-vf-add", "harddup"] + self.do_exec(cmd, wait=not fifo) class MPlayer(Command): - def init(self): - self.check_command("mplayer") - self.check_no_file("video.y4m") - self.check_no_file("audio.wav") - - def run(self): - os.mkfifo("video.y4m") - os.mkfifo("audio.wav") - cmd = [] - cmd += ["mplayer", self.opts.input] - cmd += ["-benchmark", "-noconsolecontrols", "-noconfig", "all"] - cmd += ["-vo", "yuv4mpeg:file=video.y4m"] - cmd += ["-ao", "pcm:waveheader:file=audio.wav"] - insert_mplayer_options(cmd, self.opts) - cmd += self.profile.mplayeropts - self.do_exec(cmd, wait=False) + def init(self): + self.check_command("mplayer") + self.check_no_file("video.y4m") + self.check_no_file("audio.wav") + + def run(self): + os.mkfifo("video.y4m") + os.mkfifo("audio.wav") + cmd = [] + cmd += ["mplayer", self.opts.input] + cmd += ["-benchmark", "-noconsolecontrols", "-noconfig", "all"] + cmd += ["-vo", "yuv4mpeg:file=video.y4m"] + cmd += ["-ao", "pcm:waveheader:file=audio.wav"] + insert_mplayer_options(cmd, self.opts) + cmd += self.profile.mplayeropts + self.do_exec(cmd, wait=False) class MencoderCopyAC3(Command): - def init(self): - self.check_command("mplayer") - self.check_no_file("audio.ac3") - self.profile.audio_tmp = "audio.ac3" - - def run(self): - cmd = [] - cmd += ["mencoder", self.opts.input] - cmd += ["-noconfig", "all"] - cmd += ["-ovc", "copy", "-oac", "copy"] - cmd += ["-of", "rawaudio", "-o", "audio.ac3"] - insert_mplayer_options(cmd, self.opts) - self.do_exec(cmd) + def init(self): + self.check_command("mplayer") + self.check_no_file("audio.ac3") + self.profile.audio_tmp = "audio.ac3" + + def run(self): + cmd = [] + cmd += ["mencoder", self.opts.input] + cmd += ["-noconfig", "all"] + cmd += ["-ovc", "copy", "-oac", "copy"] + cmd += ["-of", "rawaudio", "-o", "audio.ac3"] + insert_mplayer_options(cmd, self.opts) + self.do_exec(cmd) class X264(Command): - def init(self): - self.check_command("x264") - self.profile.video_tmp = "video.h264" + def init(self): + self.check_command("x264") + self.profile.video_tmp = "video.h264" - def run(self): - p = self.profile - cmd = [] - cmd += ["x264", "--no-progress"] - cmd += p.x264opts - cmd += ["-o", p.video_tmp] - cmd += ["video.y4m"] - self.do_exec(cmd, wait=False) + def run(self): + p = self.profile + cmd = [] + cmd += ["x264", "--no-progress"] + cmd += p.x264opts + cmd += ["-o", p.video_tmp] + cmd += ["video.y4m"] + self.do_exec(cmd, wait=False) class Lame(Command): - def init(self): - self.check_command("lame") - self.profile.audio_tmp = "audio.mp3" + def init(self): + self.check_command("lame") + self.profile.audio_tmp = "audio.mp3" - def run(self): - p = self.profile - cmd = [] - cmd += ["lame", "--quiet"] - cmd += p.lameopts - cmd += ["audio.wav"] - cmd += [p.audio_tmp] - self.do_exec(cmd, wait=False) + def run(self): + p = self.profile + cmd = [] + cmd += ["lame", "--quiet"] + cmd += p.lameopts + cmd += ["audio.wav"] + cmd += [p.audio_tmp] + self.do_exec(cmd, wait=False) class Faac(Command): - def init(self): - self.check_command("faac") - self.profile.audio_tmp = "audio.aac" + def init(self): + self.check_command("faac") + self.profile.audio_tmp = "audio.aac" - def run(self): - p = self.profile - cmd = [] - cmd += ["faac"] - cmd += ["-o", p.audio_tmp] - cmd += p.faacopts - cmd += ["audio.wav"] - self.do_exec(cmd, wait=False) + def run(self): + p = self.profile + cmd = [] + cmd += ["faac"] + cmd += ["-o", p.audio_tmp] + cmd += p.faacopts + cmd += ["audio.wav"] + self.do_exec(cmd, wait=False) class Mencoder(Command): - codec2opts = { - "xvid": "-xvidencopts", - "x264": "-x264encopts", - "faac": "-faacopts", - "mp3lame": "-lameopts", - } - - def init(self): - o = self.opts - p = self.profile - - self.check_command("mencoder") - self.check_no_file(o.output + ".avi") - - p.video_tmp = o.output + ".avi" - p.audio_tmp = o.output + ".avi" - - def run(self): - o = self.opts - p = self.profile - - cmd = [] - cmd += ["mencoder", o.input] - cmd += ["-noconfig", "all"] - insert_mplayer_options(cmd, o) - cmd += ["-vf-add", "harddup"] - cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts] - cmd += ["-oac", p.acodec] - if p.aopts: - cmd += [self.codec2opts[p.acodec], p.aopts] - cmd += self.profile.mplayeropts - cmd += ["-o", self.opts.output + ".avi"] - - self.do_exec(cmd) + codec2opts = { + "xvid": "-xvidencopts", + "x264": "-x264encopts", + "faac": "-faacopts", + "mp3lame": "-lameopts", + } + + def init(self): + o = self.opts + p = self.profile + + self.check_command("mencoder") + self.check_no_file(o.output + ".avi") + + p.video_tmp = o.output + ".avi" + p.audio_tmp = o.output + ".avi" + + def run(self): + o = self.opts + p = self.profile + + cmd = [] + cmd += ["mencoder", o.input] + cmd += ["-noconfig", "all"] + insert_mplayer_options(cmd, o) + cmd += ["-vf-add", "harddup"] + cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts] + cmd += ["-oac", p.acodec] + if p.aopts: + cmd += [self.codec2opts[p.acodec], p.aopts] + cmd += self.profile.mplayeropts + cmd += ["-o", self.opts.output + ".avi"] + + self.do_exec(cmd) class MencoderDemux(Command): - codec2exts = { - "xvid": "m4v", - "x264": "h264", - "faac": "aac", - "mp3lame": "mp3", - "copyac3": "ac3", - } - - def init(self): - o = self.opts - p = self.profile - - self.check_command("mencoder") - p.audio_tmp = "audio." + self.codec2exts[p.acodec] - p.video_tmp = "video." + self.codec2exts[p.vcodec] - self.check_no_file(p.audio_tmp) - self.check_no_file(p.video_tmp) - - def run(self): - o = self.opts - p = self.profile - - cmd = ["mencoder", "-ovc", "copy", "-oac", "copy", o.output + ".avi"] - cmd += ["-noconfig", "all", "-noskip", "-mc", "0"] - self.do_exec(cmd + ["-of", "rawaudio", "-o", p.audio_tmp]) - self.do_exec(cmd + ["-of", "rawvideo", "-o", p.video_tmp]) - self.do_exec(["rm", "-f", o.output + ".avi"]) + codec2exts = { + "xvid": "m4v", + "x264": "h264", + "faac": "aac", + "mp3lame": "mp3", + "copyac3": "ac3", + } + + def init(self): + o = self.opts + p = self.profile + + self.check_command("mencoder") + p.audio_tmp = "audio." + self.codec2exts[p.acodec] + p.video_tmp = "video." + self.codec2exts[p.vcodec] + self.check_no_file(p.audio_tmp) + self.check_no_file(p.video_tmp) + + def run(self): + o = self.opts + p = self.profile + + cmd = ["mencoder", "-ovc", "copy", "-oac", "copy", o.output + ".avi"] + cmd += ["-noconfig", "all", "-noskip", "-mc", "0"] + self.do_exec(cmd + ["-of", "rawaudio", "-o", p.audio_tmp]) + self.do_exec(cmd + ["-of", "rawvideo", "-o", p.video_tmp]) + self.do_exec(["rm", "-f", o.output + ".avi"]) class Profile(object): - def __init__(self, commands, **kwargs): - self.commands = commands - self.__dict__.update(kwargs) + def __init__(self, commands, **kwargs): + self.commands = commands + self.__dict__.update(kwargs) - def __contains__(self, keyname): - return hasattr(self, keyname) + def __contains__(self, keyname): + return hasattr(self, keyname) class Wait(object): - def __init__(self, commands): - self.commands = commands[:] - - def run(self): - for command in self.commands: - command.wait() + def __init__(self, commands): + self.commands = commands[:] + + def run(self): + for command in self.commands: + command.wait() profiles = { - "x264/lame" : - Profile( - commands=[MPlayer, X264, Lame, Wait, MKVMerge], - mplayeropts=[], - x264opts=["--preset", "veryslow", "--crf", "20"], - lameopts=["--preset", "medium"], - ), - - "x264/copyac3" : - Profile( - commands=[MPlayer, X264, Wait, MencoderCopyAC3, MKVMerge], - mplayeropts=["-nosound"], - x264opts=["--preset", "veryslow", "--crf", "20"], - ), - - "x264/lame/fast" : - Profile( - commands=[MPlayer, X264, Lame, Wait, MKVMerge], - mplayeropts=[], - x264opts=["--preset", "fast", "--crf", "23"], - lameopts=["--preset", "medium"], - ), - - "xvid/lame" : - Profile( - commands=[Mencoder], - mplayeropts=["-ffourcc", "DX50"], - vcodec="xvid", - vopts="fixed_quant=2:vhq=4:autoaspect", - acodec="mp3lame", - aopts="cbr:br=128", - ), - - "apple-quicktime" : - Profile( - commands=[MPlayer, X264, Faac, Wait, MP4Box], - mplayeropts=[], - x264opts=["--crf", "20", "--bframes", "1"], - faacopts=["-q", "100", "--mpeg-vers", "4"], - ), - - "nokia-n97" : - Profile( - commands=[Mencoder, MencoderDemux, MP4Box], - mplayeropts=["-vf-add", "scale=640:-10"], - vcodec="xvid", - vopts="bitrate=384:vhq=4:autoaspect:max_bframes=0", - acodec="faac", - aopts="br=64:mpeg=4:object=2", - ), + "x264/lame" : + Profile( + commands=[MPlayer, X264, Lame, Wait, MKVMerge], + mplayeropts=[], + x264opts=["--preset", "veryslow", "--crf", "20"], + lameopts=["--preset", "medium"], + ), + + "x264/copyac3" : + Profile( + commands=[MPlayer, X264, Wait, MencoderCopyAC3, MKVMerge], + mplayeropts=["-nosound"], + x264opts=["--preset", "veryslow", "--crf", "20"], + ), + + "x264/lame/fast" : + Profile( + commands=[MPlayer, X264, Lame, Wait, MKVMerge], + mplayeropts=[], + x264opts=["--preset", "fast", "--crf", "23"], + lameopts=["--preset", "medium"], + ), + + "xvid/lame" : + Profile( + commands=[Mencoder], + mplayeropts=["-ffourcc", "DX50"], + vcodec="xvid", + vopts="fixed_quant=2:vhq=4:autoaspect", + acodec="mp3lame", + aopts="cbr:br=128", + ), + + "apple-quicktime" : + Profile( + commands=[MPlayer, X264, Faac, Wait, MP4Box], + mplayeropts=[], + x264opts=["--crf", "20", "--bframes", "1"], + faacopts=["-q", "100", "--mpeg-vers", "4"], + ), + + "nokia-n97" : + Profile( + commands=[Mencoder, MencoderDemux, MP4Box], + mplayeropts=["-vf-add", "scale=640:-10"], + vcodec="xvid", + vopts="bitrate=384:vhq=4:autoaspect:max_bframes=0", + acodec="faac", + aopts="br=64:mpeg=4:object=2", + ), } mappings = { - "x264": "x264/lame", - "xvid": "xvid/lame", + "x264": "x264/lame", + "xvid": "xvid/lame", } for x, y in mappings.iteritems(): - profiles[x] = profiles[y] + profiles[x] = profiles[y] def parse_args(): - for profile_name in profiles.keys(): - if sys.argv[0].find(profile_name) >= 0: - break - else: - profile_name = "xvid/lame" - - parser = optparse.OptionParser(usage="%prog [options] input [output]") - parser.add_option("--dvd", action="store", dest="dvd") - parser.add_option("--fixmux", action="store_true", dest="fixmux") - parser.add_option("--deinterlace", action="store_true", dest="deinterlace") - parser.add_option("--detelecine", action="store_true", dest="detelecine") - parser.add_option("--mc", action="store", dest="mc", type="float") - parser.add_option("--noskip", action="store_true", dest="noskip") - parser.add_option("--vfilters", action="store", dest="vfilters") - parser.add_option("--afilters", action="store", dest="afilters") - parser.add_option("--chapter", action="store", dest="chapter") - parser.add_option("--skipkb", action="store", dest="skipkb", type="int") - parser.add_option("--startpos", action="store", dest="startpos") - parser.add_option("--endpos", action="store", dest="endpos") - parser.add_option("--audioid", action="store", dest="audioid") - parser.add_option("--subtitleid", action="store", dest="subtitleid") - parser.add_option("--profile", action="store", dest="profile_name", default=profile_name) - parser.add_option("--dump", action="store_true", dest="dump") - try: - opts, args = parser.parse_args(sys.argv[1:]) - if len(args) == 1: - input = args[0] - output = os.path.splitext(os.path.basename(input))[0] - elif len(args) == 2: - input, output = args - else: - raise ValueError - except Exception: - parser.print_usage() - sys.exit(1) - - if "://" not in input: - opts.input = os.path.abspath(input) - else: - if opts.dvd: - opts.dvd = os.path.abspath(opts.dvd) - opts.input = input - - opts.output = os.path.abspath(output) - opts.mplayer_done = False - - return opts + for profile_name in profiles.keys(): + if sys.argv[0].find(profile_name) >= 0: + break + else: + profile_name = "xvid/lame" + + parser = optparse.OptionParser(usage="%prog [options] input [output]") + parser.add_option("--dvd", action="store", dest="dvd") + parser.add_option("--fixmux", action="store_true", dest="fixmux") + parser.add_option("--deinterlace", action="store_true", dest="deinterlace") + parser.add_option("--detelecine", action="store_true", dest="detelecine") + parser.add_option("--mc", action="store", dest="mc", type="float") + parser.add_option("--noskip", action="store_true", dest="noskip") + parser.add_option("--vfilters", action="store", dest="vfilters") + parser.add_option("--afilters", action="store", dest="afilters") + parser.add_option("--chapter", action="store", dest="chapter") + parser.add_option("--skipkb", action="store", dest="skipkb", type="int") + parser.add_option("--startpos", action="store", dest="startpos") + parser.add_option("--endpos", action="store", dest="endpos") + parser.add_option("--audioid", action="store", dest="audioid") + parser.add_option("--subtitleid", action="store", dest="subtitleid") + parser.add_option("--profile", action="store", dest="profile_name", default=profile_name) + parser.add_option("--dump", action="store_true", dest="dump") + try: + opts, args = parser.parse_args(sys.argv[1:]) + if len(args) == 1: + input = args[0] + output = os.path.splitext(os.path.basename(input))[0] + elif len(args) == 2: + input, output = args + else: + raise ValueError + except Exception: + parser.print_usage() + sys.exit(1) + + if "://" not in input: + opts.input = os.path.abspath(input) + else: + if opts.dvd: + opts.dvd = os.path.abspath(opts.dvd) + opts.input = input + + opts.output = os.path.abspath(output) + opts.mplayer_done = False + + return opts def main(): - os.nice(1) - - opts = parse_args() - - # Find our profile - try: - profile = profiles[opts.profile_name] - except KeyError: - print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name - sys.exit(1) - - # Run in a temp dir so that multiple instances can be run simultaneously - tempdir = tempfile.mkdtemp() - try: - os.chdir(tempdir) - - try: - commands = [] - if opts.fixmux or opts.detelecine: - profile.commands.insert(0, MencoderLossless) - for CommandClass in profile.commands: - if Command in CommandClass.__bases__: - command = CommandClass(profile, opts) - else: - command = CommandClass(commands) - commands.append(command) - for command in commands: - command.run() - - except FatalException, e: - print >>sys.stderr, "Error:", str(e) - sys.exit(1) - - finally: - os.chdir("/") - shutil.rmtree(tempdir) + os.nice(1) + + opts = parse_args() + + # Find our profile + try: + profile = profiles[opts.profile_name] + except KeyError: + print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name + sys.exit(1) + + # Run in a temp dir so that multiple instances can be run simultaneously + tempdir = tempfile.mkdtemp() + try: + os.chdir(tempdir) + + try: + commands = [] + if opts.fixmux or opts.detelecine: + profile.commands.insert(0, MencoderLossless) + for CommandClass in profile.commands: + if Command in CommandClass.__bases__: + command = CommandClass(profile, opts) + else: + command = CommandClass(commands) + commands.append(command) + for command in commands: + command.run() + + except FatalException, e: + print >>sys.stderr, "Error:", str(e) + sys.exit(1) + + finally: + os.chdir("/") + shutil.rmtree(tempdir) if __name__ == "__main__": - main() + main() diff --git a/rip-audio b/rip-audio index 4d23eab..205c9d8 100755 --- a/rip-audio +++ b/rip-audio @@ -4,22 +4,22 @@ MPLAYER="mplayer" FFMPEG="ffmpeg" if [ -z "$1" ]; then - echo "Usage: $0 filename" - exit 1 + echo "Usage: $0 filename" + exit 1 fi INPUT="$1" acodec="$( - "$MPLAYER" -identify -vo null -ao null -frames 0 "$INPUT" | - grep ID_AUDIO_CODEC | - cut -d= -f2 + "$MPLAYER" -identify -vo null -ao null -frames 0 "$INPUT" | + grep ID_AUDIO_CODEC | + cut -d= -f2 )" if [ "$acodec" = "ffaac" ]; then - ext=".m4a" + ext=".m4a" else - echo "Unknown audio codec! $acodec" - exit 1 + echo "Unknown audio codec! $acodec" + exit 1 fi OUTPUT="$(basename "$INPUT" | sed 's/\.[a-zA-Z1-9]*$//')${ext}" diff --git a/rip-dvd b/rip-dvd index aeee4d7..588da09 100755 --- a/rip-dvd +++ b/rip-dvd @@ -3,8 +3,8 @@ DVD_DEVICE="${DVD_DEVICE:-/dev/dvd}" if [ -z "$1" ]; then - echo "Usage: $0 NAME [num]" - exit 1 + echo "Usage: $0 NAME [num]" + exit 1 fi @@ -12,12 +12,12 @@ fi base="$1" last="$(find . -maxdepth 1 -type d -name "${base}*" | sort -n | tail -n 1)" if [ -z "$last" ]; then - num="1" + num="1" elif [ -n "$2" ]; then - num="$2" + num="$2" else - num="${last##./${base}}" - num="$(($num + 1))" + num="${last##./${base}}" + num="$(($num + 1))" fi next="${base}${num}" @@ -34,8 +34,8 @@ echo echo echo "Will rip to '$next'" for i in $(seq 3 -1 1); do - echo " $i..." - sleep 1 + echo " $i..." + sleep 1 done diff --git a/trim-audio b/trim-audio index 4b68b1a..be09bf9 100755 --- a/trim-audio +++ b/trim-audio @@ -1,8 +1,8 @@ #!/bin/bash if [ -z "$1" -o -z "$2" -o -z "$3" ]; then - echo "Usage: $0 input start end" - exit 1 + echo "Usage: $0 input start end" + exit 1 fi input="$1" @@ -12,8 +12,8 @@ let duration="$endpos - $ss" output="${input%%.*}-trimmed.${input##*.}" if [ "$input" = "$output" ]; then - echo "Internal error! Would overwrite "$input" with output file" - exit 1 + echo "Internal error! Would overwrite "$input" with output file" + exit 1 fi ffmpeg -ss "$ss" -t "$duration" -i "$input" -acodec copy "$output" diff --git a/v4l-rip b/v4l-rip index 18fe8fc..f8528ad 100755 --- a/v4l-rip +++ b/v4l-rip @@ -1,8 +1,8 @@ #!/bin/bash if [ -z "$1" ]; then - echo "Usage: $0 output.avi" - exit 1 + echo "Usage: $0 output.avi" + exit 1 fi mencoder \ diff --git a/video-transform b/video-transform index 53e5561..d300152 100755 --- a/video-transform +++ b/video-transform @@ -19,307 +19,307 @@ ASPECT_RATIO = "4/3" def mkarg(arg): - if re.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg): - return arg - - if "'" not in arg: - return "'%s'" % arg - out = "\"" - for c in arg: - if c in "\\$\"`": - out += "\\" - out += c - out += "\"" - return out + if re.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg): + return arg + + if "'" not in arg: + return "'%s'" % arg + out = "\"" + for c in arg: + if c in "\\$\"`": + out += "\\" + out += c + out += "\"" + return out def fail(line_count, msg): - raise Exception(msg + " on line %d" % line_count) + raise Exception(msg + " on line %d" % line_count) def convert_frame_to_sample(frame): - return frame * AUDIO_SAMPLE_RATE / VIDEO_FPS + return frame * AUDIO_SAMPLE_RATE / VIDEO_FPS def run_cmd(cmd): - print "$", " ".join(map(mkarg, cmd)) - if DRY_RUN: - return - print - ret = subprocess.Popen(cmd).wait() - if ret != 0: - print >>sys.stderr, "Failed on command", cmd - raise Exception("Command returned non-zero: " + str(ret)) + print "$", " ".join(map(mkarg, cmd)) + if DRY_RUN: + return + print + ret = subprocess.Popen(cmd).wait() + if ret != 0: + print >>sys.stderr, "Failed on command", cmd + raise Exception("Command returned non-zero: " + str(ret)) def read_file_contents(filename): - try: - f = open(filename) - data = f.read().strip() - f.close() - return data - except IOError: - return None + try: + f = open(filename) + data = f.read().strip() + f.close() + return data + except IOError: + return None def explode_video_to_png(source): - image_cache_desc = os.path.join(TMP_DIR, "image_cache.txt") - image_cache_dir = os.path.join(TMP_DIR, "image_cache") - - # Do nothing if the current cache is what we need - current_cache = read_file_contents(image_cache_desc) - if source == current_cache: - return image_cache_dir - - # Remove if necessary - if os.path.exists(image_cache_dir): -### print "Confirm removal of image cache:", current_cache -### ok = raw_input("(Y/n) ") -### if ok != "Y": -### print "Exiting..." -### sys.exit(2) - shutil.rmtree(image_cache_dir) - - cmd = [ - "mplayer", - "-vo", "png:outdir=%s" % image_cache_dir, - "-nosound", - "-noconsolecontrols", - "-noconfig", "user", - "-benchmark", - source, - ] - run_cmd(cmd) - - # Cache has been created, save the description - f = open(image_cache_desc, "w") - f.write(source) - f.close() - - return image_cache_dir + image_cache_desc = os.path.join(TMP_DIR, "image_cache.txt") + image_cache_dir = os.path.join(TMP_DIR, "image_cache") + + # Do nothing if the current cache is what we need + current_cache = read_file_contents(image_cache_desc) + if source == current_cache: + return image_cache_dir + + # Remove if necessary + if os.path.exists(image_cache_dir): +### print "Confirm removal of image cache:", current_cache +### ok = raw_input("(Y/n) ") +### if ok != "Y": +### print "Exiting..." +### sys.exit(2) + shutil.rmtree(image_cache_dir) + + cmd = [ + "mplayer", + "-vo", "png:outdir=%s" % image_cache_dir, + "-nosound", + "-noconsolecontrols", + "-noconfig", "user", + "-benchmark", + source, + ] + run_cmd(cmd) + + # Cache has been created, save the description + f = open(image_cache_desc, "w") + f.write(source) + f.close() + + return image_cache_dir def explode_video_to_wav(source): - audio_cache_desc = os.path.join(TMP_DIR, "audio_cache.txt") - audio_cache_file = os.path.join(TMP_DIR, "audio_cache.wav") - - # Do nothing if the current cache is what we need - if source == read_file_contents(audio_cache_desc): - return audio_cache_file - - cmd = [ - "mencoder", - "-oac", "pcm", - "-ovc", "copy", - "-of", "rawaudio", - "-o", audio_cache_file + ".raw", - source, - ] - run_cmd(cmd) - - cmd = [ - "sox", - "-r", str(AUDIO_SAMPLE_RATE), "-b", "16", "-e", "signed-integer", - audio_cache_file + ".raw", - audio_cache_file, - ] - run_cmd(cmd) - - # Cache has been created, save the description - f = open(audio_cache_desc, "w") - f.write(source) - f.close() - - return audio_cache_file + audio_cache_desc = os.path.join(TMP_DIR, "audio_cache.txt") + audio_cache_file = os.path.join(TMP_DIR, "audio_cache.wav") + + # Do nothing if the current cache is what we need + if source == read_file_contents(audio_cache_desc): + return audio_cache_file + + cmd = [ + "mencoder", + "-oac", "pcm", + "-ovc", "copy", + "-of", "rawaudio", + "-o", audio_cache_file + ".raw", + source, + ] + run_cmd(cmd) + + cmd = [ + "sox", + "-r", str(AUDIO_SAMPLE_RATE), "-b", "16", "-e", "signed-integer", + audio_cache_file + ".raw", + audio_cache_file, + ] + run_cmd(cmd) + + # Cache has been created, save the description + f = open(audio_cache_desc, "w") + f.write(source) + f.close() + + return audio_cache_file def apply_audio_effects(source, dest, crop_start, crop_end, audio_normalize): - cmd = [ - "sox", - source, - dest, - ] - if audio_normalize: - cmd += ["gain", "-n"] - if crop_start and crop_end: - c = convert_frame_to_sample - cmd += ["trim", "%ds" % c(crop_start), "%ds" % c(crop_end - crop_start)] - run_cmd(cmd) + cmd = [ + "sox", + source, + dest, + ] + if audio_normalize: + cmd += ["gain", "-n"] + if crop_start and crop_end: + c = convert_frame_to_sample + cmd += ["trim", "%ds" % c(crop_start), "%ds" % c(crop_end - crop_start)] + run_cmd(cmd) def apply_single_image_effects(source_file, dest_file, color_matrix): - cmd = [ - "convert", - source_file, - "-color-matrix", color_matrix, - dest_file, - ] - run_cmd(cmd) + cmd = [ + "convert", + source_file, + "-color-matrix", color_matrix, + dest_file, + ] + run_cmd(cmd) def apply_image_effects(source_dir, crop_start, crop_end, color_matrix): - dest_dir = os.path.join(TMP_DIR, "image_processed") - if os.path.exists(dest_dir): - shutil.rmtree(dest_dir) - os.mkdir(dest_dir) - - inframe = crop_start - outframe = 0 - while inframe <= crop_end: - source_file = os.path.join(source_dir, str(inframe+1).zfill(8) + ".png") - dest_file = os.path.join(dest_dir, str(outframe+1).zfill(8) + ".png") - if color_matrix: - apply_single_image_effects(source_file, dest_file, color_matrix) - else: - os.link(source_file, dest_file) - inframe += 1 - outframe += 1 - - return dest_dir + dest_dir = os.path.join(TMP_DIR, "image_processed") + if os.path.exists(dest_dir): + shutil.rmtree(dest_dir) + os.mkdir(dest_dir) + + inframe = crop_start + outframe = 0 + while inframe <= crop_end: + source_file = os.path.join(source_dir, str(inframe+1).zfill(8) + ".png") + dest_file = os.path.join(dest_dir, str(outframe+1).zfill(8) + ".png") + if color_matrix: + apply_single_image_effects(source_file, dest_file, color_matrix) + else: + os.link(source_file, dest_file) + inframe += 1 + outframe += 1 + + return dest_dir def combine_audio_video(audio_file, image_dir, dest): - cmd = [ - "mencoder", - "mf://%s/*.png" % image_dir, - "-audiofile", audio_file, - "-force-avi-aspect", ASPECT_RATIO, - "-vf", "harddup", - "-af", "channels=1", - "-ovc", "lavc", - "-lavcopts", "vcodec=ffv1:ilme:ildct", - "-oac", "pcm", - "-o", dest, - ] - run_cmd(cmd) + cmd = [ + "mencoder", + "mf://%s/*.png" % image_dir, + "-audiofile", audio_file, + "-force-avi-aspect", ASPECT_RATIO, + "-vf", "harddup", + "-af", "channels=1", + "-ovc", "lavc", + "-lavcopts", "vcodec=ffv1:ilme:ildct", + "-oac", "pcm", + "-o", dest, + ] + run_cmd(cmd) class Job(object): - def __init__(self): - self.source = None - self.dest = None - self.crop_start = None - self.crop_end = None - self.color_matrix = None - self.audio_normalize = True - - def set_source(self, arg): - self.source = os.path.join(SOURCE_DIR, arg) - - def set_dest(self, arg): - self.dest = os.path.join(DEST_DIR, arg) - if not self.dest.endswith(".avi"): - self.dest += ".avi" - - def set_crop(self, arg): - a, b = arg.split("-") - self.crop_start = int(a) - self.crop_end = int(b) - - def set_colormatrix(self, arg): - [float(x) for x in arg.split(" ") if x] # check it's valid - self.color_matrix = arg - - def set_whitecolor(self, arg): - arg = arg.split(" ") - color = arg[0] - r = 0xff / int(color[0:2], 16) - g = 0xff / int(color[2:4], 16) - b = 0xff / int(color[4:6], 16) - # don't change the brightness - avg = (r + g + b) / 3 - if (avg - 1) > 0.02: - diff = avg - 1.0 - r -= diff - g -= diff - b -= diff - if len(arg) == 2: - brightness = float(arg[1]) - r *= brightness - g *= brightness - b *= brightness - self.set_colormatrix("%.3f 0 0 0 %.3f 0 0 0 %.3f" % (r, g, b)) - - def set_audionormalize(self, arg): - self.audio_normalize = int(arg) - - def validate(self, line_count, unique): - if self.dest in unique: - fail(line_count, "Non-unique output file: " + self.dest) - if self.source is None: - fail(line_count, "Missing source") - if self.dest is None: - fail(line_count, "Missing dest") - if not os.path.isfile(self.source): - fail(line_count, "Unable to find source: " + self.source) - - def is_done(self): - return os.path.isfile(self.dest) - - def run(self): - image_cache_dir = explode_video_to_png(self.source) - image_dir = apply_image_effects(image_cache_dir, self.crop_start, self.crop_end, self.color_matrix) - - audio_cache_file = explode_video_to_wav(self.source) - audio_file = os.path.join(TMP_DIR, "audio_processed.wav") - apply_audio_effects(audio_cache_file, audio_file, self.crop_start, self.crop_end, self.audio_normalize) - - combine_audio_video(audio_file, image_dir, self.dest+".tmp") - os.rename(self.dest+".tmp", self.dest) - - def __str__(self): - return "Job :: %s (%s)" % (self.dest, self.source) + def __init__(self): + self.source = None + self.dest = None + self.crop_start = None + self.crop_end = None + self.color_matrix = None + self.audio_normalize = True + + def set_source(self, arg): + self.source = os.path.join(SOURCE_DIR, arg) + + def set_dest(self, arg): + self.dest = os.path.join(DEST_DIR, arg) + if not self.dest.endswith(".avi"): + self.dest += ".avi" + + def set_crop(self, arg): + a, b = arg.split("-") + self.crop_start = int(a) + self.crop_end = int(b) + + def set_colormatrix(self, arg): + [float(x) for x in arg.split(" ") if x] # check it's valid + self.color_matrix = arg + + def set_whitecolor(self, arg): + arg = arg.split(" ") + color = arg[0] + r = 0xff / int(color[0:2], 16) + g = 0xff / int(color[2:4], 16) + b = 0xff / int(color[4:6], 16) + # don't change the brightness + avg = (r + g + b) / 3 + if (avg - 1) > 0.02: + diff = avg - 1.0 + r -= diff + g -= diff + b -= diff + if len(arg) == 2: + brightness = float(arg[1]) + r *= brightness + g *= brightness + b *= brightness + self.set_colormatrix("%.3f 0 0 0 %.3f 0 0 0 %.3f" % (r, g, b)) + + def set_audionormalize(self, arg): + self.audio_normalize = int(arg) + + def validate(self, line_count, unique): + if self.dest in unique: + fail(line_count, "Non-unique output file: " + self.dest) + if self.source is None: + fail(line_count, "Missing source") + if self.dest is None: + fail(line_count, "Missing dest") + if not os.path.isfile(self.source): + fail(line_count, "Unable to find source: " + self.source) + + def is_done(self): + return os.path.isfile(self.dest) + + def run(self): + image_cache_dir = explode_video_to_png(self.source) + image_dir = apply_image_effects(image_cache_dir, self.crop_start, self.crop_end, self.color_matrix) + + audio_cache_file = explode_video_to_wav(self.source) + audio_file = os.path.join(TMP_DIR, "audio_processed.wav") + apply_audio_effects(audio_cache_file, audio_file, self.crop_start, self.crop_end, self.audio_normalize) + + combine_audio_video(audio_file, image_dir, self.dest+".tmp") + os.rename(self.dest+".tmp", self.dest) + + def __str__(self): + return "Job :: %s (%s)" % (self.dest, self.source) def main(frames): - jobs = [] - unique = set() - - f = open(frames) - - job = None - count = 0 - - def append_job(): - if job is None: - return - job.validate(count, unique) - if job.is_done(): - print "Skipping", job - else: - jobs.append(job) - - for line in f: - count += 1 - line = line.strip() - if line.startswith("#"): - continue - if not line: - if job is not None: - append_job() - job = None - continue - - if job is None: - job = Job() - cmd, arg = line.split(" ", 1) - f = getattr(job, "set_"+cmd, None) - if not f: - fail(count, "Invalid command: " + cmd) - try: - f(arg) - except Exception, e: - fail(count, str(e)) - - # trailing job... - append_job() - - # optimise image and audio cache usage, use the current cache first if it exists - current_image_cache = read_file_contents(os.path.join(TMP_DIR, "image_cache.txt")) - jobs.sort(key=lambda job: (job.source if job.source != current_image_cache else "", job.dest)) - for job in jobs: - print "\n\n\nStarted job:", job, "\n\n" - job.run() + jobs = [] + unique = set() + + f = open(frames) + + job = None + count = 0 + + def append_job(): + if job is None: + return + job.validate(count, unique) + if job.is_done(): + print "Skipping", job + else: + jobs.append(job) + + for line in f: + count += 1 + line = line.strip() + if line.startswith("#"): + continue + if not line: + if job is not None: + append_job() + job = None + continue + + if job is None: + job = Job() + cmd, arg = line.split(" ", 1) + f = getattr(job, "set_"+cmd, None) + if not f: + fail(count, "Invalid command: " + cmd) + try: + f(arg) + except Exception, e: + fail(count, str(e)) + + # trailing job... + append_job() + + # optimise image and audio cache usage, use the current cache first if it exists + current_image_cache = read_file_contents(os.path.join(TMP_DIR, "image_cache.txt")) + jobs.sort(key=lambda job: (job.source if job.source != current_image_cache else "", job.dest)) + for job in jobs: + print "\n\n\nStarted job:", job, "\n\n" + job.run() if __name__ == "__main__": - try: - frames = sys.argv[1] - SOURCE_DIR = sys.argv[2] - DEST_DIR = sys.argv[3] - TMP_DIR = sys.argv[4] - except IndexError: - print >>sys.stderr, "Usage: %s frames.txt source_dir dest_dir tmp_dir" % sys.argv[0] - sys.exit(1) - - main(frames) + try: + frames = sys.argv[1] + SOURCE_DIR = sys.argv[2] + DEST_DIR = sys.argv[3] + TMP_DIR = sys.argv[4] + except IndexError: + print >>sys.stderr, "Usage: %s frames.txt source_dir dest_dir tmp_dir" % sys.argv[0] + sys.exit(1) + + main(frames) -- 2.39.2