A tutorial on writing Python scripts to get MP3 file tag information

Source: Internet
Author: User
Tags comments join python script in python


This article mainly introduces the writing Python script to obtain MP3 file tag information tutorial, the code is based on python2.x, the text of the comments in detail, the need for friends can refer to the



Here's how to learn python using a Python instance program. The purpose of this program is to analyze the tag information of all MP3 files and output it.



Import OS # importing OS modules, providing file paths, listing files, and other methods



Import SYS # imports the SYS module, using Sys.modules to get all the content in the module, similar to the reflection function



From userdict Import UserDict # This representation imports userdict from the UserDict class, similar to the import userdict.userdict in Java




"The processing function of an empty string replaces all 00 bytes with empty characters, and removes the empty strings before and after"
  # Strip in Python is used to remove the first and last characters of the string. Similarly, lstrip is used to remove the left character, and rstrip is used to remove the right character.
  return data.replace ("\ 00", "") .strip ()
  
class FileInfo (UserDict):
  '' 'File base class, store the file name of the file, inherited from UserDict (a class that stores key-value, you can override __setitem__, __getitem__ methods
  You can use []) '' '
  # self is used when it is defined, and it is not needed when it is used. If there is no parameter, the filename defaults to None. If there is one parameter, the parameter is filename
  def __init __ (self, filename = None):
    UserDict .__ init __ (self) # Initialize the parent class
    self ["name"] = filename # Set name to filaname
  
class MP3FileInfo (FileInfo):
  "Information class of MP3 files, used to analyze MP3 files and store information"
  # tagDataMap is used to store the location of the MP3 Tag information, (key: start position, end position, processing function),
  # stripnulls indicates the first defined function
  tagDataMap = {"title": (3, 33, stripnulls),
  "artist": (33, 63, stripnulls),
  "album": (63, 93, stripnulls),
  "year": (93, 97, stripnulls),
  "comment": (97, 126, stripnulls),
  "genre": (127, 128, ord)}
    
  def __parse (self, filename): # Parse MP3 files
    self.clear ()
    try:
      fsock = open (filename, "rb", 0) # open the file
      try:
        # Set the pointer position for file reading, seek the second parameter, 2 means to use the end of the file as a reference point,
        # -128 indicates that there is a point at the end of 128 bytes, 0 indicates the reference point at the beginning of the file, and 1 indicates the reference point at the current position
        fsock.seek (-128, 2)
        tagdata = fsock.read (128) # read 128 bytes of data
      finally:
        fsock.close () # Close the file. Note that in finally, the file handle needs to be closed when an error occurs
      if tagdata [: 3] == "TAG": # Determine whether it is a valid MP3 file with tags
        # Cyclically retrieve the location information of Tag information, such as 3, 33, stripnulls, and assign them to start, end, parseFunc
        for tag, (start, end, parseFunc) in self.tagDataMap.items ():
          # tagdata [start: end] Read the bytes from start to end, use parseFunc to process the content
          self [tag] = parseFunc (tagdata [start: end])
    except IOError: # If IOError occurs, skip to continue
      pass
    
  # Rewrite the __setitem__ method, the above self [tag] = parseFunc (tagdata [start: end]) will use this method,
  # key is tag, itme is parseFunc (tagdata [start: end])
  def __setitem __ (self, key, item):
    if key == "name" and item: # If key is name and item is not empty
      self .__ parse (item) # Parse MP3 files
      # problem here, should out of the if
      # FileInfo .__ setitem __ (self, key, item) If you use this indent, an error will occur
    # The previous error point, pay attention to the indentation here, anyway, the key-value will be stored, use the method of FileInfo .__ setitem__ parent class to store
    FileInfo .__ setitem __ (self, key, item)
        
def listDirectory (directory, fileExtList):
  "Get all files in the fileExtList format under the directory directory, fileExtList is a list, and can have multiple formats"
  fileList = [os.path.normcase (f)
    for f in os.listdir (directory)] # List all files in directory
  fileList = [os.path.join (directory, f)
    for f in fileList
    # Filter files to meet a format in fileExtList. os.path.splitext divides the file into file names and extensions
    if os.path.splitext (f) [1] in fileExtList]
      
  # sys.modules [FileInfo .__ module__] Get the FileInfo .__ module__ module, where FileInfo .__ module__ will be main here,
  # If it is called by another module, it is not, why is this not directly using "main"
  def getFileInfoClass (filename, module = sys.modules [FileInfo .__ module__]):
    "Define a function to get file information"
     # Get the class that needs to be parsed, if it is an mp3 file, the result is MP3FileInfo, the other is FileInfo
    subclass = "% sFileInfo"% os.path.splitext (filename) [1] .upper () [1:]
    # Return a class, note that what is returned is a "class". Use getattr to get the subclass class in the moudle module
    return hasattr (module, subclass) and getattr (module, subclass) or FileInfo
  # Note that this sentence may be more difficult to understand, why there are two (f) getFileInfoClass (f) (f)
  # Return a parsing class based on the file name, here is the MP3FileInfo, and the second (f) means to initialize MP3FileInfo (f) with f for this class
  return [getFileInfoClass (f) (f) for f in fileList]
  
if __name__ == "__main__": # main function, this code will not be allowed in other modules
  for info in listDirectory ("E: \\ Music", [".mp3"]): # loop to get information of all mp3 files in the E: \\ Music folder
    # Since MP3FileInfo inherits from FileInfo, FileInfo inherits from UserDict, this items () is to get the key-value collection.
    # Use "% s =% s" to format the output, and use "\ n" .join to join all the information with new lines.
    print "\ n" .join (["% s =% s"% (k, v) for k, v in info.items ()])
    print # After each file, output a blank line



The results are:




album = What Are Words-Single
 
comment = pythontab
 
name = E: \ Music \ chris medina-what_are_words.mp3
 
title = What Are Words
 
artist = Chris Medina
 
year = 2011
 
genre = 13
 
  
 
album = After the Wedding
 
comment = pythontab
 
name = E: \ Music \ two fathers.mp3
 
title = Two Fathers
 
artist = pythontab
 
year = 2010
 
genre = 255



Note: The logic is more, the code is not too small, do not understand more read comments


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.