'Update metadata in all formats of the specified book_ids to current metadata in the database.'

@write_api
def embed_metadata(self, book_ids, only_fmts=None, report_error=None, report_progress=None):

    field = self.fields[u'formats']
    from calibre.ebooks.metadata.opf2 import pretty_print
    from calibre.customize.ui import apply_null_metadata
    from calibre.ebooks.metadata.meta import set_metadata
    if only_fmts:
        only_fmts = {f.lower() for f in only_fmts}
    def doit(fmt, mi, stream):
        with apply_null_metadata:
            with pretty_print:
                set_metadata(stream, mi, stream_type=fmt, report_error=report_error)
        stream.seek(0, os.SEEK_END)
        return stream.tell()
    for (i, book_id) in enumerate(book_ids):
        fmts = field.table.book_col_map.get(book_id, ())
        if (not fmts):
            continue
        mi = self.get_metadata(book_id, get_cover=True, cover_as_data=True)
        try:
            path = self._field_for(u'path', book_id).replace(u'/', os.sep)
        except:
            continue
        for fmt in fmts:
            if ((only_fmts is not None) and (fmt.lower() not in only_fmts)):
                continue
            try:
                name = self.fields[u'formats'].format_fname(book_id, fmt)
            except:
                continue
            if (name and path):
                new_size = self.backend.apply_to_format(book_id, path, name, fmt, partial(doit, fmt, mi))
                if (new_size is not None):
                    self.format_metadata_cache[book_id].get(fmt, {})[u'size'] = new_size
                    max_size = self.fields[u'formats'].table.update_fmt(book_id, fmt, name, new_size, self.backend)
                    self.fields[u'size'].table.update_sizes({book_id: max_size})
        if (report_progress is not None):
            report_progress((i + 1), len(book_ids), mi)
