How to capture the thumb image of an audio file? (ffmpeg)

717 views
Skip to first unread message

Christophe

unread,
Sep 23, 2011, 9:30:59 AM9/23/11
to jpl...@googlegroups.com
Hi,

When uploading music to your computer or download it from the internet, sometimes there is a small thumb image shows up instead of the default OS audio icon. Does anybody know how to capture this thumb while upload it to a server?

I tried it with an .m4a audio file which has a thumb (cover of CD) from iTunes and this is the output metadata:

Array ( 
 [0] => ffmpeg version N-32733-gcadbe4e, Copyright (c) 2000-2011 the FFmpeg developers 
 [1] => built on Sep 20 2011 14:03:26 with gcc 4.4.3 
 [2] => configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid 
 [3] => libavutil 51. 16. 1 / 51. 16. 1 
 [4] => libavcodec 53. 16. 0 / 53. 16. 0
 [5] => libavformat 53. 12. 0 / 53. 12. 0 
 [6] => libavdevice 53. 4. 0 / 53. 4. 0 
 [7] => libavfilter 2. 43. 2 / 2. 43. 2 
 [8] => libswscale 2. 1. 0 / 2. 1. 0 
 [9] => libpostproc 51. 2. 0 / 51. 2. 0 
 [10] => Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/tmp/php0YlvtP': 
 [11] => Metadata: 
 [12] => major_brand : M4A 
 [13] => minor_version : 0 
 [14] => compatible_brands: M4A mp42isom 
 [15] => creation_time : 2010-05-13 19:51:19 
 [16] => title : Cleanin' Out My Closet 
 [17] => artist : Eminem 
 [18] => album : Curtain Call - The Hits
 [19] => track : 14 
 [20] => date : 2005 
 [21] => encoder : iTunes 9.1.1.12, QuickTime 7.6.6
 [22] => album_artist : Eminem 
 [23] => genre : Hip-Hop/Rap
 [24] => disc : 1
 [25] => Duration: 00:04:59.04, start: 0.000000, bitrate: 263 kb/s
 [26] => Stream #0.0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, s16, 260 kb/s
 [27] => Metadata:
 [28] => creation_time : 2010-05-13 19:51:19 
 [29] => At least one output file must be specified

I hope somebody knows this ... The rest of the upload script is working just fine :-)

Best,
Christophe

Jonathan2

unread,
Sep 23, 2011, 12:23:17 PM9/23/11
to jpl...@googlegroups.com
On Friday, September 23, 2011 2:30:59 PM UTC+1, Christophe wrote:

When uploading music to your computer or download it from the internet, sometimes there is a small thumb image shows up instead of the default OS audio icon. Does anybody know how to capture this thumb while upload it to a server?

I'm not exactly sure what you mean, but if you mean the embedded album art, I've just started using a library that takes care of id3 tags including album art etc

You need PHP:

Do either of the below look like they might work?

Example #4: Case study: How to display album cover image

ID3v2 can also contain images you might say. Well, there is a way to read them.

<?php
require_once
'Zend/Media/Id3v2.php'; // or using autoload

$id3
= new Zend_Media_Id3v2("file.mp3");
header
("Content-Type: " . $id3->apic->mimeType);
echo $id3
->apic->imageData;

No harder than that!

Example #5: Case study: How to extract cover images from music files

In a slightly harder example we extract the cover images from a directory full of music files. In this example we first scan the whole directory looking for MP3 files. After ensuring the file contains a valid ID3v2 tag and actually has a cover image, we gather information about the image and write the image data to a separate file, or the same file with an added image suffix.

<?php
require_once
'Zend/Media/Id3v2.php'; // or using autoload
require_once
'Zend/Media/Id3/Exception.php';

$directory
= "../change/path/to/mp3/directory/here";

/* Retrieve all files in the directory */
foreach (glob($directory . "/*.mp3") as $file) {
  echo
"Reading " . $file . "\n";
 
 
/* Attempt to parse the file, catching any exceptions */
 
try {
    $id3
= new Zend_Media_Id3v2($file);
 
}
 
catch (Zend_Media_Id3_Exception $e) {
    echo
"  " . $e->getMessage() . "\n";
   
continue;
 
}

 
if (isset($id3->apic)) {
    echo
"  Found a cover image, writing image data to a separate file..\n";
   
   
/* Write the image */
    $type
= explode("/", $id3->apic->mimeType, 2);
   
if (($handle = fopen
         
($image = $file . "." . $type[1], "wb")) !== false) {
     
if (fwrite($handle, $id3->apic->imageData,
                 $id3
->apic->imageSize) != $id3->apic->imageSize)
        echo
"  Found a cover image, but unable to write image file: " .
          $image
. "\n";
      fclose
($handle);
   
}
   
else echo "  Found a cover image, but unable to open image file " .
     
"for writing: " . $image . "\n";
 
} else
    echo
"  No cover image found!\n";
}
?>

One can run the script from a command prompt or terminal. With a little tuning, one could even take the source directory from the command line arguments.

Christophe

unread,
Sep 23, 2011, 2:26:19 PM9/23/11
to jpl...@googlegroups.com
Hi,

I think we mean the same thing ;-)

However, I don't use Zend, but CodeIgniter. Unfortunately I don't find a ID3v2 library for CI ... I'll try to convert the Zend library (or plugin) to a CI library. ;-)

Thanks!

Best,
Christophe

Jonathan2

unread,
Sep 25, 2011, 4:55:30 PM9/25/11
to jpl...@googlegroups.com
Ah, that's easy - you literally just include the one file you want from the Zend library:

require_once 'Zend/Media/Id3v2.php'

And that's it. No need to run the rest of the Zend framework.

Interesting you're using CI - I've just started learning it, and after finding endless tutorials for 1.7.3, finally I found this series of 5 introductions for 2.0.x:

What's this got to do with Jplayer some might ask?

Well, unless I've got it badly wrong, there's an ORM library called "redbean php" http://www.redbeanphp.com/ which integrates nicely with CI and  appears to make getting playlist info from a DB and chucking it out in jplayer-friendly JSON format a single-command piece of cake.

I'm still learning and fiddling, but if this is as easy as it looks, even I could get the hang of it. And that's saying something.

I'll report back!

Christophe

unread,
Sep 26, 2011, 7:46:07 AM9/26/11
to jpl...@googlegroups.com
Hi Jonathan,

It is a bit more complicated than that. CI has a different syntax than Zend. For example you have to set $this->include->library('Id3v2'); and after this line call methods like follow: $this->Id3v2->method('params');

However, a friend of mine said the album cover isn't part of the actual audio file, but you have to post the track name to an online database, and this database will send you the audio cover of that particular track.

Besides some extra libs, the main update from CI 1.7.x to 2.0 is the construct function at the beginning of each controller. I have learned CI with this book: http://www.amazon.co.uk/CodeIgniter-Professional-Development-Adam-Griffith/dp/1849510903/ref=sr_1_1?ie=UTF8&qid=1317037197&sr=8-1 and online tutorials like you said.

Best,
Christophe
PS: I love CI! I have tried Zend and Symfony, but always come back to CodeIgniter!!! ;-)

Christophe

unread,
Sep 26, 2011, 8:45:21 AM9/26/11
to jpl...@googlegroups.com
MY friend mailed me back and also referred to the id3v2  library ;-)

Grtz!
Reply all
Reply to author
Forward
0 new messages