62 lines
1.1 KiB
Python
62 lines
1.1 KiB
Python
#!/usr/bin/env python
|
|
|
|
import os
|
|
import sys
|
|
import vamp
|
|
import tempfile
|
|
import numpy as np
|
|
import subprocess as sp
|
|
|
|
from copyparty.util import fsenc
|
|
|
|
"""
|
|
dep: vamp
|
|
dep: beatroot-vamp
|
|
dep: ffmpeg
|
|
"""
|
|
|
|
|
|
def det(tf):
|
|
# fmt: off
|
|
sp.check_call([
|
|
"ffmpeg",
|
|
"-nostdin",
|
|
"-hide_banner",
|
|
"-v", "fatal",
|
|
"-ss", "13",
|
|
"-y", "-i", fsenc(sys.argv[1]),
|
|
"-ac", "1",
|
|
"-ar", "22050",
|
|
"-t", "300",
|
|
"-f", "f32le",
|
|
tf
|
|
])
|
|
# fmt: on
|
|
|
|
with open(tf, "rb") as f:
|
|
d = np.fromfile(f, dtype=np.float32)
|
|
c = vamp.collect(d, 22050, "beatroot-vamp:beatroot")
|
|
cl = c["list"]
|
|
|
|
# throws if detection failed:
|
|
bpm = float(cl[-1]["timestamp"] - cl[1]["timestamp"])
|
|
bpm = round(60 * ((len(cl) - 1) / bpm), 2)
|
|
print(f"{bpm:.2f}")
|
|
|
|
|
|
def main():
|
|
with tempfile.NamedTemporaryFile(suffix=".pcm", delete=False) as f:
|
|
f.write(b"h")
|
|
tf = f.name
|
|
|
|
try:
|
|
det(tf)
|
|
except:
|
|
pass
|
|
finally:
|
|
os.unlink(tf)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|