音频类型转换工具-可执行文件exe/dmg制作

朋友车载音乐需要MP3格式,想要个批量转换工具

准备工作

  1. brew install ffmpeg --HEAD或者官网下载安装ffmpeg并配置环境
  2. conda install ffmpeg 或者pip install ffmpeg-python

音频类型转换程序.py文件

exe文件在windows下打包,dmg在macos下打包,需要更换文件路径\\为/

# windows
# 获取文件夹中所有的文件名
def getFiles(path):
    import os
    filenames = os.listdir(path)
    return filenames

def trans_video_to_mp3(filePath,output_name,newType='mp3'):
    from pydub import AudioSegment
    # 读取数据文件
    song = AudioSegment.from_file(filePath)
    # 输出转换文件    
    song.export(output_name+'.'+str(newType), format=str(newType))
    song = None
    
# 转换音频
def changeVideo(dataPath,newType=False):
    print('-------Start------')    
    import os
               
    # 如果是文件夹,则执行以下操作
    if os.path.isdir(dataPath):
        outputDir = r"" + dataPath + '\\..\\' + '输出'            
        # 如果输出文件夹不存在,则创建
        if not os.path.exists(outputDir):
            os.makedirs(outputDir) 
        print('默认跳过.开头隐藏文件')    
        # 遍历文件夹中所有的文件,执行转换操作
        for file_name in getFiles(dataPath):
            
            if not file_name[0]=='.':
                # 生成文件路径
                filePath = r"" + dataPath + '\\' + file_name
                print('源文件路径', filePath)
                # 输出文件名    
                output_name = r"" + outputDir + '\\' + file_name
                print('输出文件路径', output_name)
                # 将数据文件转换到输出文件
                trans_video_to_mp3(filePath, output_name,newType)
    # 如果是一个文件则直接转换
    else:
        print('单个文件输出到同级目录下')
        output_name = os.path.splitext(dataPath)[0]         
        # 将数据文件转换到输出文件夹
        print('输出文件路径为 ', output_name+'.'+str(newType))
        trans_video_to_mp3(dataPath,output_name,newType) 
            
    print('-----The  End-----')


if __name__ == '__main__':       
    print('请将文件或目录地址输入控制台,然后回车')
    path = input("Enter your path: ")
    print('音频要转换为什么格式?')
    newType = input('Enter new type: ')
    changeVideo(path, newType if newType else 'mp3')

生成可执行文件

# 安装pipenv
pip install pipenv

#建立虚拟环境
pipenv install
#进入虚拟环境(上一步可省略,因为没有虚拟环境的话会自动建立一个)
pipenv shell
# 随时查看安装的包
pip list
#安装模块
pip install pydub 
#打包的模块也要安装,否则会调用原有的库依旧会关联很多无用库
pip install pyinstaller
# 打包
pyinstaller -F .py文件路径 --distpath 输出文件夹
# 退出虚拟环境
exit
# 删除虚拟环境
pipenv ––rm

  • 可以转换常见格式如:mp4、mp3、wav、flac等
  • 无法解码转换网易云QQ音乐下载的ncm、mgg等加密格式(um库被删了可以找找,bushi)
  • 视频转换后只有音频
    使用方式

相关文章:

  1. 【最新】windows电脑FFmpeg安装教程手把手详解
  2. 解决pyinstaller打包exe文件过大的问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值