Author Topic: Converting MIDI to WAV and MP3 (BASH Script)  (Read 2932 times)

0 Members and 1 Guest are viewing this topic.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Converting MIDI to WAV and MP3 (BASH Script)
« on: December 19, 2021, 07:20:03 pm »
Awhile back I ran into issues where my MIDI file collection needed to be converted into an acceptable file format to be able to play it in QB64, as the only valid files are: WAV, OGG or MP3 file types.

So below is a BASH Shell script that converts a MIDI file into BOTH a .wav and a .mp3 file.

You will need to have installed:

Zenity - Used for the BASH GUI
timidity - To convert MIDI to WAV (Software synthesizer)
ffmpeg -  To convert timidity WAV to MP3 files.

Except for Zenity, timidity and ffmpeg I believe exists on all OS's, Since I only run Linux and macOS, I can't guarantee that this will run on Windows, but the principle behind what the script does should apply. I haven't completely tested it yet on my macOS, but I have installed the three requirements above. If I have any issues with it, I will post an update (not an edit). I know not all features of Zenity work on a macOS system. So I may create a command-line version.

Now I can convert all my Christmas MIDI files into WAV and MP3 files.

Merry Christmas.

Code: Bash: [Select]
  1. #!/bin/bash
  2. #
  3. # MIDI to WAV and MP3 Converter -- Shell Script -- George McGinn 2021
  4. #Version 1.0 -- December 19, 2021
  5. #
  6. # convertMIDI - Script to convert a MIDI file into both a .WAV and .MP3
  7. #               audio formats. Uses Zenity to retrieve the MIDI file,
  8. #               create the output files, show progress, and other messages.
  9. #
  10. # NOTES: This script needs the following installed:
  11. #        Zenity - Script's GUI
  12. #        timidity - Converts the MIDI to a WAV format
  13. #        ffmpeg - Converts a MIDI/WAV to MP3 format
  14. #
  15. # USEAGE:
  16. #   This script can be invoked from a File Manager or from a terminal
  17. #   session. When started, it defaults to your $HOME/Music directory.
  18. #   Select the location of your MIDI file. You will be able to change
  19. #   the directory. Select your MID or MIDI file then click [OK]. Next
  20. #   The output for the WAV and MP3 files will default to your $HOME/Music
  21. #   directory. You can change this location. Enter the file name WITHOUT
  22. #   any extensions. The .wav and .mp3 will be added to the file name
  23. #   you provide. The script will create both files.
  24. #
  25. #   If you select directory(ies) that require ROOT access, the script will
  26. #   ask you for your password and then do the conversions as stated above.
  27. #   There is no need to start this script with sudo.
  28. #
  29.  
  30. # Change directory to Home/Music
  31.  
  32. cd $HOME/Music
  33.  
  34. # Get the MIDI path/file name
  35. mid=$(zenity --file-selection)
  36. if [ $? = 1 ];
  37.         then exit
  38. fi
  39.  
  40.  
  41. # Get save path/file name
  42. wav=$(zenity --file-selection --save --confirm-overwrite)
  43. if [ $? = 1 ];
  44.         then exit
  45. fi
  46.  
  47. # see if current user has write permissions by creating an empty file
  48. > "$wav:".wav
  49. > "$wav".mp3
  50.  
  51. # if so, delete the empty files and do the conversion and show progress bar
  52. if [ $?  -eq 0 ]; then
  53.         rm "$wav:".wav
  54.         rm "$wav".mp3
  55.  
  56.         timidity "$mid" -Ow -o "$wav".wav  | zenity --progress --pulsate --auto-close --text "Converting to WAV..."
  57.         timidity "$mid" -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k "$wav".mp3 | zenity --progress --pulsate --auto-close --text "Converting to MP3..."
  58.  
  59. # Tell us the conversion is done
  60.         zenity --info --text "Conversion complete!"
  61.  
  62. # if not, get root password, run command as root
  63. else
  64. # Get the users password
  65.         passwd=$(zenity --password)
  66.  
  67. # Do the conversion and show a progress bar
  68.         echo $passwd|sudo -S timidity "$mid" -Ow -o "$wav".wav | zenity --progress --pulsate --auto-close --text "Converting to WAV..."
  69.         echo $passwd|sudo -S timidity "$mid" -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k "$wav".mp3 | zenity --progress --pulsate --auto-close --text "Converting to MP3..."
  70.  
  71.         if [ $? = 1 ];
  72.                 then exit
  73.         fi
  74.  
  75. # Tell us the conversion is done
  76.         zenity --info --text "Conversion complete!"
  77. fi
  78.  
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Converting MIDI to WAV and MP3 (BASH Script)
« Reply #1 on: December 20, 2021, 01:43:19 pm »
George,

Many thanks for the script. I have a squillion (not actually a squillion but lots...) midi files that cound make use of this script. Thank you. Your blood's worth bottling.

J

ps: Just tested the script and ran like a charm!!

Sample output:
 
 
(the 'wav' file is 30mb! - too large to attach)
* insideout.mid (Filesize: 8.51 KB, Downloads: 192)
« Last Edit: December 20, 2021, 02:19:34 pm by johnno56 »
Logic is the beginning of wisdom.