[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1417
  • Last Modified:

Reading/editing MP3 file properties

Is there a .NET class or namespace which provides facilities for reading/editing MP3 file properties?
0
StaffordMartin
Asked:
StaffordMartin
  • 4
  • 3
1 Solution
 
StaffordMartinAuthor Commented:
/
0
 
Dave BrettCommented:

Hi,
Reading the properties into Excel is straightforward with VBA using the Shell.Application
If your run the VBA below (  ListAllMP3Files() macro credited to Zorvek) then the code will do a recursive dump into the active Excel sheet (clearing all data) for the attributes below, of all Mp3 files that sit below the Music folder for the logged on user
  Artist,   AlbumTitle,    SongTitle,    RecordingYear ,    Genre, Duration, TrackNumber,  BitRate, AlbumArtist , Composer
The attribute numbers vary between Vista and XP, the attributes are listed in this thread, http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=160880&page=1 
While it is possible to use a DLL to write the properties this can be problematic - in my case when I played with this code 6 months back I didn't get it working as I'd hoped
http://www.mrexcel.com/forum/showthread.php?t=322393 
Cheers
Dave
Option Explicit
 
Public Enum tMP3Tag
    Artist = 13
    AlbumTitle = 14
    SongTitle = 21
    RecordingYear = 15
    Genre = 16
    Duration = 27
    TrackNumber = 26
    BitRate = 28
    AlbumArtist = 204
    Composer = 206
End Enum
 
Public Sub ListAllMP3Files()
 
    Dim ws As Object
    Dim Row As Long, MyDir As String
 
    Set ws = CreateObject("wscript.shell")
    MyDir = ws.specialfolders(16)
    ActiveSheet.Cells.Clear
    ActiveSheet.[A1:m1].Value = Array("Folder", "File", "Size", "Artist", "Album Title", "Song Title", "Recording Year", "Genre", "Duration", "number", "Bit Rate", "Album Artist", "Composer")
    ListMP3Files MyDir & "\Music\"
    Application.StatusBar = False
End Sub
 
Public Sub ListMP3Files( _
       ByVal Folder As Variant _
     )
 
    Dim FileName As String
    Dim Folders As Collection
    Dim IsDirectory As Boolean
 
    FileName = Dir(Folder & "*.*", vbNormal + vbDirectory)
    Do While Len(FileName) > 0
        If Not FileName = "." And Not FileName = ".." Then
            Application.StatusBar = Left("Processing " & Folder & FileName, 255)
            On Error Resume Next
            IsDirectory = False
            IsDirectory = GetAttr(Folder & FileName) And vbDirectory
            On Error GoTo 0
            If IsDirectory Then
                If Folders Is Nothing Then Set Folders = New Collection
                Folders.Add Folder & FileName & "\"
            ElseIf LCase(Right(FileName, 4)) = ".mp3" Then
                ListMP3File Folder, FileName
            End If
        End If
        FileName = Dir
    Loop
    If Not Folders Is Nothing Then
        For Each Folder In Folders
            ListMP3Files Folder
        Next Folder
    End If
 
End Sub
 
Public Sub ListMP3File( _
       ByVal Folder As String, _
       ByVal FileName As String _
     )
    Static Row As Long
 
    If Row = 0 Then Row = 2
    ActiveSheet.Cells(Row, 1).Resize(1, 12).Value = _
    Array( _
    Folder, _
    FileName, _
    FileLen(Folder & FileName), _
    GetMP3Tag(Folder & FileName, Artist), _
    GetMP3Tag(Folder & FileName, AlbumTitle), _
    GetMP3Tag(Folder & FileName, SongTitle), _
    GetMP3Tag(Folder & FileName, RecordingYear), _
    GetMP3Tag(Folder & FileName, Genre), _
    GetMP3Tag(Folder & FileName, Duration), _
    GetMP3Tag(Folder & FileName, TrackNumber), _
    GetMP3Tag(Folder & FileName, BitRate), _
    GetMP3Tag(Folder & FileName, AlbumArtist))
    Row = Row + 1
 
End Sub
 
Public Function GetMP3Tag( _
       ByVal MP3FilePath As String, _
       ByVal MP3Tag As tMP3Tag _
     ) As String
 
' Return the requested MP3 tag from the specified MP3 file. Pass the fill path to the MP3
' file in the parameter MP3FilePath. Pass the requested MP3 tag ID in the parameter MP3
' tag. The MP3 tags that can be read are:
'
'   Artist = 16
'   AlbumTitle = 17
'   SongTitle = 10
'   TrackNumber = 19
'   RecordingYear = 18
'   Genre = 20
'   Duration = 21
'   BitRate = 22
 
    Static ShellApplication As Object
    Dim ShellFolder As Object
    Dim ShellFolderItem As Object
    Dim FilePath As String
    Dim FileName As String
 
    FilePath = Left(MP3FilePath, InStrRev(MP3FilePath, "\"))
    FileName = Right(MP3FilePath, Len(MP3FilePath) - InStrRev(MP3FilePath, "\"))
 
    Set ShellApplication = CreateObject("Shell.Application")
    Set ShellFolder = ShellApplication.Namespace(CStr(FilePath))
    Set ShellFolderItem = ShellFolder.ParseName(FileName)
    GetMP3Tag = ShellFolder.GetDetailsOf(ShellFolderItem, MP3Tag)
 
End Function

Open in new window

0
 
Dave BrettCommented:
Hi,
Reading the properties into Excel is straightforward with VBA using the Shell.Application
If your run the VBA below (  ListAllMP3Files() macro credited to Zorvek) then the code will do a recursive dump into the active Excel sheet (clearing all data) for the attributes below, of all Mp3 files that sit below the Music folder for the logged on user
  Artist,   AlbumTitle,    SongTitle,    RecordingYear ,    Genre, Duration, TrackNumber,  BitRate, AlbumArtist , Composer
The attribute numbers vary between Vista and XP, the attributes are listed in this thread, http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=160880&page=1
While it is possible to use a DLL to write the properties this can be problematic - in my case when I played with this code 6 months back I didn't get it working as I'd hoped
http://www.mrexcel.com/forum/showthread.php?t=322393
Cheers
Dave

Option Explicit
 
Public Enum tMP3Tag
    Artist = 13
    AlbumTitle = 14
    SongTitle = 21
    RecordingYear = 15
    Genre = 16
    Duration = 27
    TrackNumber = 26
    BitRate = 28
    AlbumArtist = 204
    Composer = 206
End Enum
 
Public Sub ListAllMP3Files()
 
    Dim ws As Object
    Dim Row As Long, MyDir As String
 
    Set ws = CreateObject("wscript.shell")
    MyDir = ws.specialfolders(16)
    ActiveSheet.Cells.Clear
    ActiveSheet.[A1:m1].Value = Array("Folder", "File", "Size", "Artist", "Album Title", "Song Title", "Recording Year", "Genre", "Duration", "number", "Bit Rate", "Album Artist", "Composer")
    ListMP3Files MyDir & "\Music\"
    Application.StatusBar = False
End Sub
 
Public Sub ListMP3Files( _
       ByVal Folder As Variant _
     )
 
    Dim FileName As String
    Dim Folders As Collection
    Dim IsDirectory As Boolean
 
    FileName = Dir(Folder & "*.*", vbNormal + vbDirectory)
    Do While Len(FileName) > 0
        If Not FileName = "." And Not FileName = ".." Then
            Application.StatusBar = Left("Processing " & Folder & FileName, 255)
            On Error Resume Next
            IsDirectory = False
            IsDirectory = GetAttr(Folder & FileName) And vbDirectory
            On Error GoTo 0
            If IsDirectory Then
                If Folders Is Nothing Then Set Folders = New Collection
                Folders.Add Folder & FileName & "\"
            ElseIf LCase(Right(FileName, 4)) = ".mp3" Then
                ListMP3File Folder, FileName
            End If
        End If
        FileName = Dir
    Loop
    If Not Folders Is Nothing Then
        For Each Folder In Folders
            ListMP3Files Folder
        Next Folder
    End If
 
End Sub
 
Public Sub ListMP3File( _
       ByVal Folder As String, _
       ByVal FileName As String _
     )
    Static Row As Long
 
    If Row = 0 Then Row = 2
    ActiveSheet.Cells(Row, 1).Resize(1, 12).Value = _
    Array( _
    Folder, _
    FileName, _
    FileLen(Folder & FileName), _
    GetMP3Tag(Folder & FileName, Artist), _
    GetMP3Tag(Folder & FileName, AlbumTitle), _
    GetMP3Tag(Folder & FileName, SongTitle), _
    GetMP3Tag(Folder & FileName, RecordingYear), _
    GetMP3Tag(Folder & FileName, Genre), _
    GetMP3Tag(Folder & FileName, Duration), _
    GetMP3Tag(Folder & FileName, TrackNumber), _
    GetMP3Tag(Folder & FileName, BitRate), _
    GetMP3Tag(Folder & FileName, AlbumArtist))
    Row = Row + 1
 
End Sub
 
Public Function GetMP3Tag( _
       ByVal MP3FilePath As String, _
       ByVal MP3Tag As tMP3Tag _
     ) As String
 
' Return the requested MP3 tag from the specified MP3 file. Pass the fill path to the MP3
' file in the parameter MP3FilePath. Pass the requested MP3 tag ID in the parameter MP3
' tag. The MP3 tags that can be read are:
'
'   Artist = 16
'   AlbumTitle = 17
'   SongTitle = 10
'   TrackNumber = 19
'   RecordingYear = 18
'   Genre = 20
'   Duration = 21
'   BitRate = 22
 
    Static ShellApplication As Object
    Dim ShellFolder As Object
    Dim ShellFolderItem As Object
    Dim FilePath As String
    Dim FileName As String
 
    FilePath = Left(MP3FilePath, InStrRev(MP3FilePath, "\"))
    FileName = Right(MP3FilePath, Len(MP3FilePath) - InStrRev(MP3FilePath, "\"))
 
    Set ShellApplication = CreateObject("Shell.Application")
    Set ShellFolder = ShellApplication.Namespace(CStr(FilePath))
    Set ShellFolderItem = ShellFolder.ParseName(FileName)
    GetMP3Tag = ShellFolder.GetDetailsOf(ShellFolderItem, MP3Tag)
 
End Function

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
StaffordMartinAuthor Commented:
I am using C#.  Are you implicitly using any VB specific namespaces in this solution?  If not, I should be able to adapt the solution.

0
 
Dave BrettCommented:
The work is being done by the Shell Application - I'm not sure what this means for you in terms of C#
Cheers
Dave
 
0
 
StaffordMartinAuthor Commented:
Thanks all for your input.

I have solved the problem or READING the properties of various kinds of file types. But I have not sound a solution to the problem of EDITING those properties.

Any help in that regard would be greatly appreciated.


Regards
Stafford Marin
0
 
Dave BrettCommented:
As per my earlier post you would need to use a DLL as per the thread below
http://www.mrexcel.com/forum/showthread.php?t=322393  
Cheers
Dave
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now