-
-
Save nomadicloudz/ea909706b36e86462d2d97439e594c13 to your computer and use it in GitHub Desktop.
Kdenlive beat sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import librosa # you need librosa to get this working | |
import datetime | |
def parse_delta(s): | |
date = datetime.datetime.strptime(s, "%H:%M:%S") | |
return datetime.timedelta(hours=date.hour, minutes=date.minute, seconds=date.second) | |
def main(): | |
parser = argparse.ArgumentParser( | |
prog='Kdenlive beatfinder', | |
description='Creates timeline guides matching the song beat') | |
parser.add_argument('input_file') | |
parser.add_argument('--output_file', required=False, default="beats.txt") | |
parser.add_argument("--bpm", type=float, default=None) | |
parser.add_argument("--offset", default="0:00:0") | |
parser.add_argument("--tightness", type=float, default=100) | |
args = parser.parse_args() | |
offset = parse_delta(args.offset) | |
y, sr = librosa.load(args.input_file) | |
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr, bpm=args.bpm, tightness=args.tightness) | |
beat_times = librosa.frames_to_time(beat_frames, sr=sr) | |
print(f"{args.input_file} BPM: {tempo:.2f}") | |
print(f"Writing timeline guides to {args.output_file}...") | |
with open(args.output_file, "wt") as f: | |
for i, seconds in enumerate(beat_times): | |
f.write(f"{str(datetime.timedelta(seconds=seconds) + offset)} {i}\n") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment