wma转mp3

女儿的英语作业音频文件是WMA格式,导入到iPad中无法播放。查了一下自己电脑上的转换工具,仅支持视频格式。于是撸了几行代码,进行格式转换。

  • 安装Pydub

    1
    pip install pydub
  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    import os
    import re
    from pydub import AudioSegment

    PATH = '/Users/xxx/Downloads/Book4'
    OUT = '/Users/xxx/Downloads/MP3'

    def ():
    files = os.listdir(PATH)
    if not os.path.isdir(OUT):
    os.mkdir(OUT)
    for file in files:
    filename, extenion = os.path.splitext(file)
    if extenion.lower() == '.wma':
    print('正在对文件<{}>进行MP3格式转换...'.format(file))

    temp = re.sub('D', '', filename)
    filename = filename.replace(temp, temp.zfill(2))
    inputfile = PATH + '/' + file
    outfile = OUT + '/' + filename + '.mp3'
    sound = AudioSegment.from_file(inputfile)

    if os.path.isfile(outfile):
    os.remove(outfile)
    sound.export(outfile, format="mp3", bitrate="192k")
    print('文件<{}>格式转换完成!'.format(file))

    if __name__ == '__main__':
    main()