As I mentioned in previous posts on how to convert MIDI files to WAV in Windows using VLC, I had to find a way to convert them as well in Ubuntu. After some research, I ended up using timidity to play the MIDI file and FFmpeg to store the output stream into an MP3 file using the command line.
In this article, I will explain to you how to easily convert MIDI files to MP3 in your Ubuntu 20.04 desktop and server using Timidity to play the midi files and FFMPEG to pipe it into an MP3 file.
1. Install timidity
TiMidity++ is a CLI utility that converts some of MIDI files (supported formats: Standard MIDI files (*.mid), Recomposer files (*.rcp, *.r36, *.g18, *.g36) and Module files (*.mod)) into formatted audio files (e.g. RIFF WAVE). TiMidity++ uses Gravis Ultrasound-compatible patch files or Soundfonts (*.sfx, *.sf2) to generate digital audio data from MIDI files. The digital audio data generated by TiMidity++ can be stored in a file for processing, or played in real-time through an audio device. In real-time playing, TiMidity++ can show the lyrics contained in KAR or WRD files. So as you can see, it cannot export to our desired format MP3, however, we can manipulate the real-time playing to store it into an MP3 file using FFMPEG.
Before continuing with the installation, update the apt repositories:
And then install Timidity with the following command:
The installation of this package will take about 150MB of space on your disk. Once it finishes you should be able to use timidity from the command line.
2. Install FFmpeg
As next, install FFmpeg, the suite of libraries and programs for handling video, audio, and other multimedia files and streams. The following command should do the trick if you don't have it installed already:
The installation of this package will take about 260MB of space on your disk. For more information and steps to install in other operative systems, please visit the official downloads page of the FFmpeg website here.
3. Converting MIDI to MP3
Finally, now that you have the 2 required libraries to make this work, proceed with the conversion using the following command. It works like this, timidity will play the provided MIDI file as the first positional argument and the -Ow
option will generate a RIFF WAVE format output. Then, we will pipe the output to FFMPEG that will handle the stream, and store it into an MP3 file:
Replace input_file.mid
with the absolute or relative path to the MIDI file in your system that you want to convert to MP3 and replace output_file.mp3
with the absolute or relative path to the MP3 file that will contain the converted audio.
This would generate an output similar to the following one in the terminal:
And of course, would generate a new MP3 file in the desired destination with the specified filename that you can now play on any device that supports this format.
Happy coding ❤️!
0 Comments