Skip to content

Instantly share code, notes, and snippets.

@mattdsteele
Last active September 19, 2025 14:05
Show Gist options
  • Select an option

  • Save mattdsteele/082fd77c3e65faa1332a36962c11da78 to your computer and use it in GitHub Desktop.

Select an option

Save mattdsteele/082fd77c3e65faa1332a36962c11da78 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# /// script
# dependencies = [
# "click",
# "deeprhythm"
# ]
# ///
import glob
import shutil
import os
import click
from deeprhythm import DeepRhythmPredictor
@click.command()
@click.option("--min-bpm", default = 170, prompt = "Minimum BPM")
@click.option("--max-bpm", default = 174, prompt = "Maximum BPM")
@click.option("--dir", prompt = "Input directory", multiple = True)
@click.option("--playlist-name", default = 'running-pl')
@click.option("--types", default = 'mp3,m4a')
@click.option("--confidence-threshold", default = 0.2)
def main(min_bpm, max_bpm, confidence_threshold, dir, types, playlist_name):
minimum_bpm = min_bpm / 2
maximum_bpm = max_bpm / 2
file_types = types.split(",")
paths = dir
model = DeepRhythmPredictor()
defs = []
def process(path: str, ext: str):
files = glob.glob(f'{path}/**/*.{ext}', recursive=True)
print(f'proccessing {path} ext {ext} ({len(files)})')
for file in files:
try:
tempo, confidence = model.predict(file, include_confidence=True)
defs.append({'tempo': tempo, 'confidence': confidence, 'file': file})
except Exception:
print(f'failed {file}')
# get BPMs
for path in paths:
for ext in file_types:
process(path, ext)
# filter for thresholds
below = list(filter(lambda x: x['tempo'] >= minimum_bpm, defs))
above = list(filter(lambda x: x['tempo'] <= maximum_bpm, below))
thresh = list(filter(lambda x: x['confidence'] >= confidence_threshold, above))
# copy files to directory
playlist_songs = []
playlist_songs.append('#EXTM3U')
os.mkdir(playlist_name)
for song in thresh:
path = song['file']
base = os.path.basename(path)
shutil.copy2(path, f'{playlist_name}/{base}')
playlist_songs.append(f'0:/MUSIC/{playlist_name}/{base}')
# make playlist
playlist_content = '\n'.join(playlist_songs)
with open(f'{playlist_name}/{playlist_name}.m3u8', 'w') as f:
f.write(playlist_content)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment