Skip to content

Instantly share code, notes, and snippets.

@albedozero
Last active August 13, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albedozero/c74fa7411a9659ae3e27883fef34ef86 to your computer and use it in GitHub Desktop.
Save albedozero/c74fa7411a9659ae3e27883fef34ef86 to your computer and use it in GitHub Desktop.
A command-line python script that prints info about a midi file. Requires mido (https://pypi.org/project/mido/)
#!/usr/bin/env python3
from sys import argv
from mido import MidiFile
if len(argv) < 2:
exit("Usage: midifile [filename]")
mid = MidiFile(argv[1])
channels = set()
for track in mid.tracks:
for event in track:
if hasattr(event, 'channel'):
channels.add(event.channel)
midichannels = ', '.join([str(x + 1) for x in sorted(channels)])
length = None
if mid.type != 2:
length = mid.length
print(f"File: {mid.filename}")
print(f"Type: {mid.type}")
print(f"No. of tracks: {len(mid.tracks)}")
print(f"Used channels: {midichannels}")
if length != None:
print(f"Length: {length:.1f} seconds")
print(f"Ticks per beat: {mid.ticks_per_beat}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment