From 28522376be46f2d2e69a53852c6fa2fe86905414 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Wed, 6 Mar 2013 21:41:40 -0300 Subject: [PATCH 1/2] Adding "keep new files" option to convert plugin --- beetsplug/convert.py | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 3ce3e34a4..3706c3617 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -22,6 +22,7 @@ from subprocess import Popen from beets.plugins import BeetsPlugin from beets import ui, util from beetsplug.embedart import _embed +from beets import library from beets import config log = logging.getLogger('beets') @@ -47,12 +48,18 @@ def encode(source, dest): log.info(u'Finished encoding {0}'.format(util.displayable_path(source))) -def convert_item(lib, dest_dir): +def convert_item(lib, dest_dir, keep_new): while True: item = yield - dest = os.path.join(dest_dir, lib.destination(item, fragment=True)) - dest = os.path.splitext(dest)[0] + '.mp3' + if keep_new: + dest_new = lib.destination(item) + dest_new = os.path.splitext(dest_new)[0] + '.mp3' + dest = os.path.join(dest_dir, lib.destination(item, + fragment=True)) + else: + dest = os.path.join(dest_dir, lib.destination(item, fragment=True)) + dest = os.path.splitext(dest)[0] + '.mp3' if os.path.exists(util.syspath(dest)): log.info(u'Skipping {0} (target file exists)'.format( @@ -71,9 +78,16 @@ def convert_item(lib, dest_dir): log.info(u'Copying {0}'.format(util.displayable_path(item.path))) util.copy(item.path, dest) else: - encode(item.path, dest) + if keep_new: + encode(item.path, dest_new) + log.info(u'Copying to destination {0}'. + format(util.displayable_path(dest))) + util.move(item.path, dest) + item.path = dest_new + else: + encode(item.path, dest) + item.path = dest - item.path = dest item.write() if config['convert']['embed']: @@ -83,14 +97,23 @@ def convert_item(lib, dest_dir): if artpath: _embed(artpath, [item]) + if keep_new: + item.read() + log.info(u'Updating new format {0}'.format(item.format)) + item.write() + lib.store(item) + def convert_func(lib, opts, args): dest = opts.dest if opts.dest is not None else \ - config['convert']['dest'].get() + config['convert']['dest'].get() if not dest: raise ui.UserError('no convert destination set') threads = opts.threads if opts.threads is not None else \ - config['convert']['threads'].get(int) + config['convert']['threads'].get(int) + + keep_new = opts.keep_new if opts.keep_new is not None \ + else config['convert']['keep_new'].get() ui.commands.list_items(lib, ui.decargs(args), opts.album, None) @@ -101,7 +124,7 @@ def convert_func(lib, opts, args): items = (i for a in lib.albums(ui.decargs(args)) for i in a.items()) else: items = lib.items(ui.decargs(args)) - convert = [convert_item(lib, dest) for i in range(threads)] + convert = [convert_item(lib, dest, keep_new) for i in range(threads)] pipe = util.pipeline.Pipeline([items, convert]) pipe.run_parallel() @@ -116,6 +139,7 @@ class ConvertPlugin(BeetsPlugin): u'opts': u'-aq 2', u'max_bitrate': 500, u'embed': True, + u'keep_new': False }) def commands(self): @@ -125,6 +149,9 @@ class ConvertPlugin(BeetsPlugin): cmd.parser.add_option('-t', '--threads', action='store', type='int', help='change the number of threads, \ defaults to maximum availble processors ') + cmd.parser.add_option('-k', '--keep-new', action='store_true', + dest='keep_new', help='keep only the converted \ + and move the old files') cmd.parser.add_option('-d', '--dest', action='store', help='set the destination directory') cmd.func = convert_func From a338b95bb78a7d53e7a5cc3b29b051d43d664412 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Wed, 6 Mar 2013 21:44:26 -0300 Subject: [PATCH 2/2] Changing behavior in "keep new files" --- beetsplug/convert.py | 56 +++++++++++++++++++++++----------------- docs/plugins/convert.rst | 4 ++- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 3706c3617..11c23223f 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -30,6 +30,26 @@ DEVNULL = open(os.devnull, 'wb') _fs_lock = threading.Lock() +def _dest_out(lib, dest_dir, item, keep_new): + """Path to the files outside the directory""" + + if keep_new: + return os.path.join(dest_dir, lib.destination(item, fragment=True)) + + dest = os.path.join(dest_dir, lib.destination(item, fragment=True)) + return os.path.splitext(dest)[0] + '.mp3' + + +def _dest_converted(lib, dest_dir, item, keep_new): + """Path to the newly converted files""" + + if keep_new: + dest = lib.destination(item) + return os.path.splitext(dest)[0] + '.mp3' + + return _dest_out(lib, dest_dir, item, keep_new) + + def encode(source, dest): log.info(u'Started encoding {0}'.format(util.displayable_path(source))) @@ -52,16 +72,10 @@ def convert_item(lib, dest_dir, keep_new): while True: item = yield - if keep_new: - dest_new = lib.destination(item) - dest_new = os.path.splitext(dest_new)[0] + '.mp3' - dest = os.path.join(dest_dir, lib.destination(item, - fragment=True)) - else: - dest = os.path.join(dest_dir, lib.destination(item, fragment=True)) - dest = os.path.splitext(dest)[0] + '.mp3' + dest_converted = _dest_converted(lib, dest_dir, item, keep_new) + dest_out = _dest_out(lib, dest_dir, item, keep_new) - if os.path.exists(util.syspath(dest)): + if os.path.exists(util.syspath(dest_out)): log.info(u'Skipping {0} (target file exists)'.format( util.displayable_path(item.path) )) @@ -71,23 +85,21 @@ def convert_item(lib, dest_dir, keep_new): # time. (The existence check is not atomic with the directory # creation inside this function.) with _fs_lock: - util.mkdirall(dest) + util.mkdirall(dest_out) maxbr = config['convert']['max_bitrate'].get(int) if item.format == 'MP3' and item.bitrate < 1000 * maxbr: log.info(u'Copying {0}'.format(util.displayable_path(item.path))) - util.copy(item.path, dest) + util.copy(item.path, dest_out) else: - if keep_new: - encode(item.path, dest_new) - log.info(u'Copying to destination {0}'. - format(util.displayable_path(dest))) - util.move(item.path, dest) - item.path = dest_new - else: - encode(item.path, dest) - item.path = dest + encode(item.path, dest_converted) + if keep_new: + log.info(u'Moving to destination {0}'. + format(util.displayable_path(dest_out))) + util.move(item.path, dest_out) + + item.path = dest_converted item.write() if config['convert']['embed']: @@ -112,8 +124,7 @@ def convert_func(lib, opts, args): threads = opts.threads if opts.threads is not None else \ config['convert']['threads'].get(int) - keep_new = opts.keep_new if opts.keep_new is not None \ - else config['convert']['keep_new'].get() + keep_new = opts.keep_new ui.commands.list_items(lib, ui.decargs(args), opts.album, None) @@ -139,7 +150,6 @@ class ConvertPlugin(BeetsPlugin): u'opts': u'-aq 2', u'max_bitrate': 500, u'embed': True, - u'keep_new': False }) def commands(self): diff --git a/docs/plugins/convert.rst b/docs/plugins/convert.rst index 1295b6a04..fd45265a6 100644 --- a/docs/plugins/convert.rst +++ b/docs/plugins/convert.rst @@ -26,7 +26,9 @@ Usage To convert a part of your collection, run ``beet convert QUERY``. This will display all items matching ``QUERY`` and ask you for confirmation before starting the conversion. The ``-a`` (or ``--album``) option causes the command -to match albums instead of tracks. +to match albums instead of tracks. The ``-k`` (or ``--keep-new``) allows you to +keep the new, converted, files in your library and move the origin files to the +destination directory. The ``-t`` (``--threads``) and ``-d`` (``--dest``) options allow you to specify or overwrite the respective configuration options.