1 Star 0 Fork 1

闫安华仔/music-converter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
music_converter.py 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
YanAnHuaZai 提交于 2023-08-08 22:39 . feat: 音乐文件格式转换
import os
from pydub import AudioSegment
def convert_wav(filepath, save_path, convert_to):
sourcefile = AudioSegment.from_wav(filepath)
filename = filepath.split('\\')[-1].split('.wav')[0] + '.' + convert_to
print(filename)
sourcefile.export(save_path + filename, format=convert_to)
def convert_m4a(filepath, save_path, convert_to):
sourcefile = AudioSegment.from_file(filepath)
filename = filepath.split('\\')[-1].split('.m4a')[0] + '.' + convert_to
print(filename)
sourcefile.export(save_path + filename, format=convert_to)
def convert_mp3(filepath, save_path, convert_to):
sourcefile = AudioSegment.from_mp3(filepath)
filename = filepath.split('\\')[-1].split('.mp3')[0] + '.' + convert_to
print(filename)
sourcefile.export(save_path + filename, format=convert_to)
def convert_ogg(filepath, save_path, convert_to):
sourcefile = AudioSegment.from_ogg(filepath)
filename = filepath.split('\\')[-1].split('.ogg')[0] + '.' + convert_to
print(filename)
sourcefile.export(save_path + filename, format=convert_to)
def convert_flac(filepath, save_path, convert_to):
sourcefile = AudioSegment.from_file(filepath)
filename = filepath.split('\\')[-1].split('.flac')[0] + '.' + convert_to
print(filename)
sourcefile.export(save_path + filename, format=convert_to)
def convert_any(filepath, save_path, source_from, convert_to):
sourcefile = AudioSegment.from_file(filepath)
filename = filepath.split('\\')[-1].split(f'.{source_from}')[0] + '.' + convert_to
print(filename)
sourcefile.export(save_path + filename, format=convert_to)
def get_file_name(filepath, file_suffix):
file_list = []
for root, dirs, files in os.walk(filepath):
for file in files:
if file_suffix in file.split('.')[-1]:
file_list.append(os.path.join(root, file))
return file_list
if __name__ == '__main__':
source_path = 'C:\\Users\\HuaZai\\Workspace\\Code\\Tool\\music-converter\\data\\m4a'
convert_path = 'C:\\Users\\HuaZai\\Workspace\\Code\\Tool\\music-converter\\data\\m4a\\convert\\'
wav_list = get_file_name(source_path, 'm4a')
for item in wav_list:
convert_m4a(item, convert_path, 'mp3')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/YanAnHuaZai/music-converter.git
git@gitee.com:YanAnHuaZai/music-converter.git
YanAnHuaZai
music-converter
music-converter
master

搜索帮助