#380: correctly skip no-op transcodes

The format key is now the (lower-cased) format name string used by beets,
which means we can precisely detect which transcodes would be unnecessary. To
facilitate this, I added an ALIASES dict which allows more convenient names to
work for this (e.g., "wma" is easier to remember than "windows media").
This commit is contained in:
Adrian Sampson 2013-10-06 11:21:56 -07:00
parent 0d303ffde7
commit d828d7aae4
2 changed files with 30 additions and 16 deletions

View file

@ -31,6 +31,12 @@ DEVNULL = open(os.devnull, 'wb')
_fs_lock = threading.Lock()
_temp_files = [] # Keep track of temporary transcoded files for deletion.
# Some convenient alternate names for formats.
ALIASES = {
u'wma': u'windows media',
u'vorbis': u'ogg',
}
def _destination(dest_dir, item, keep_new, path_formats):
"""Return the path under `dest_dir` where the file should be placed
@ -50,7 +56,8 @@ def _destination(dest_dir, item, keep_new, path_formats):
def get_format():
"""Get the currently configured format command and extension.
"""
format = config['convert']['format'].get(unicode)
format = config['convert']['format'].get(unicode).lower()
format = ALIASES.get(format, format)
format_info = config['convert']['formats'][format].get(dict)
try:
return (format_info['command'].split(),
@ -89,7 +96,9 @@ def encode(source, dest):
return
if not quiet:
log.info(u'Finished encoding {0}'.format(util.displayable_path(source)))
log.info(u'Finished encoding {0}'.format(
util.displayable_path(source))
)
def should_transcode(item):
@ -97,8 +106,9 @@ def should_transcode(item):
conversion (i.e., its bitrate is high or it has the wrong format).
"""
maxbr = config['convert']['max_bitrate'].get(int)
return item.format not in ['AAC', 'MP3', 'Opus', 'OGG', 'Windows Media'] or item.bitrate >= 1000 * maxbr
format_name = config['convert']['format'].get(unicode)
return format_name.lower() == item.format.lower() or \
item.bitrate >= 1000 * maxbr
def convert_item(dest_dir, keep_new, path_formats):
@ -235,11 +245,11 @@ class ConvertPlugin(BeetsPlugin):
u'command': u'ffmpeg -i $source -y -acodec libopus -vn -ab 96k $dest',
u'extension': u'opus',
},
u'vorbis': {
u'ogg': {
u'command': u'ffmpeg -i $source -y -acodec libvorbis -vn -aq 2 $dest',
u'extension': u'ogg',
},
u'wma': {
u'windows media': {
u'command': u'ffmpeg -i $source -y -acodec wmav2 -vn $dest',
u'extension': u'wma',
},

View file

@ -1,13 +1,15 @@
Convert Plugin
==============
The ``convert`` plugin lets you convert parts of your collection to a directory
of your choice. It converts all input formats supported by `FFmpeg`_ to MP3.
It will skip files that are already present in the target directory. Converted
files follow the same path formats as your library.
The ``convert`` plugin lets you convert parts of your collection to a
directory of your choice, transcoding audio and embedding album art along the
way. It can transcode to and from any format using a configurable command
line. It will skip files that are already present in the target directory.
Converted files follow the same path formats as your library.
.. _FFmpeg: http://ffmpeg.org
Installation
------------
@ -20,6 +22,7 @@ by the plugin. Otherwise, configure the plugin to locate the executable::
convert:
ffmpeg: /usr/bin/ffmpeg
Usage
-----
@ -52,11 +55,12 @@ The plugin offers several configuration options, all of which live under the
transcoded and those with a lower bitrate will simply be copied. Note that
this does not guarantee that all converted files will have a lower
bitrate---that depends on the encoder and its configuration.
* ``format`` specify which format preset you would like to use. Default: mp3.
* ``formats`` lets you specify additional formats to convert to. Presets for
AAC, ALAC, FLAC, MP3, Opus, Vorbis and Windows Meda are provided, however
support may vary depending on your ffmpeg library. Each format is defined as
a command and a file extension.
* ``format`` is the name of the audio file format to transcode to. Files that
are already in the format (and are below the maximum bitrate) will not be
transcoded. Available formats include MP3, AAC, ALAC, FLAC, Opus, Vorbis,
and Windows Media; the default is MP3.
* ``formats`` lets you specify additional formats to convert to. Each format
is defined as a command and a file extension.
* ``auto`` gives you the option to import transcoded versions of your files
automatically during the ``import`` command. With this option enabled, the
importer will transcode all non-MP3 files over the maximum bitrate before
@ -97,4 +101,4 @@ use ``$$`` to emit a dollars sign.
In this example ``-aq <num>`` is equivalent to the LAME option ``-V num``. If
you want to specify a bitrate, use ``-ab <bitrate>``. Refer to the `FFmpeg`_
documentation for more details.
documentation for more details.