# Buggy code
def convert_item(dest_dir, keep_new, path_formats, command, ext, pretend=False):
    while True:
        item = yield
        dest = item.destination(basedir=dest_dir, path_formats=path_formats)

        # When keeping the new file in the library, we first move the
        # current (pristine) file to the destination. We'll then copy it
        # back to its old path or transcode it to a new path.
        if keep_new:
            original = dest
            converted = replace_ext(item.path, ext)
        else:
            original = item.path
            dest = replace_ext(dest, ext)
            converted = dest

        # Ensure that only one thread tries to create directories at a
        # time. (The existence check is not atomic with the directory
        # creation inside this function.)
        if not pretend:
            with _fs_lock:
                util.mkdirall(dest)

        if os.path.exists(util.syspath(dest)):
            log.info(
                "Skipping {0} (target file exists)".format(
                    util.displayable_path(item.path)
                )
            )
            continue

        if keep_new:
            if pretend:
                log.info(
                    "mv {0} {1}".format(
                        util.displayable_path(item.path),
                        util.displayable_path(original),
                    )
                )
            else:
                log.info("Moving to {0}".format(util.displayable_path(original)))
                util.move(item.path, original)

        if not should_transcode(item):
            if pretend:
                log.info(
                    "cp {0} {1}".format(
                        util.displayable_path(original),
                        util.displayable_path(converted),
                    )
                )
            else:
                # No transcoding necessary.
                log.info("Copying {0}".format(util.displayable_path(item.path)))
                util.copy(original, converted)
        else:
            try:
                encode(command, original, converted, pretend)
            except subprocess.CalledProcessError:
                continue

        if pretend:
            continue

        # Write tags from the database to the converted file.
        item.write(path=converted)

        if keep_new:
            # If we're keeping the transcoded file, read it again (after
            # writing) to get new bitrate, duration, etc.
            item.path = converted
            item.read()
            item.store()  # Store new path and audio data.

        if config["convert"]["embed"]:
            album = item.get_album()
            if album and album.artpath:
                embed_item(item, album.artpath, itempath=converted)

        plugins.send("after_convert", item=item, dest=dest, keepnew=keep_new)


# Traceback (beets.mediafile.FileTypeError)
Traceback (most recent call last):
File "/bin/beet", line 9, in <module>
load_entry_point('beets==1.3.6', 'console_scripts', 'beet')()
File "/usr/lib/python2.7/site-packages/beets/ui/__init__.py", line 967, in main
_raw_main(args)
File "/usr/lib/python2.7/site-packages/beets/ui/__init__.py", line 958, in _raw_main
subcommand.func(lib, suboptions, subargs)
File "/usr/lib/python2.7/site-packages/beetsplug/convert.py", line 251, in convert_func
pipe.run_parallel()
File "/usr/lib/python2.7/site-packages/beets/util/pipeline.py", line 345, in run
self.coro.send(msg)
File "/usr/lib/python2.7/site-packages/beetsplug/convert.py", line 179, in convert_item
item.write(path=converted)
File "/usr/lib/python2.7/site-packages/beets/library.py", line 369, in write
mediafile = MediaFile(path)
File "/usr/lib/python2.7/site-packages/beets/mediafile.py", line 1239, in __init__
raise FileTypeError('file type unsupported by Mutagen')
beets.mediafile.FileTypeError: file type unsupported by Mutagen

# Fixed code
def convert_item(dest_dir, keep_new, path_formats, command, ext, pretend=False):
    while True:
        item = yield
        dest = item.destination(basedir=dest_dir, path_formats=path_formats)

        # When keeping the new file in the library, we first move the
        # current (pristine) file to the destination. We'll then copy it
        # back to its old path or transcode it to a new path.
        if keep_new:
            original = dest
            converted = replace_ext(item.path, ext)
        else:
            original = item.path
            dest = replace_ext(dest, ext)
            converted = dest

        # Ensure that only one thread tries to create directories at a
        # time. (The existence check is not atomic with the directory
        # creation inside this function.)
        if not pretend:
            with _fs_lock:
                util.mkdirall(dest)

        if os.path.exists(util.syspath(dest)):
            log.info(
                "Skipping {0} (target file exists)".format(
                    util.displayable_path(item.path)
                )
            )
            continue

        if keep_new:
            if pretend:
                log.info(
                    "mv {0} {1}".format(
                        util.displayable_path(item.path),
                        util.displayable_path(original),
                    )
                )
            else:
                log.info("Moving to {0}".format(util.displayable_path(original)))
                util.move(item.path, original)

        if not should_transcode(item):
            if pretend:
                log.info(
                    "cp {0} {1}".format(
                        util.displayable_path(original),
                        util.displayable_path(converted),
                    )
                )
            else:
                # No transcoding necessary.
                log.info("Copying {0}".format(util.displayable_path(item.path)))
                util.copy(original, converted)
        else:
            try:
                encode(command, original, converted, pretend)
            except subprocess.CalledProcessError:
                continue

        if pretend:
            continue

        # Write tags from the database to the converted file.
        item.try_write(path=converted)

        if keep_new:
            # If we're keeping the transcoded file, read it again (after
            # writing) to get new bitrate, duration, etc.
            item.path = converted
            item.read()
            item.store()  # Store new path and audio data.

        if config["convert"]["embed"]:
            album = item.get_album()
            if album and album.artpath:
                embed_item(item, album.artpath, itempath=converted)

        plugins.send("after_convert", item=item, dest=dest, keepnew=keep_new)