import mido
from pydub import AudioSegment
def midi_to_mp3(midi_path, mp3_path):
# Load MIDI file
midi = mido.MidiFile(midi_path)
# Create an empty list to store notes
notes = []
for track in midi.tracks:
# Iterate through MIDI messages in track
for msg in track:
# Check if message is a note on event
if msg.type == 'note_on':
# Add note (pitch, velocity, time) to notes list
notes.append((msg.note, msg.velocity, msg.time))
tempo = 120
duration = 1000
segments = []
for note in notes:
pitch, velocity, time = note
# Calculate duration of note based on tempo and time
note_duration = int(duration * (mido.tick2second(time, midi.ticks_per_beat, tempo) * 1000))
# Create audio segment for note
segment = AudioSegment.silent(duration=note_duration)
segments.append(segment)
audio = sum(segments)
audio.export(mp3_path, format="mp3")
midi_to_mp3("melody.mid", "output.mp3")
This is the full error message:
C:\Dipika\Study\musicrypt\env\Scripts\python.exe "C:/Program Files/JetBrains/PyCharm 2023.1.3/plugins/python/helpers/pydev/pydevd.py" --multiprocess --qt-support=auto --client 127.0.0.1 --port 56448 --file C:\Dipika\Study\musicrypt\musicrypt.py
Connected to pydev debugger (build 231.9161.41)
C:\Dipika\Study\musicrypt\env\Lib\site-packages\pydub\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
C:\Dipika\Study\musicrypt\env\Lib\site-packages\pydub\utils.py:198: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2023.1.3\plugins\python\helpers\pydev\pydevd.py", line 1496, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\JetBrains\PyCharm 2023.1.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:\Dipika\Study\musicrypt\musicrypt.py", line 70, in <module>
convert_to_mp3('melody.mid', 'audio.mp3')
File "C:\Dipika\Study\musicrypt\musicrypt.py", line 46, in convert_to_mp3
midi_audio = AudioSegment.from_file(midi_path, format="midi")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Dipika\Study\musicrypt\env\Lib\site-packages\pydub\audio_segment.py", line 728, in from_file
info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Dipika\Study\musicrypt\env\Lib\site-packages\pydub\utils.py", line 274, in mediainfo_json
res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\JetBrains\PyCharm 2023.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 585, in new_CreateProcess
return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified
python-BaseException
Process finished with exit code 1
I am trying to convert text to melody using music21 library which unfortunately gives mid file format as output. I am not able to play this mid file through any web broswer. That's why I tried to convert the mid file using mido and pydub libraries. But each time I run this my system gives an error "The system cannot find the file specified". I have my melody.mid file in the same path as the python file.
I was expecting to get a mp3 file in the end
You are getting this error because you have not installed the additional non-python dependencies for the pydub
library. pydub
relies on additional programs being installed on your system to handle the encoding/decoding of various audio file formats. As stated in the package installation instructions:
Installing pydub is easy, but don't forget to install ffmpeg/avlib (the next section in this doc)
From the Depenendencies section of the same documentation:
You can open and save WAV files with pure python. For opening and saving non-wav files – like mp3 – you'll need ffmpeg or libav.
There is an additional set of directions specifically for setting up ffmpeg, if needed. ffmpeg is the default library that pydub tries to use if it can't determine what library is installed.