Skip to content

Instantly share code, notes, and snippets.

@raphsilva
Last active October 11, 2024 11:09
Show Gist options
  • Save raphsilva/01cb94b67e104b41d51eb5bba9b66105 to your computer and use it in GitHub Desktop.
Save raphsilva/01cb94b67e104b41d51eb5bba9b66105 to your computer and use it in GitHub Desktop.
Try this if you have errors opening a MIDI file, such as 'Corrupt MIDI file' or 'no MTrk header at start of track'. I have tried this on some old mobile phone generated MIDI (Sony Ericsson Music DJ).
# github.com/raphsilva, October 2020
# Have all your MIDI files in a 'files' folder and run this script from one level above that folder.
# The fixed files will be in the same 'files' folder, renamed with a '_f' suffix.
# MAKE A BACKUP OF YOUR FILES BEFORE RUNNING!
import os
for filepath in os.listdir('files'):
filename = filepath.replace('.mid', '')
print(filename)
file = open(f'files/{filename}.mid', 'r+b').read()
# This will remove the SEM1 part of the file, which seems to be the cause of the problem.
a = file.find(b'SEM1')
b = file.find(b'MTrk')
rem = file[a:b]
file = file.replace(rem, b'')
t = open(f'files/{filename}_f.mid', 'wb')
t.write(file)
t.close()
@Maverynthia
Copy link

This seemed to snip off a little bit of the beginning, but made the SEM1 midi play just fine. Thanks!

@raphsilva
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment