Page 1 of 1

Get Image from ID3 Tag

Posted: Mon Feb 07, 2011 1:05 pm
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

Re: Get Image from ID3 Tag

Posted: Wed Feb 16, 2011 1:05 pm
by effis
pliz!!!

Re: Get Image from ID3 Tag

Posted: Wed Feb 16, 2011 1:22 pm
by Rings

Re: Get Image from ID3 Tag

Posted: Wed Feb 16, 2011 1:24 pm
by effis
i've seen this
but it doesnt explaind how to save the picture

Re: Get Image from ID3 Tag

Posted: Wed Feb 16, 2011 1:38 pm
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.

Re: Get Image from ID3 Tag

Posted: Wed Feb 16, 2011 2:07 pm
by Baldrick

Re: Get Image from ID3 Tag

Posted: Wed Feb 16, 2011 2:12 pm
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

Re: Get Image from ID3 Tag

Posted: Thu Feb 17, 2011 3:04 am
by kenmo
Do you have a link to an example MP3 with an image in it?

I could try this out.

Re: Get Image from ID3 Tag

Posted: Thu Feb 17, 2011 4:19 am
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

Re: Get Image from ID3 Tag

Posted: Thu Feb 17, 2011 6:00 am
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

Re: Get Image from ID3 Tag

Posted: Thu Feb 17, 2011 7:26 am
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

Re: Get Image from ID3 Tag

Posted: Thu Feb 17, 2011 7:04 pm
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.)

Re: Get Image from ID3 Tag

Posted: Fri Feb 18, 2011 10:40 am
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