none
Mp3 Tag Editor and Excel VBA RRS feed

  • Question

  • Hi  Experts,

    1. I want to use Excel VBA, for editing Mp3 tags like filename, artists, track, etc.

    2. Using FileSystemObject class I can only access music files but can't access their tags / attributes.

    3. Which class/dll/ocx I have to use in Excel VBA or VB for such task.

    Saturday, June 9, 2012 11:40 PM

Answers

  • It seems that page can't display the "<" sign.

    Head the module Option Explicit and do Debug Compile repeatedly while finding & replacing the following as appropriate

    If (BufLen < 1) Then Exit Function
    If ((ReadID3v2.iiVersion < &HFF) And (ReadID3v2.iiSize And &H80808080) = 0&) Then
    If (ReadPos < (m_FileSize - 48)) Then
    Min = IIf(inA < inB, inA, inB)

    A Class module needs to be Instanciated and "kept alive' by assigning to an appropriately declared object variable. The following is a only simple example but hopefully enough to set you on your way to building a comprehensive MP3 project!

    ' in a normal module
    Sub Test_Read_MP3Tags()
    Dim rng As Range
    Dim c As clsMP3Info ' name of the class module
    Dim nSecs As Long
    ' put a valid MP3 path & file in A2:B2
    ' A2 = "C:\<path>\"
    ' B2 = "myFile.mp3"
         Set rng = Range("A2:P2")
         sFile = rng(1) & rng(2)
         Set c = New clsMP3Info
         With c
                 If Len(sFile) Then c.Filename = sFile
                 If Not c.ValidMP3 Then
                         rng(3) = "invalid MP3"
                 Else
                         rng(3) = .Artist
                         rng(4) = .Title
                         rng(5) = .Album
                         rng(6) = .GenreString
                         rng(7) = IIf(.Original, "yes", "no")
                         If .TrackNum = 0 Then
                         Else: rng(8) = .TrackNum
                         End If
                         rng(9) = .Year
                         rng(9) = .BitRate
                         nSecs = .SongLength
                         If nSecs Then
                                 rng(11) = CDate(nSecs / (60& * 60 * 24))
                                 rng(11).NumberFormat = "m:ss"
                         End If
                         rng(12) = .Comment
                 End If
         End With
    End Sub

    Look at all the Public Property Get & Let (ie read/write), obviously there are many more tags available.

    In your eventual project you will probably want to pass the filename to a routine as an argument, process along the lines as the example, and return an array of tag values as an argument

    Peter Thornton

    • Marked as answer by Ajay Check Wednesday, June 20, 2012 12:41 AM
    Sunday, June 10, 2012 8:43 PM
    Moderator

All replies

  • MP3 tags can be read (and written) with VBA but there's no single dll, the code required is complex and involves various API calls.

    I have MP3 tag stuff which works well with VBA and Excel which I adapted it from a class written by Mike Sutton & Norm Cook (for VB6). It wouldn't be appropriate to post it here and unfortunately it seems no longer available on the original site, however a copy of the module as text appears here and at a glance it appears to contain all the original header information -

    http://read.pudn.com/downloads149/sourcecode/windows/file/644748/clsMP3Info.cls__.htm

    Copy/paste from the first line starting with a comment into a class module named clsMP3Info

    Peter Thornton

    Sunday, June 10, 2012 3:32 PM
    Moderator
  • Hi, peter!

    1. I copied that class code, and inserted one class module in excel and pasted in it.

    2. But editor highlighted few code lines as syntax error, for eg:

    'Twice this code came
            BufLen = m_FileSize - m_HeaderOffset
            If (BufLen  1) Then Exit Function
            ReDim FBuffer(BufLen - 1) As Byte
    
    'Another instance of wrong syntax
     If ((ReadID3v2.iiVersion  &HFF) And (ReadID3v2.iiSize And &H80808080) = 0&) Then ' Look's valid
                                m_ID3v2Present = True
    
    
    'In above cases if condition produces syntax error.

    4. I cross checked with original code, and found no discrepancy.

    5. And second I have never worked with customized class.

    6. So after inserting this class, where should I edit name to name it clsMP3Info, and how could I use it.

    With Regards : Ajay Check

    
    Sunday, June 10, 2012 7:18 PM
  • It seems that page can't display the "<" sign.

    Head the module Option Explicit and do Debug Compile repeatedly while finding & replacing the following as appropriate

    If (BufLen < 1) Then Exit Function
    If ((ReadID3v2.iiVersion < &HFF) And (ReadID3v2.iiSize And &H80808080) = 0&) Then
    If (ReadPos < (m_FileSize - 48)) Then
    Min = IIf(inA < inB, inA, inB)

    A Class module needs to be Instanciated and "kept alive' by assigning to an appropriately declared object variable. The following is a only simple example but hopefully enough to set you on your way to building a comprehensive MP3 project!

    ' in a normal module
    Sub Test_Read_MP3Tags()
    Dim rng As Range
    Dim c As clsMP3Info ' name of the class module
    Dim nSecs As Long
    ' put a valid MP3 path & file in A2:B2
    ' A2 = "C:\<path>\"
    ' B2 = "myFile.mp3"
         Set rng = Range("A2:P2")
         sFile = rng(1) & rng(2)
         Set c = New clsMP3Info
         With c
                 If Len(sFile) Then c.Filename = sFile
                 If Not c.ValidMP3 Then
                         rng(3) = "invalid MP3"
                 Else
                         rng(3) = .Artist
                         rng(4) = .Title
                         rng(5) = .Album
                         rng(6) = .GenreString
                         rng(7) = IIf(.Original, "yes", "no")
                         If .TrackNum = 0 Then
                         Else: rng(8) = .TrackNum
                         End If
                         rng(9) = .Year
                         rng(9) = .BitRate
                         nSecs = .SongLength
                         If nSecs Then
                                 rng(11) = CDate(nSecs / (60& * 60 * 24))
                                 rng(11).NumberFormat = "m:ss"
                         End If
                         rng(12) = .Comment
                 End If
         End With
    End Sub

    Look at all the Public Property Get & Let (ie read/write), obviously there are many more tags available.

    In your eventual project you will probably want to pass the filename to a routine as an argument, process along the lines as the example, and return an array of tag values as an argument

    Peter Thornton

    • Marked as answer by Ajay Check Wednesday, June 20, 2012 12:41 AM
    Sunday, June 10, 2012 8:43 PM
    Moderator
  • I forgot this -

    6. So after inserting this class, where should I edit name to name it clsMP3Info, and how could I use it.

    Ensure VBE Properties window is showing (F4). Select the class module, you should see two fields, Name & Instancing. Edit the Name field. FWIW you can rename your ordinary modules the same way.

    "how could I use it.", see example in my previous post.

    Peter Thornton

    Sunday, June 10, 2012 8:51 PM
    Moderator
  • Hi, Peter and All,

    1. I tried, what you said, and came up with following code.

    Sub Mpegname()
    Dim c As clsMP3Info
    sfile = Sheets(1).Range("A1").Value
    
    Set oMp3 = New clsMP3Info
    oMp3.Filename = sfile
    Debug.Print Len(sfile)
    
    If oMp3.ValidMP3 = True Then
    Debug.Print oMp3.Artist
    End If
    End Sub

    2. But this code stops, at 5 line where mp3 filename gets passed into argument. I know, it never looks like passing argument, but oMp3.Filename(strMpefilename) produced error, so I followed your code.

    3. While debugging I could saw it gets halt at Loadmp3 method, giving error message : expecting array

    at function

    Else ' Read in variable bitrate header
                If (ReadPos(m_FileSize - 48)) Then
                    Seek #FNum, ReadPos + 36
                    Get #FNum, , VBRHeader()
                End If
            End If

    4. I couldn't understand more from your code :

    If Len(sFile) Then c.Filename = sFile

    5. To use this class properly, first I need to attach(passing filename as argument) or make connection of mp3 filename with class, after which I can read its attributes. But I am unable to acheive first part of attaching/connection of mp3 filename and class.

    With regards : Ajay Check

    Wednesday, June 13, 2012 9:36 PM
  • I just copied the code I posted last time into a normal module, added the class with several corrected instances of "<", put a valid file & path in cells, ran the routine and all worked perfectly. It's hard to know why it doesn't for you but try with exactly the code as posted (not sure why you changed things- what's oMp3 declared as).

    Ensure sFile represents a valid mp3 file, on which point rather than picking it up from cells do simply

    sFile = "C:\<path>\myFile.mp3"

    You say the class code fails on line 5, but line 5 is a commented line at the top of the module, so which line do you mean?

    Peter Thornton

    Thursday, June 14, 2012 3:10 PM
    Moderator
  • Hi, Peter and All !

    1. Peter, you guessed right. Problem was with instances of "<".
    2. There were few instances where Visual Basic Editor did not highlighted the absence of "<" in Class clsMp3Info ( Function LoadMp3).
    3. So it took bit time, to go through code lines, and to correct it.
    4. Thanks for making it successful.  . . . . . . . . . ;-)
    5. I went through Class clsMP3info, and got just basics of it. How it reads mp3 file in binary mode, checks for correct header tag, and converting value to string.
    6. I want to add 2 abilities to this Class : album art (from jpg/png image) and Lyrics (from a text file).
    7. But how can I achieve this? Is there any way to get full collection of tags ?(lots of tags are still missing in clsMp3Info) Is byte position predefined or it varies according to info inside tags.
    8. Any information/link/book reference would be helpful, in case it is out of scope topic. I am asking it because of curiosity, otherwise my task with mp3 tags, has got complete with clsMp3info. (my need was just to fill tag info (mainly artist, track name, no. , album) of various mp3 files in my music collection)

    With Regards : Ajay Check

    For reference : 

    Wednesday, June 20, 2012 1:18 AM
  • 2. There were few instances where Visual Basic Editor did not highlighted the absence of "<" in Class clsMp3Info ( Function LoadMp3).
    3. So it took bit time, to go through code lines, and to correct it.

    Strange, for me the compiler found all the missing "<" immediately, the whole process took about two minutes. Did you follow my suggestions how to go about this.

    4. Thanks for making it successful. . . . . . . . . . ;-)

    Glad it's working!

    6. I want to add 2 abilities to this Class : album art (from jpg/png image) and Lyrics (from a text file).
    7. But how can I achieve this? Is there any way to get full collection of tags ?(lots of tags are still missing in clsMp3Info) Is byte position predefined or it varies according to info inside tags.

    Afraid I don't know how to handle the other tags, images, lyrics etc.

    8. Any information/link/book reference would be helpful, in case it is out of scope topic.

    try searching "VB ID3 tag edit" (without quotes), also see this site
    http://id3.org/Home

    (my need was just to fill tag info (mainly artist, track name, no. , album) of various mp3 files in my music collection)

    If this is for your personal use you are welcome to try what I made for my own purposes, it does all of what you mention after 'my need' and quite a lot more, but not the "extended" stuff. If interested leave some way for me to contact you, eg a disguised email address.

    Peter Thornton

    Wednesday, June 20, 2012 10:39 AM
    Moderator
  • Hi, Peter

    1. Thanks, regarding sharing of your code. 
    2. Pass it on ajaycheck attherate yahoomail dot com. Looking forward to test code.

    With Regards : Ajay Check

    Thursday, June 21, 2012 11:37 PM
  • I've sent the file

    Peter Thornton

    Friday, June 22, 2012 2:40 PM
    Moderator
  • Hi, Peter

    1. My mistake. The correct id is : ajaycheck attherate yahoomail dot co dot in

    With Regards : Ajay Check

    Saturday, June 23, 2012 12:24 AM
  • You might take a look at http://www.mp3tag.de/en/ which does all you appear to want and more, without any wheel re-invention :)

    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, June 23, 2012 2:47 PM
  • I've just seen this, I'll send a quick email to the new address and await a response before sending the file

    Peter Thornton

    Wednesday, June 27, 2012 9:27 AM
    Moderator
  • Hi Graham,

    But where's the fun in that! OK, to be serious yes of course an app such as you referred to will do far more and more easily than anything we can write from scratch in VBA. FWIW I have also used MediaMonkey. That said, the Class I referred to works very well straight out of the box, at least for the non extended tags. Excel lends itself to batch processing the data in more flexible ways than any off-the-shelf-app can provide, and adapt to own needs. Eg, dump all filenames & tags to cells, rearrange/rewrite as required including renaming files based on tags, etc.

    Peter Thornton

    Wednesday, June 27, 2012 9:38 AM
    Moderator
  • > The correct id is : ajaycheck attherate yahoomail dot co dot in

    I sent an email to the above address but it bounced back!

    Peter Thornton

    Thursday, June 28, 2012 10:00 AM
    Moderator
  • Hi, Peter and All.

    1. The correct id is : ajaycheck attherate yahoo dot co dot in
    2. Never noticed that I typed yahoo-mail instead of yahoo. . . . :-(
    3. Did reading about ID3v2 basics and currently reading more about frames : http://www.id3.org/id3v2.4.0-frames.
    4. Yes, (Graham) there are custom apps for same purpose, but I am doing this for fun and learn purpose.
    5. Thanks, for still holding on this, with me.

    With Regards : Ajay Check

    Thursday, June 28, 2012 2:20 PM
  • > 1.The correct id is : ajaycheck attherate yahoo dot co dot in

    I sent an email to this address too and it also bounced back.

    Try sending yourself an email and see it arrives.

    Peter Thornton

    Friday, June 29, 2012 1:48 PM
    Moderator
  • this is exactly what I have been searching for long... :-) :-) could you please send it to riyas202 at gmail dot com..?
    Thursday, June 26, 2014 6:09 PM
  • I sent you an email, did you get it?
    Tuesday, July 1, 2014 4:26 PM
    Moderator
  • Guys, I am looking through this thread in details and this is probably end will my search:-)

    Can anyone of you please share the file or code with me?

    sandeep.patil attherate cyient.com

    Wednesday, July 23, 2014 9:15 AM