Delete after convert (#3700)

* If import move is true, files will be deleted after converting.
Fixes #2947

* Removed trailing whitespace to comply with W293, fixing build

* Add period to the end of the comment

Co-Authored-By: Adrian Sampson <adrian@radbox.org>

* Added changelog entry for this fix.

* Added delete_originals option to remove source files after transcode

* Added unit test, removed redundant syspath call

Co-authored-by: Logan Arens <logan-arens@users.noreply.github.com>
Co-authored-by: Logan Arens <heresmygithub@protonmail.com>
Co-authored-by: Adrian Sampson <adrian@radbox.org>
Co-authored-by: Logan Arens <logan.arens@protonmail.com>
This commit is contained in:
Samuel Cook 2020-08-03 18:20:20 -07:00 committed by GitHub
parent 78d8e31eeb
commit b89a2650cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 0 deletions

View file

@ -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:

View file

@ -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:

View file

@ -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):

View file

@ -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