【问题标题】:Want to remove illegal tags from mp3 file using Python想要使用 Python 从 mp3 文件中删除非法标签
【发布时间】:2010-11-26 09:07:30
【问题描述】:

如何在我的 mp3 文件中实现这一点。

艺术家:www.xyz.com ----> 艺术家:
艺术家:免费 下载,免费音乐,xyzhi.com ----> 艺术家:
艺术家:Kurukuru Kan (Amma Na) - www.musicxyx.com - ® Danaa 收藏品® ----> 艺术家: Kurukuru Kan (Amma Na)
艺术家: Nan Pogiren - - ® Danna 系列 ® ----> 艺术家:南 波吉伦

我一直在使用 Mutagen 访问 ID3 标签。如何操作标签中的字符串来实现以上功能?

【问题讨论】:

  • 您怎么知道Kurukuru Kan 是合法名称而free downloads 不是?
  • 我认为字符串没有区别,只是想实用。试图删除无用的标签值。
  • 请提供您想要制作的映射的明确表格?

标签: python regex string mp3


【解决方案1】:

首先你需要一个库来理解 MP3 格式并允许你编辑标签,可能是:http://id3-py.sourceforge.net/

除此之外,您只需要处理字符串替换。

对于您指定的那些(包括奇怪的空间要求):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
EXPECTED = {
'Artist:www.xyz.com':'Artist:',
'Artist:free downloads,free music,xyzhi.com':'Artist:',
'Artist:Kurukuru Kan (Amma Na) - www.musicxyx.com - ® Danaa collections ®':'Artist: Kurukuru Kan (Amma Na)',
'Artist: Nan Pogiren - - ® Danna collections ®':'Artist:Nan Pogiren'}

import re

def process(instr):
    assert instr.startswith("Artist:")
    mo = re.match(r"^(Artist:)( ?)(.*?) - .*$",instr)
    if mo:
        spc = mo.group(2)
        if spc == " ":
            spc = ""
        else:
            spc = " "

        return "Artist:"+spc+mo.group(3)
    return "Artist:"

for (instr,outstr) in EXPECTED.iteritems():
    print process(instr),outstr,process(instr) == outstr
    assert process(instr) == outstr

【讨论】:

  • 我一直在使用 Mutagen ID3 Python 库,并且我能够获取没有附加任何艺术家或专辑之类的字符串。上述脚本中的哪些更改将使我能够在原始字符串和预期字符串中没有“艺术家:”的情况下执行上述确切操作。
  • @Arun - 你为什么在示例中包含 Artist: 呢?
猜你喜欢
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
相关资源
最近更新 更多