convert: switch from flac and lame to ffmpeg

Instead of flac and lame the convert plugin now uses ffmpeg. This adds
support for more input formats and simplifies the code. ffmpeg also uses
the lame encoder internally and has equivalents of all the -V<num>
presets which should be sufficient.
This commit is contained in:
Jakob Schnitzer 2012-11-05 22:30:21 +01:00
parent 6e889bb6d0
commit 19acf0809f
2 changed files with 19 additions and 38 deletions

View file

@ -32,41 +32,22 @@ _fs_lock = threading.Lock()
def encode(source, dest):
log.info(u'Started encoding {0}'.format(util.displayable_path(source)))
temp_dest = dest + '~'
source_ext = os.path.splitext(source)[1].lower()
if source_ext == '.flac':
decode = Popen([conf['flac'], '-c', '-d', '-s', source],
stdout=PIPE)
encode = Popen([conf['lame']] + conf['opts'] + ['-', temp_dest],
stdin=decode.stdout, stderr=DEVNULL)
decode.stdout.close()
encode.communicate()
elif source_ext == '.mp3':
encode = Popen([conf['lame']] + conf['opts'] + ['--mp3input'] +
[source, temp_dest], close_fds=True, stderr=DEVNULL)
encode.communicate()
else:
log.error(u'Only converting from FLAC or MP3 implemented')
return
encode = Popen([conf['ffmpeg']] + ['-i', source] + conf['opts'] +
[dest], close_fds=True, stderr=DEVNULL)
encode.wait()
if encode.returncode != 0:
# Something went wrong (probably Ctrl+C), remove temporary files
log.info(u'Encoding {0} failed. Cleaning up...'.format(source))
util.remove(temp_dest)
util.prune_dirs(os.path.dirname(temp_dest))
util.remove(dest)
util.prune_dirs(os.path.dirname(dest))
return
shutil.move(temp_dest, dest)
log.info(u'Finished encoding {0}'.format(util.displayable_path(source)))
def convert_item(lib, dest_dir):
while True:
item = yield
if item.format != 'FLAC' and item.format != 'MP3':
log.info(u'Skipping {0} (unsupported format)'.format(
util.displayable_path(item.path)
))
continue
dest = os.path.join(dest_dir, lib.destination(item, fragment=True))
dest = os.path.splitext(dest)[0] + '.mp3'
@ -122,10 +103,9 @@ class ConvertPlugin(BeetsPlugin):
conf['dest'] = ui.config_val(config, 'convert', 'dest', None)
conf['threads'] = int(ui.config_val(config, 'convert', 'threads',
util.cpu_count()))
conf['flac'] = ui.config_val(config, 'convert', 'flac', 'flac')
conf['lame'] = ui.config_val(config, 'convert', 'lame', 'lame')
conf['ffmpeg'] = ui.config_val(config, 'convert', 'ffmpeg', 'ffmpeg')
conf['opts'] = ui.config_val(config, 'convert',
'opts', '-V2').split(' ')
'opts', '-aq 2').split(' ')
conf['max_bitrate'] = int(ui.config_val(config, 'convert',
'max_bitrate', '500'))
conf['embed'] = ui.config_val(config, 'convert', 'embed', True,

View file

@ -2,7 +2,7 @@ Convert Plugin
==============
The ``convert`` plugin lets you convert parts of your collection to a directory
of your choice. Currently only converting from MP3 or FLAC to MP3 is supported.
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.
@ -11,13 +11,12 @@ Installation
First, enable the ``convert`` plugin (see :doc:`/plugins/index`).
To transcode music, this plugin requires the ``flac`` and ``lame`` command-line
tools. If those executables are in your path, they will be found automatically
by the plugin. Otherwise, configure the plugin to locate the executables::
To transcode music, this plugin requires the ``ffmpeg`` command-line
tool. If its executable is in your path, it will be found automatically
by the plugin. Otherwise, configure the plugin to locate the executable::
[convert]
flac: /usr/bin/flac
lame: /usr/bin/lame
ffmpeg: /usr/bin/ffmpeg
Usage
-----
@ -44,10 +43,12 @@ The plugin offers several configuration options, all of which live under the
* If you set ``max_bitrate``, all MP3 files with a higher bitrate will be
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. By default, FLAC
files will be converted and all MP3s will be copied without transcoding.
* ``opts`` are the encoding options that are passed to ``lame``. Default:
"-V2". Please refer to the LAME documentation for possible options.
bitrate---that depends on the encoder and its configuration. By default MP3s
will be copied without transcoding and all other formats will be converted.
* ``opts`` are the encoding options that are passed to ``ffmpeg``. Default:
"-aq 2". "-aq <num>" is equivalent to the LAME option "-V <num>". If you
want to specify a bitrate use "-ab <bitrate>". Please refer to the FFMPEG
documentation for more details.
* 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.
@ -57,6 +58,6 @@ Here's an example configuration::
[convert]
embed: false
max_bitrate: 200
opts: -V4
opts: -aq 4
dest: /home/user/MusicForPhone
threads: 4