python批量修改mp3信息

# coding=utf-8

import os
import eyed3


def getMp3Files(parent):
    mp3Files = []

    sons = os.listdir(parent)

    if (isinstance(sons, list)):
        for n in sons:
            # print("{}  {}".format(n, os.sep.join((parent, n))))
            sonPath = os.sep.join((parent, n))

            if (os.path.isdir(sonPath)):
                grandChildren = getMp3Files(sonPath)
                # print("mepath {}, {}".format(sonPath, grandChildren))
                mp3Files.extend(grandChildren)
            elif os.path.isfile(sonPath) and len(sonPath) > 3:
                suffix = sonPath[len(sonPath)-4:]
                if suffix == ".mp3":
                    mp3Files.append(sonPath)
            else:
                pass

    return mp3Files


def dealOneMp3(path):
    audio = eyed3.load(path)
    title = audio.tag.title
    fn = os.path.basename(path)
    parentPath = os.path.dirname(path)
    parent = os.path.basename(parentPath)
    audio.tag.title = parent + "_" + fn
    audio.tag.save()

    mp3 = eyed3.load(path)
    print("{}  {} {} {}".format(path, title, fn, mp3.tag.title))


def dealAll(allPath):
    for p in allPath:
        dealOneMp3(p)


if __name__ == "__main__":
    all = getMp3Files(os.path.abspath("."))
    # print("\n".join(all))
    # print(len(all))

    dealAll(all)

y

 

 

用mutagen修改mp3歌曲名

# coding=utf-8

import os
import eyed3
from mutagen.id3 import ID3, TIT2, APIC, TPE1, TALB


def getMp3Files(parent):
    mp3Files = []

    sons = os.listdir(parent)

    if (isinstance(sons, list)):
        for n in sons:
            # print("{}  {}".format(n, os.sep.join((parent, n))))
            sonPath = os.sep.join((parent, n))

            if (os.path.isdir(sonPath)):
                grandChildren = getMp3Files(sonPath)
                # print("mepath {}, {}".format(sonPath, grandChildren))
                mp3Files.extend(grandChildren)
            elif os.path.isfile(sonPath) and len(sonPath) > 3:
                suffix = sonPath[len(sonPath)-4:]
                if suffix == ".mp3":
                    mp3Files.append(sonPath)
            else:
                pass

    return mp3Files


'''
description: 使用mutagen修改歌曲名,挺好用
param {type} 
return {type} void
'''
def dealMutagen(path):
    song = ID3(path)
    # song['TIT2'] = TIT2(
    #     encoding=3,
    #     text=info
    # )
    info = song["TIT2"]
    if (info):
        fn = os.path.basename(path)
        parentPath = os.path.dirname(path)
        pn = os.path.basename(parentPath)
        realName = pn + "-" + fn
        info.text = realName
    song.save()


'''
description: 使用eyed3修改歌曲名,不好用
param {type} 
return {type} void
'''
def dealOneMp3(path, count):

    index = "1000.mp3"

    try:
        audio = eyed3.load(path)
        if (audio.tag is None):
            audio.initTag()

        # print("tag " + audio.tag.title)

        title = audio.tag.title
        fn = os.path.basename(path)
        # parentPath = os.path.dirname(path)
        # parent = os.path.basename(parentPath)
        audio.tag.title = fn
        # audio.tag.title = parent + "_" + fn
        # print("{} {} {}".format(path, parent, fn))
        isError = "空白"
        audio.tag.save()
    except BaseException as identifier:
        print("BaseException ", identifier)
        isError = "错了"
    else:
        isError = "正确"
    finally:
        mp3 = eyed3.load(path)
        title = "none"
        if (not (mp3.tag is None)):
            title = mp3.tag.title

        if (title > index):
            print("{}  {} {} {} {}".format(
                path, title, fn, count, isError))


def dealAll(allPath):
    count = 0
    for p in allPath:
        print("开始 {} {}".format(p, count))
        # dealOneMp3(p, count)
        dealMutagen(p)
        count += 1


if __name__ == "__main__":
    all = getMp3Files(os.path.abspath("."))
    # print("\n".join(all))
    # print(len(all))

    dealAll(all)

升级版,用mutagen

# coding=utf-8

import os
import eyed3
import mutagen
from mutagen.id3 import ID3, TIT2
from mutagen.easyid3 import EasyID3


'''
description: 使用mutagen修改歌曲名,挺好用
param {type} 
return {type} void
'''


def dealMutagen(path):
    fn = os.path.basename(path)
    parentPath = os.path.dirname(path)
    pn = os.path.basename(parentPath)
    realName = pn + "-" + fn

    try:
        meta = EasyID3(path)
    except mutagen.id3.ID3NoHeaderError:
        meta = mutagen.File(path, easy=True)
        meta.add_tags()
        meta.save()

    song = ID3(path)
    # song['TIT2'] = TIT2(
    #     encoding=3,
    #     text=info
    # )

    song["TIT2"] = TIT2(encoding=3, text=realName)
    song.save()


def getMp3Files(parent):
    mp3Files = []

    sons = os.listdir(parent)

    if (isinstance(sons, list)):
        for n in sons:
            # print("{}  {}".format(n, os.sep.join((parent, n))))
            sonPath = os.sep.join((parent, n))

            if (os.path.isdir(sonPath)):
                grandChildren = getMp3Files(sonPath)
                # print("mepath {}, {}".format(sonPath, grandChildren))
                mp3Files.extend(grandChildren)
            elif os.path.isfile(sonPath) and len(sonPath) > 3:
                suffix = sonPath[len(sonPath)-4:]
                if suffix == ".mp3":
                    mp3Files.append(sonPath)
            else:
                pass

    return mp3Files


'''
description: 使用eyed3修改歌曲名,不好用
param {type} 
return {type} void
'''


def dealOneMp3(path, count):

    index = "1000.mp3"

    try:
        audio = eyed3.load(path)
        if (audio.tag is None):
            audio.initTag()

        # print("tag " + audio.tag.title)

        title = audio.tag.title
        fn = os.path.basename(path)
        # parentPath = os.path.dirname(path)
        # parent = os.path.basename(parentPath)
        audio.tag.title = fn
        # audio.tag.title = parent + "_" + fn
        # print("{} {} {}".format(path, parent, fn))
        isError = "空白"
        audio.tag.save()
    except BaseException as identifier:
        print("BaseException ", identifier)
        isError = "错了"
    else:
        isError = "正确"
    finally:
        mp3 = eyed3.load(path)
        title = "none"
        if (not (mp3.tag is None)):
            title = mp3.tag.title

        if (title > index):
            print("{}  {} {} {} {}".format(
                path, title, fn, count, isError))


def dealAll(allPath):
    count = 0
    for p in allPath:
        print("开始 {} {}".format(p, count))
        # dealOneMp3(p, count)
        dealMutagen(p)
        count += 1


if __name__ == "__main__":
    all = getMp3Files(os.path.abspath("."))
    # print("\n".join(all))
    # print(len(all))

    dealAll(all)

批量修改mp3信息第三版:

# coding=utf-8
import sys
import os
import eyed3
import mutagen
from mutagen.id3 import ID3, TIT2
from mutagen.easyid3 import EasyID3


'''
description: 使用mutagen修改歌曲名,挺好用
param {type} 
return {type} void
'''


def dealMutagen(path):
    fn = os.path.basename(path)
    parentPath = os.path.dirname(path)
    pn = os.path.basename(parentPath)
    realName = pn + "-" + fn

    try:
        meta = EasyID3(path)
    except mutagen.id3.ID3NoHeaderError:
        meta = mutagen.File(path, easy=True)
        meta.add_tags()
        meta.save()

    song = ID3(path)
    # song['TIT2'] = TIT2(
    #     encoding=3,
    #     text=info
    # )

    song["TIT2"] = TIT2(encoding=3, text=realName)
    song.save()


def getMp3Files(parent):
    mp3Files = []

    sons = os.listdir(parent)

    if (isinstance(sons, list)):
        for n in sons:
            # print("{}  {}".format(n, os.sep.join((parent, n))))
            sonPath = os.sep.join((parent, n))

            if (os.path.isdir(sonPath)):
                grandChildren = getMp3Files(sonPath)
                # print("mepath {}, {}".format(sonPath, grandChildren))
                mp3Files.extend(grandChildren)
            elif os.path.isfile(sonPath) and len(sonPath) > 3:
                suffix = sonPath[len(sonPath)-4:]
                if suffix == ".mp3":
                    mp3Files.append(sonPath)
            else:
                pass

    return mp3Files


'''
description: 使用eyed3修改歌曲名,不好用
param {type} 
return {type} void
'''


def dealOneMp3(path, count):

    index = "1000.mp3"

    try:
        audio = eyed3.load(path)
        if (audio.tag is None):
            audio.initTag()

        # print("tag " + audio.tag.title)

        title = audio.tag.title
        fn = os.path.basename(path)
        # parentPath = os.path.dirname(path)
        # parent = os.path.basename(parentPath)
        audio.tag.title = fn
        # audio.tag.title = parent + "_" + fn
        # print("{} {} {}".format(path, parent, fn))
        isError = "空白"
        audio.tag.save()
    except BaseException as identifier:
        print("BaseException ", identifier)
        isError = "错了"
    else:
        isError = "正确"
    finally:
        mp3 = eyed3.load(path)
        title = "none"
        if (not (mp3.tag is None)):
            title = mp3.tag.title

        if (title > index):
            print("{}  {} {} {} {}".format(
                path, title, fn, count, isError))


def dealAll(allPath):
    count = 0
    for p in allPath:
        print("开始 {} {}".format(p, count))
        # dealOneMp3(p, count)
        dealMutagen(p)
        count += 1


def renameAll(root):
    newMp3Paths = []
    oldAll = getMp3Files(root)

    for p in oldAll:
        wanSuffix = "(完).mp3"
        dp = os.path.dirname(p)
        fn = os.path.basename(p)
        if fn.endswith(wanSuffix):
            try:
                newFn = fn[0:len(fn)-len(wanSuffix)] + ".mp3"
                newPath = os.path.sep.join((dp, newFn))
                os.rename(p, newPath)
            except Exception as e:
                print(e)
                sys.exit(1)
            else:
                pass
    
    newAll = getMp3Files(root)
    for p in newAll:
        dp = os.path.dirname(p)
        fn = os.path.basename(p)
        oldPrefix = fn[0:len(fn)-len(".mp3")]
        newName = oldPrefix.zfill(4) + ".mp3"
        newPath = os.path.sep.join((dp, newName))
        try:
            os.rename(p, newPath)
        except Exception as e:
            print(e)
            newMp3Paths.append(p)
        else:
            newMp3Paths.append(newPath)

    newMp3Paths.sort(reverse=False)
    return newMp3Paths


if __name__ == "__main__":
    all = renameAll(os.path.abspath("."))
    # print("\n".join(all))
    # print(len(all))

    dealAll(all)

 

    原文作者:水星灭绝
    原文地址: https://blog.csdn.net/wulong710/article/details/108035809
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞