东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2355|回复: 1

[课堂笔记] MacOS下通过HomeBrew安装ffmpeg与timidity实现将midi文件转换为mp3...

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14429
QQ
发表于 2019-5-30 10:34:05 | 显示全部楼层 |阅读模式


MacOS下通过HomeBrew安装ffmpeg与timidity实现将midi文件转换为mp3的Python代码


  1. import os
  2. import subprocess
  3. import pickle
  4. import glob
  5. from music21 import converter, instrument, note, chord, stream


  6. def convert_midi_to_mp3():
  7.     """
  8.     将神经网络生成的 MIDI 文件转成 MP3 文件
  9.     """
  10.     input_file = '1.mid'
  11.     output_file = 'output.mp3'

  12.     if not os.path.exists(input_file):
  13.         raise Exception("MIDI 文件 {} 不在此目录下,请确保此文件被正确生成".format(input_file))

  14.     print('将 {} 转换为 MP3'.format(input_file))

  15.     """
  16.     用 timidity 生成 mp3 文件
  17.     # 注意:Windows 用户可能需要用其他命令来将 MIDI 文件转为 MP3 文件
  18.     # 注意:Mac 用户貌似 libmp3lame 选项会出错,需要下载编译 libmp3lame,
  19.     # 再重新编译 ffmpeg :http://blog.csdn.net/qinggebuyao/article/details/20933497
  20.     # 不过,Mac 用户直接用自带的 GarageBand(车库乐队)可以直接播放生成的 MIDI 文件,比 ffmpeg 生成的 MP3 音色更好
  21.     """
  22.     command = 'timidity {} -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k {}'.format(input_file, output_file)
  23.     return_code = subprocess.call(command, shell=True)

  24.     if return_code != 0:
  25.         print('转换时出错,请查看出错信息')
  26.     else:
  27.         print('转换完毕. 生成的文件是 {}'.format(output_file))


  28. convert_midi_to_mp3()
复制代码




timidity on Mac OSX.png
Install ffmpeg on Mac OSX.png
brew list.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

96

帖子

198

积分

注册会员

Rank: 2

积分
198
发表于 2019-7-2 14:04:25 | 显示全部楼层
多谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-18 14:52 , Processed in 0.188086 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表