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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示