diff options
| -rw-r--r-- | config.py | 23 | ||||
| -rwxr-xr-x | cutter | 35 | ||||
| -rw-r--r-- | formats/flac.py | 8 | ||||
| -rw-r--r-- | formats/mp3.py | 15 | ||||
| -rw-r--r-- | formats/ogg.py | 33 |
5 files changed, 76 insertions, 38 deletions
@@ -43,15 +43,18 @@ DEFAULT_FILENAME_FORMAT = "{tracknumber:02d}.{title}" cfg = CfgParser() cfg.read(os.path.expanduser("~/.cutter.cfg")) -DIR = cfg.get("encoding", "dir", ".") -TYPE = cfg.get("encoding", "type") -USE_TEMPDIR = cfg.getbool("encoding", "use_tempdir") -COMPRESSION = cfg.getint("encoding", "compression") -BITRATE = cfg.getint("encoding", "bitrate") +DIR = cfg.get("encoding", "dir", ".") +TYPE = cfg.get("encoding", "type") +USE_TEMPDIR = cfg.getbool("encoding", "use_tempdir") +COMPRESSION = cfg.getint("encoding", "compression") -SAMPLE_RATE = cfg.getint("output", "sample_rate") -CHANNELS = cfg.getint("output", "channels") -BITS_PER_SAMPLE = cfg.getint("output", "bits_per_sample") +SAMPLE_RATE = cfg.getint("output", "sample_rate") +CHANNELS = cfg.getint("output", "channels") +BITS_PER_SAMPLE = cfg.getint("output", "bits_per_sample") -FILENAME_FORMAT = cfg.get("filename", "format", DEFAULT_FILENAME_FORMAT) -CONVERT_CHARS = cfg.getbool("filename", "convert_chars", False) +FILENAME_FORMAT = cfg.get("filename", "format", DEFAULT_FILENAME_FORMAT) +CONVERT_CHARS = cfg.getbool("filename", "convert_chars", False) + +FLAC_COMPRESSION = cfg.getint("flac", "compression") +OGG_COMPRESSION = cfg.getint("ogg", "compression") +MP3_BITRATE = cfg.getint("mp3", "bitrate") @@ -160,12 +160,11 @@ def parse_args(): help="do not use temporary directory") enc.add_option("-C", "--compression", type="int", - dest="compression", default=config.COMPRESSION, - metavar="FACTOR", + dest="compression", metavar="FACTOR", help="compression factor for output format (used for flac)") enc.add_option("--bitrate", type="int", - dest="bitrate", default=config.BITRATE, + dest="bitrate", default=config.MP3_BITRATE, help="audio bitrate (used for mp3)") parser.add_option_group(enc) @@ -213,14 +212,16 @@ def parse_args(): return parser.parse_args() -def process_options(opt): - if opt.compression is not None and (opt.compression < 0 or opt.compression > 8): - printerr("invalid compression value %d, must be in range 0 .. 8", opt.compression) +def option_check_range(option, value, min, max): + if value is not None and (value < min or value > max): + printerr("invalid %s value %d, must be in range %d .. %d", option, value, min, max) return False - if opt.bitrate is not None and (opt.bitrate < 32 or opt.bitrate > 320): - printerr("invalid bitrate value %d, must be in range 32 .. 320", opt.bitrate) - return False + return True + +def process_options(opt): + def choose(a, b): + return a if a is not None else b if opt.type is None and config.TYPE: if not formats.issupported(config.TYPE): @@ -233,6 +234,18 @@ def process_options(opt): printerr("--type option is missed") return False + if opt.type == "flac": + opt.compression = choose(opt.compression, config.FLAC_COMPRESSION) + if not option_check_range("compression", opt.compression, 0, 8): + return False + elif opt.type == "ogg": + opt.compression = choose(opt.compression, config.OGG_COMPRESSION) + if not option_check_range("compression", opt.compression, -1, 10): + return False + elif opt.type == "mp3": + if not option_check_range("bitrate", opt.bitrate, 32, 320): + return False + if not opt.dir: opt.dir = u"." else: @@ -431,9 +444,13 @@ class CueSplitter: return self.track_info[track].tags def tag(self, track, path): + printf("Tag [%s] : ", path) if not self.enctype.tag(path, self.track_tags(track)): + printf("FAILED\n") sys.exit(1) + printf("OK\n") + def copy_file(self, file): noteq = lambda a, b: a and a != b diff --git a/formats/flac.py b/formats/flac.py index 258b122..35db1e4 100644 --- a/formats/flac.py +++ b/formats/flac.py @@ -20,7 +20,6 @@ class FlacHandler(BaseHandler): def tag(self, path, tags): args = ["metaflac", "--remove-all-tags", "--import-tags-from=-", path] - self.log("Tag [%s] : ", path) proc = subprocess.Popen(args, stdin = subprocess.PIPE) for k, v in tags.items(): @@ -28,12 +27,7 @@ class FlacHandler(BaseHandler): proc.stdin.write(to_bytes("%s=%s\n" % (k.upper(), v))) proc.stdin.close() - if proc.wait(): - self.log("FAILED\n") - return False - - self.log("OK\n") - return True + return proc.wait() is 0 def init(): return FlacHandler diff --git a/formats/mp3.py b/formats/mp3.py index c668b47..cbd912f 100644 --- a/formats/mp3.py +++ b/formats/mp3.py @@ -59,9 +59,9 @@ class ID3Tagger: off = self.__offset.get(tag) if off: struct.pack_into("30s", self.v1, off, value) - elif tag is "date": + elif tag == "date": struct.pack_into("4s", self.v1, 93, value) - elif tag is "tracknumber": + elif tag == "tracknumber": number = int(value.partition(b"/")[0]) struct.pack_into("B", self.v1, 126, number) @@ -99,7 +99,7 @@ class Mp3Handler(BaseHandler): return self.build() - def do_tag(self, path, tags): + def tag(self, path, tags): tagger = ID3Tagger() for k, v in tags.items(): @@ -112,14 +112,5 @@ class Mp3Handler(BaseHandler): tagger.write(path) return True - def tag(self, path, tags): - self.log("Tag [%s] : ", path) - if self.do_tag(path, tags): - self.log("OK\n") - return True - - self.log("FAILED\n") - return False - def init(): return Mp3Handler diff --git a/formats/ogg.py b/formats/ogg.py new file mode 100644 index 0000000..c7352c2 --- /dev/null +++ b/formats/ogg.py @@ -0,0 +1,33 @@ +from formats.__base__ import * +from utils import to_bytes + +import subprocess + +class OggHandler(BaseHandler): + name = "ogg" + ext = "ogg" + + def encode(self, opt, info): + self.add("cust ext=%s sox -" % self.ext) + + if opt.compression is not None: + self.add("-C %d" % opt.compression) + + self.add_sox_args(opt, info) + self.add("%f") + + return self.build() + + def tag(self, path, tags): + args = ["vorbiscomment", "--raw", "--write", path] + + proc = subprocess.Popen(args, stdin = subprocess.PIPE) + for k, v in tags.items(): + if v is not "": + proc.stdin.write(to_bytes("%s=%s\n" % (k.upper(), v))) + proc.stdin.close() + + return proc.wait() is 0 + +def init(): + return OggHandler |
