From e49ca34f3cfe895a484c982ae0023d9d4bbeeb6b Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sat, 9 Mar 2013 19:09:43 -0300 Subject: [PATCH 1/4] partial commit --- beetsplug/convert.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 1a5574460..4c0ced6b1 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -17,6 +17,7 @@ import logging import os import threading +import pdb from subprocess import Popen from beets.plugins import BeetsPlugin @@ -121,6 +122,18 @@ def convert_item(lib, dest_dir, keep_new): _embed(artpath, [item]) +def convert_on_import(lib, item): + maxbr = config['convert']['max_bitrate'].get(int) + if item.format != 'MP3' or item.bitrate >= 1000 * maxbr: + # Transcoding necessary + dest = os.path.splitext(item.path)[0] + '.mp3' + encode(item.path, dest) + item.path = dest + item.write() + item.read() + lib.store(item) + + def convert_func(lib, opts, args): dest = opts.dest if opts.dest is not None else \ config['convert']['dest'].get() @@ -154,7 +167,9 @@ class ConvertPlugin(BeetsPlugin): u'opts': u'-aq 2', u'max_bitrate': 500, u'embed': True, + u'auto_convert': False }) + self.import_stages = [self.auto_convert] def commands(self): cmd = ui.Subcommand('convert', help='convert to external location') @@ -170,3 +185,12 @@ class ConvertPlugin(BeetsPlugin): help='set the destination directory') cmd.func = convert_func return [cmd] + + def auto_convert(self, config, task): + if self.config['auto_convert'].get(): + pdb.set_trace() + if not task.is_album: + convert_on_import(config.lib, task.item) + else: + for item in task.items: + convert_on_import(config.lib, item) From 27b1d6d7ccbcd68dacaf0884ced529ac756666dc Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sat, 9 Mar 2013 19:36:34 -0300 Subject: [PATCH 2/4] clean up old files and remove pdb --- beetsplug/convert.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 4c0ced6b1..3ed4aa726 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -17,7 +17,6 @@ import logging import os import threading -import pdb from subprocess import Popen from beets.plugins import BeetsPlugin @@ -28,6 +27,7 @@ from beets import config log = logging.getLogger('beets') DEVNULL = open(os.devnull, 'wb') _fs_lock = threading.Lock() +_convert_tmp = [] def _destination(lib, dest_dir, item, keep_new): @@ -127,6 +127,7 @@ def convert_on_import(lib, item): if item.format != 'MP3' or item.bitrate >= 1000 * maxbr: # Transcoding necessary dest = os.path.splitext(item.path)[0] + '.mp3' + _convert_tmp.append(dest) encode(item.path, dest) item.path = dest item.write() @@ -188,9 +189,16 @@ class ConvertPlugin(BeetsPlugin): def auto_convert(self, config, task): if self.config['auto_convert'].get(): - pdb.set_trace() if not task.is_album: convert_on_import(config.lib, task.item) else: for item in task.items: convert_on_import(config.lib, item) + + +@ConvertPlugin.listen('import_task_files') +def _cleanup(task, session): + for path in task.old_paths: + if path in _convert_tmp and os.path.isfile(path): + util.remove(path) + _convert_tmp.remove(path) From a3d8105a5a6153b25d2de2c39adf4d39bdb559ee Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sat, 9 Mar 2013 20:04:26 -0300 Subject: [PATCH 3/4] small fix --- beetsplug/convert.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 3ed4aa726..111debeeb 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -199,6 +199,7 @@ class ConvertPlugin(BeetsPlugin): @ConvertPlugin.listen('import_task_files') def _cleanup(task, session): for path in task.old_paths: - if path in _convert_tmp and os.path.isfile(path): - util.remove(path) + if path in _convert_tmp: + if os.path.isfile(path): + util.remove(path) _convert_tmp.remove(path) From c2c96d522f71d26f9b8f62e425f8ceef8eea43eb Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sat, 9 Mar 2013 23:33:45 -0300 Subject: [PATCH 4/4] config name and docs update --- beetsplug/convert.py | 4 ++-- docs/plugins/convert.rst | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 111debeeb..e79341eeb 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -168,7 +168,7 @@ class ConvertPlugin(BeetsPlugin): u'opts': u'-aq 2', u'max_bitrate': 500, u'embed': True, - u'auto_convert': False + u'auto': False }) self.import_stages = [self.auto_convert] @@ -188,7 +188,7 @@ class ConvertPlugin(BeetsPlugin): return [cmd] def auto_convert(self, config, task): - if self.config['auto_convert'].get(): + if self.config['auto'].get(): if not task.is_album: convert_on_import(config.lib, task.item) else: diff --git a/docs/plugins/convert.rst b/docs/plugins/convert.rst index d88fc40f6..6ec7d5b5e 100644 --- a/docs/plugins/convert.rst +++ b/docs/plugins/convert.rst @@ -57,6 +57,8 @@ The plugin offers several configuration options, all of which live under the "-aq 2". (Note that "-aq " is equivalent to the LAME option "-V ".) If you want to specify a bitrate, use "-ab ". Refer to the `FFmpeg`_ documentation for more details. +* ``auto`` gives you the option to import transcoded versions of your files + automatically during the ``import`` command. * Finally, ``threads`` determines the number of threads to use for parallel encoding. By default, the plugin will detect the number of processors available and use them all.