Get Image from ID3 Tag

Just starting out? Need help? Post your questions and find answers here.
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Get Image from ID3 Tag

Post by effis »

mp3 files comport information about the song, icluding a picture of the album
how can i get it and save it as a file?
and how can i write a new picture?

thanks
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Get Image from ID3 Tag

Post by effis »

pliz!!!
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Get Image from ID3 Tag

Post by Rings »

SPAMINATOR NR.1
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Get Image from ID3 Tag

Post by effis »

i've seen this
but it doesnt explaind how to save the picture
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Get Image from ID3 Tag

Post by c4s »

Well, either you get a code, library, dll that does this for you or you have to parse the ID3 information for yourself: Read ID3 into memory, parse it and find the appropriate tag, read out where the image is saved (if any available), save this memory section to disk.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: Get Image from ID3 Tag

Post by Baldrick »

effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Get Image from ID3 Tag

Post by effis »

i've print all af this site and resd it but its complicated for me,
maybe someone has a code for me?

thanks
User avatar
kenmo
Addict
Addict
Posts: 2070
Joined: Tue Dec 23, 2003 3:54 am

Re: Get Image from ID3 Tag

Post by kenmo »

Do you have a link to an example MP3 with an image in it?

I could try this out.
User avatar
kenmo
Addict
Addict
Posts: 2070
Joined: Tue Dec 23, 2003 3:54 am

Re: Get Image from ID3 Tag

Post by kenmo »

This code seems to work with [ this mp3 ]. Only tested with ID3 v2.3, might need to be adapted more to work with the newer v2.4.

Code: Select all

; Quick'n'dirty ID3 image extractor by kenmo

File.s = OpenFileRequester("Open MP3", "", "MP3 Files|*.mp3|All Files|*.*", 0)

If (File)
  If (ReadFile(0, File))
    LOF.i = Lof(0)
    If (LOF > 0)
      *Buffer.i = AllocateMemory(LOF)
      If (*Buffer)
        If (ReadData(0, *Buffer, LOF) = LOF)
          If (PeekS(*Buffer, 3, #PB_Ascii) = "ID3")
            ID3Version.i = PeekU(*Buffer + 3)
            If ((ID3Version = 3) Or (ID3Version = 4))
              *Image.i = #Null
              For i = 0 To LOF - 1
                If (PeekS(*Buffer + i, 4, #PB_Ascii) = "APIC")
                  *Image = *Buffer + i
                  Break
                EndIf
              Next i
              If (*Image)
                Length.i = 0
                If (ID3Version = 3)
                  For i = 0 To 3
                    Length = (Length << 8) | PeekA(*Image + 4 + i)
                  Next i
                Else
                  For i = 0 To 3
                    Length = (Length << 7) | (PeekA(*Image + 4 + i) & %01111111)
                  Next i
                EndIf
                If (Length > 0)
                
                  Flags.i = PeekU(*Image + 8)
                  Encoding.i = PeekA(*Image + 10)
                  If (Encoding = 1)
                    StrType.i  = #PB_Unicode
                    NullSize.i =  2
                  Else
                    StrType.i  = #PB_Ascii
                    NullSize.i =  1
                  EndIf
                  
                  Mime.s = LCase(Trim(PeekS(*Image + 11, -1, #PB_Ascii)))
                  PicType.i = PeekA(*Image + 12 + Len(Mime))
                  
                  *Image = *Image + 13 + Len(Mime)
                  Desc.s = PeekS(*Image, -1, StrType)
                  *Image = *Image + StringByteLength(Desc, StrType) + NullSize
                  
                  UsePNGImageDecoder()
                  UseJPEGImageDecoder()
                  
                  Img.i = CatchImage(0, *Image)
                  If (Img)
                    If (OpenWindow(0, 0, 0, ImageWidth(0) + 80, ImageHeight(0) + 80, GetFilePart(File), #PB_Window_ScreenCentered|#PB_Window_MinimizeGadget))
                      ImageGadget(0, 40, 40, ImageWidth(0), ImageHeight(0), ImageID(0))
                      GadgetToolTip(0, "Click image to save.")
                      Repeat
                        Event.i = WaitWindowEvent()
                        If (Event = #PB_Event_Gadget)
                          If (EventType() = #PB_EventType_LeftClick)
                            NewFile.s = SaveFileRequester("Save Image", GetPathPart(File), "Bitmap Image|*.bmp", 0)
                            If (NewFile)
                              If (LCase(GetExtensionPart(NewFile)) <> "bmp")
                                NewFile + ".bmp"
                              EndIf
                              If (SaveImage(0, NewFile))
                                Debug "Image saved!"
                              Else
                                Debug "Could not save image."
                              EndIf
                            EndIf
                          EndIf
                        EndIf
                      Until (Event = #PB_Event_CloseWindow)
                      CloseWindow(0)
                    Else
                      Debug "Could not open window."
                    EndIf
                    FreeImage(0)
                  Else
                    Debug "Could not catch image from memory."
                  EndIf
                  
                Else
                  Debug "Frame length is nonpositive."
                EndIf
              Else
                Debug "No image found in MP3."
              EndIf
            Else
              Debug "Unsupported ID3 version (not 2.3 or 2.4)."
            EndIf
          Else
            Debug "File does not have ID3v2 tag."
          EndIf
        Else
          Debug "Could not read entire file."
        EndIf
        FreeMemory(*Buffer)
      Else
        Debug "Could not allocate memory."
      EndIf
    Else
      Debug "File length is zero."
    EndIf
    CloseFile(0)
  Else
    Debug "File could not be read."
  EndIf
EndIf
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Get Image from ID3 Tag

Post by effis »

Thank you you are a genius!!!!!!!!!!!!!!!!!!!!!! :D :D :D

and to writing a new image in the tag, maybe you have something?

thanks again
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Get Image from ID3 Tag

Post by effis »

Indeed it does not work on other versions (2 and 4)
This is another coding system, or just a little trick has changed?
thank you A LOT
User avatar
kenmo
Addict
Addict
Posts: 2070
Joined: Tue Dec 23, 2003 3:54 am

Re: Get Image from ID3 Tag

Post by kenmo »

Writing to tags is much harder than reading from them, since you have to keep track of the tags already there, reorganize them, possibly resize the file, etc...

A few years ago I messed with ID3 tags but it was a big mess trying to keep track of all the different encodings and tiny changes between versions. (For example, in the code above where it calculates the Frame size, ID3v2.3 bitshifts by 8 where ID3v2.4 bitshifts by 7.... but clearly there are other problems if you said it doesn't work on v2.4.)
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Get Image from ID3 Tag

Post by effis »

i dont know what happend with other version, but you wrote in the code that its only for v3 and for other versions it debug an error
Post Reply