Repository: beetbox/beets

Issue:
convert: Respect `album_art_maxwidth` in conjunction with `embed`
### Problem

Hey guys, I think I've spotted a bug - like the title says. I noticed that my converted files have the full size images rather than my requested maxwidth of 600. 

However, if I just use beet embedart QUERY, the resize works fine and I can see the temporary images of the correct resolution being created in /tmp. So this tells me that its not a problem with imagemagick.

``` sh
$ beet -vv convert AURORA runaway
```

Led to this problem:

```
user configuration: /home/barry/.config/beets/config.yaml
data directory: /home/barry/.config/beets
plugin paths: 
Sending event: pluginload
artresizer: method is (2, (6, 9, 4))
library database: /home/barry/.config/beets/musiclibrary.blb
library directory: /home/barry/Music/lossless
Sending event: library_opened
AURORA - All My Demons Greeting Me as a Friend - Runaway
Convert? (Y/n) Y
convert: Encoding /home/barry/Music/lossless/AURORA/All My Demons Greeting Me as a Friend/01 Runaway.flac
convert: Finished encoding /home/barry/Music/lossless/AURORA/All My Demons Greeting Me as a Friend/01 Runaway.flac
Sending event: write
Sending event: after_write
convert: embedding album art from /home/barry/Music/lossless/AURORA/All My Demons Greeting Me as a Friend/cover.jpg
convert: embedding /home/barry/Music/lossless/AURORA/All My Demons Greeting Me as a Friend/cover.jpg
Sending event: write
Sending event: after_write
Sending event: after_convert
Sending event: cli_exit

```

I would have expected to see a line like:

```
artresizer: ImageMagick resizing /home/barry/Music/lossless/AURORA/All My Demons Greeting Me as a Friend/cover.jpg to /tmp/tmp5vch3h.jpg
```
### Setup
- OS: Arch Linux
- Python version: 2.7.12
- beets version: 1.3.18
- Turning off plugins made problem go away (yes/no): no

My configuration (output of `beet config`) is:

``` yaml
convert:
    never_convert_lossy_files: yes
    dest: ~/Music/lossy
    embed: yes
    album_art_maxwidth: 600
    format: aac
    formats:
        aac:
            command: ffmpeg -i $source -y -c:a libfdk_aac -vbr 3 $dest
            extension: m4a

```



Discussion:
Thanks for the report! I took a look at the code, and it seems that `album_art_maxwidth` is currently only respected when _copying_ album art files (i.e., with the `copy_album_art` option). It doesn't apply when embedding album art during conversion (i.e., with the `embed` option). It seems reasonable to me to make it apply to both.

Technical details: we use the setting in the `convert_album_art` method, but not in the condition under `if self.config['embed']:`. The right fix is probably just to pass the `maxwidth` parameter to the `embed_item` call therein.

This is a problem for me also. It seems my ancient Android phone will display album art embedded in MP3 files if 300x300 pixels, but not if 450x450 pixels. Thus this bug is blocking me from getting my MP3s right for that device. I may come up with a patch for this soon.


Resolving patch:
diff --git a/beetsplug/convert.py b/beetsplug/convert.py
--- a/beetsplug/convert.py
+++ b/beetsplug/convert.py
@@ -343,9 +343,10 @@ def convert_item(self, dest_dir, keep_new, path_formats, fmt,
             if self.config['embed'] and not linked:
                 album = item._cached_album
                 if album and album.artpath:
+                    maxwidth = self._get_art_resize(album.artpath)
                     self._log.debug('embedding album art from {}',
                                     util.displayable_path(album.artpath))
-                    art.embed_item(self._log, item, album.artpath,
+                    art.embed_item(self._log, item, album.artpath, maxwidth,
                                    itempath=converted, id3v23=id3v23)
 
             if keep_new:
@@ -389,20 +390,10 @@ def copy_album_art(self, album, dest_dir, path_formats, pretend=False,
             return
 
         # Decide whether we need to resize the cover-art image.
-        resize = False
-        maxwidth = None
-        if self.config['album_art_maxwidth']:
-            maxwidth = self.config['album_art_maxwidth'].get(int)
-            size = ArtResizer.shared.get_size(album.artpath)
-            self._log.debug('image size: {}', size)
-            if size:
-                resize = size[0] > maxwidth
-            else:
-                self._log.warning('Could not get size of image (please see '
-                                  'documentation for dependencies).')
+        maxwidth = self._get_art_resize(album.artpath)
 
         # Either copy or resize (while copying) the image.
-        if resize:
+        if maxwidth is not None:
             self._log.info('Resizing cover art from {0} to {1}',
                            util.displayable_path(album.artpath),
                            util.displayable_path(dest))
@@ -531,6 +522,24 @@ def convert_on_import(self, lib, item):
                 )
                 util.remove(source_path, False)
 
+    def _get_art_resize(self, artpath):
+        """For a given piece of album art, determine whether or not it needs
+        to be resized according to the user's settings. If so, returns the
+        new size. If not, returns None.
+        """
+        newwidth = None
+        if self.config['album_art_maxwidth']:
+            maxwidth = self.config['album_art_maxwidth'].get(int)
+            size = ArtResizer.shared.get_size(artpath)
+            self._log.debug('image size: {}', size)
+            if size:
+                if size[0] > maxwidth:
+                    newwidth = maxwidth
+            else:
+                self._log.warning('Could not get size of image (please see '
+                                  'documentation for dependencies).')
+        return newwidth
+
     def _cleanup(self, task, session):
         for path in task.old_paths:
             if path in _temp_files:


Test patch: