diff --git a/beetsplug/convert.py b/beetsplug/convert.py index e7ac4f3ac..70363f6eb 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -148,6 +148,7 @@ class ConvertPlugin(BeetsPlugin): u'never_convert_lossy_files': False, u'copy_album_art': False, u'album_art_maxwidth': 0, + u'delete_originals': False, }) self.early_import_stages = [self.auto_convert] @@ -532,11 +533,16 @@ class ConvertPlugin(BeetsPlugin): # Change the newly-imported database entry to point to the # converted file. + source_path = item.path item.path = dest item.write() item.read() # Load new audio information data. item.store() + if self.config['delete_originals']: + self._log.info(u'Removing original file {0}', source_path) + util.remove(source_path, False) + def _cleanup(self, task, session): for path in task.old_paths: if path in _temp_files: diff --git a/docs/changelog.rst b/docs/changelog.rst index 4a87f7f07..0dfa2c5f8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -142,6 +142,10 @@ New features: * :doc:`/plugins/thumbnails`: Fix a bug where pathlib expected a string instead of bytes for a path. :bug:`3360` +* :doc:`/plugins/convert`: If ``delete_originals`` is enabled, then the source files will + be deleted after importing. + Thanks to :user:`logan-arens`. + :bug:`2947` Fixes: diff --git a/docs/plugins/convert.rst b/docs/plugins/convert.rst index 6e9d00a11..9581e24a4 100644 --- a/docs/plugins/convert.rst +++ b/docs/plugins/convert.rst @@ -111,6 +111,8 @@ file. The available options are: This option overrides ``link``. Only works when converting to a directory on the same filesystem as the library. Default: ``false``. +- **delete_originals**: Transcoded files will be copied or moved to their destination, depending on the import configuration. By default, the original files are not modified by the plugin. This option deletes the original files after the transcoding step has completed. + Default: ``false``. You can also configure the format to use for transcoding (see the next section): diff --git a/test/test_convert.py b/test/test_convert.py index 33bdb3b24..b8cd56741 100644 --- a/test/test_convert.py +++ b/test/test_convert.py @@ -15,6 +15,7 @@ from __future__ import division, absolute_import, print_function +import fnmatch import sys import re import os.path @@ -121,6 +122,15 @@ class ImportConvertTest(unittest.TestCase, TestHelper): self.assertIsNotNone(item) self.assertTrue(os.path.isfile(item.path)) + def test_delete_originals(self): + self.config['convert']['delete_originals'] = True + self.importer.run() + for path in self.importer.paths: + for root, dirnames, filenames in os.walk(path): + self.assertTrue(len(fnmatch.filter(filenames, '*.mp3')) == 0, + u'Non-empty import directory {0}' + .format(util.displayable_path(path))) + class ConvertCommand(object): """A mixin providing a utility method to run the `convert`command