duanqiongdu9916 2009-04-21 20:44
浏览 49
已采纳

Python脚本化mp3数据库,带有php前端

So, here's the deal. I am attempting to write a quick python script that reads the basic id3 tags from an mp3 (artist, album, songname, genre, etc). The python script will use most likely the mutagen library (unless you know of a better one). I'm not sure how to recursively scan through a directory to get each mp3's tags, and then fill a database. Also, as far as the database end, I want to make it as solid as possible, so I was wondering if anyone had any ideas on how I should design the database itself. Should I just use one big table, should I use certain relationships, etc. I am not very good at relational databases so I would appreciate any help. Oh, this is running on a linux box.

  • 写回答

1条回答 默认 最新

  • dongmie3987067 2009-04-21 20:53
    关注

    To get started with extracting ID3 tags in Python, there's a module for that.

    from ID3 import ID3
    
    mp3_filepath = r'/music/song.mp3'
    id3_data = ID3(mp3_filepath)
    print 'Artist:', id3_data['ARTIST']
    print 'Title:', id3_data['TITLE']
    

    More info on ID3 module.

    If you want to recursively search a directory for mp3 files, the built-in os module can do that:

    import os
    
    def mp3_files(root):
      # this is a generator that will return mp3 file paths within given dir
      for f in os.listdir(root):
          fullpath = os.path.join(root,f)
          if os.path.isdir(fullpath) and not os.path.islink(fullpath):
              for x in mp3_files(fullpath):  # recurse into subdir
                  yield x
          else:
              if fullpath[len(fullpath)-3:] == 'mp3':
                yield fullpath
    
    for p in mp3_files(root_dir):
      id3_data = ID3(p)
      print 'Artist:', id3_data['ARTIST']
      print 'Title:', id3_data['TITLE']
    

    Reference.

    In terms of creating the database, you don't need to reinvent the wheel (storing music data is a common database problem) -- a Google search will help you out. Here's one example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)