Is there a way to programmatically extract cover art?

Is there a way to programmatically extract the cover art of an MP3 file and save it as a file? I know that it can be done manually through the Metadata pane, but I'd like to do it through the scripting interface if it's possible. I haven't been able to find a command to do that in the help though. Does anybody else have any ideas?

I don't recall seeing this in the scripting documentation, but I could be wrong. If not, maybe one of these (under Embedded Cover Art) will help:

[rockbox.org/wiki/AlbumArt](http://www.rockbox.org/wiki/AlbumArt)

and you might be able to call them from your script, if necessary.

The ExifTool will also do it:

sno.phy.queensu.ca/~phil/exiftool/

Example (from Mac OS, but Windows usage will be similar):

$ exiftool -a -G4 "-picture*" ~/Music\ Converted/Music/a-ha/Hunting\ High\ and\ Low/01\ Take\ On\ Me.mp3 [] Picture Mime Type : image/jpeg [] Picture Type : Front Cover [] Picture Description : Picture [] Picture : (Binary data 89337 bytes, use -b option to extract) [$ exiftool -picture -b ~/Music\ Converted/Music/a-ha/Hunting\ High\ and\ Low/01\ Take\ On\ Me.mp3 > cover.jpg

More info, and help setting the correct file extension:

u88.n24.queensu.ca/exiftool/foru ... l#msg25600
u88.n24.queensu.ca/exiftool/foru ... pic=2837.0

There is also a Perl module to do this programmatically (since you can use ActivePerl as a scripting language):

[code]use Image::ExifTool;

$exifTool = new Image::ExifTool;

$info = $exifTool->ImageInfo(shift) or
die 'ImageInfo open failed';

exists $info->{'Picture'} or
die 'No embedded image';

This gets the file type: $exifTool->ImageInfo($info->{'Picture'})->{'FileType'}

e.g 'JPEG'

This gets the MIME type: $exifTool->ImageInfo($info->{'Picture'})->{'MIMEType'}

e.g. 'image/jpeg'

open($out, '>:raw', 'cover.jpg') or
die "Unable to open: $!";
print $out ${$info->{'Picture'}};
close $out;

[/code]

Or you could simply use the MP3::Tag package. This is based on the answer found here

  • Have Perl installed, and enabled for DOpus scripting.
  • Add the MP3::Tag package using PPM.
  • Save this script in the addins folder and enable it
  • create a button with the command
ExtractTagImage

and it'll extract images from all selected mp3's.
As it is, it will create 0 byte files for mp3's without an image.

No guarantees, but it does so here anyway.

use v5.14;
use warnings;
use strict;
use Win32::OLE;
use Win32::OLE qw(in valof with OVERLOAD);
use MP3::Tag;

sub OnInit{
  my $initData=shift;
  my $cmd;
  $initData->{name} = "Extract Tag images";
  $initData->{desc} = "Extract images from tags in selected files";
  $initData->{copyright} = "myarmor";
  $initData->{version}="0.1";
  $initData->{default_enable}=1;

  $cmd = $initData->AddCommand();
  $cmd->{desc} = $initData->{desc};
  $cmd->{hide} = 0;
  $cmd->{icon} = "info";
  $cmd->{label} = "ExtractTagImage";
  $cmd->{method} = "OnExtractTagImage";
  $cmd->{name} = "ExtractTagImage";
  $cmd->{template} = "";
  return 0;
}

sub ExtractImage{
  my ($infile,$outfile)=@_;
  my $id3v2;
  $::DOpus->Output("Extract image from ".$infile);
  my $mp3 = MP3::Tag->new($infile) or die "died in ExtractImage";
  # Get the tags from the MP3
  $mp3->get_tags;
  $id3v2 = $mp3->{ID3v2};
  # Get the APIC frame from the Tag (The Image) which
  # returns a hash ref
  my $Pic = $id3v2->get_frame("APIC"); # returns
  open (SAVE, ">".$outfile);
  binmode SAVE;
  print SAVE $Pic->{_Data};
  close SAVE;
}

# Implement the TestIt command
sub OnExtractTagImage{
  my $scriptCommandData=shift;
  my $filename;
  my $mi;
  my $Data;
  #disable deselect
  $scriptCommandData->func->command->{deselect}=0;

  foreach my $item (in ($scriptCommandData->func->sourcetab->selected)){
    $::DOpus->Output($item->realpath);
    $filename=$item->realpath;
    ExtractImage($filename,$item->realpath."_output.jpg");
  }
}


# Called to display an About dialog for this script
sub OnAboutScript{
  my $aboutData=shift;
  my $dlg = $::DOpus->Dlg();
  $dlg->{window} = $aboutData->window;
  $dlg->{message} = "ExtractTagImage";
  $dlg->{title} = "About ExtractTagImage";
  $dlg->{buttons} = "OK";
  $dlg->{icon} = "info";
  $dlg->Show();
}

1;

ExtractImages.zip (955 Bytes)

In the Buttons area you'll find a useful post: Set/Clear/Extract WMA/MP3 Cover-art + Populate AlbumArtist

These are great ideas! Thank you all so much. I really appreciate it. :thumbsup:

I suspect I'll use ExifTool, since it has so much flexibility and I might be able to use it for other things as well.

I was hoping to avoid using a 3rd party program, but it'll have to do for now. If I wanted to request that this functionality be added to Directory Opus, where would be the best place to make that feature request? Here in this help forum?

Thanks again for all the help!

You don't need to do anything extra to request it; asking for it in this thread is enough.

Awesome! Thank you very much. :smiley: