Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect BPM using ffmpeg/soundstretch #1331

Open
jooola opened this issue Sep 7, 2021 · 5 comments
Open

Detect BPM using ffmpeg/soundstretch #1331

jooola opened this issue Sep 7, 2021 · 5 comments
Assignees
Labels
is: feature-request status: pinned This issue or pull request won't stale

Comments

@jooola
Copy link
Contributor

jooola commented Sep 7, 2021

The analyzer could analyze bpm for those who need it, I have a working script below:

soundstrech is based on the soundtouch library which seem broadly used.

We simply need to convert the audio files to pcm and we can analyze it using soundstretch.

I will propose a Draft PR soon with this new analyzer. But I will probably do some large changes to the analyzer so it won't land in v3.

#!/usr/bin/env python3

from subprocess import run
from tempfile import TemporaryDirectory
import re

from pathlib import Path

src = Path("python_apps/airtime_analyzer/tests/fixtures/s1.flac")


with TemporaryDirectory() as tmp:
    tmp = Path(tmp)
    tmp.mkdir(exist_ok=True)
    tmp_file = tmp / "file.wav"

    result = run(
        ["ffmpeg", "-i", src, "-acodec", "pcm_s16le", "-f", "wav", tmp_file],
        capture_output=True,
        universal_newlines=True,
        check=True,
    )
    result.check_returncode()

    result = run(
        ["soundstretch", tmp_file, "-bpm"],
        capture_output=True,
        universal_newlines=True,
        check=True,
    )

    track_bpm_match = re.search(
        r"Detected BPM rate ([0-9]+\.[0-9]+)",
        result.stderr,
    )

    if track_bpm_match:
        print(float(track_bpm_match.group(1)))

Ref: http://www.surina.net/soundtouch/

@zklosko
Copy link
Member

zklosko commented Sep 14, 2021

Two thoughts:

  1. The last stable release of Soundtouch came out almost three years ago. Does the project still have enough life left in it for a longer-aiming project like ours?
  2. Needing to convert a track to PCM just to analyze BPM doesn't seem like a worthwhile trade, especially since we're making broadcasting software and not DJ software. Some of our users (read: us) use Libretime for playout of full, 2 hour shows, which wouldn't have a consistent BPM to analyze, making the conversion a waste of computing resources on a live production server.

@jooola
Copy link
Contributor Author

jooola commented Sep 15, 2021

  1. The last stable release of Soundtouch came out almost three years ago. Does the project still have enough life left in it for a longer-aiming project like ours?

I am confident that the soundtouch library will be working for age, the project using it would not let it die (https://www.surina.net/soundtouch/applications.html). If the soundstretch binary is not the right tool for us, we could pick another one or create our own. But I don't see any problem at the moment.

  1. Needing to convert a track to PCM just to analyze BPM doesn't seem like a worthwhile trade, especially since we're making broadcasting software and not DJ software. Some of our users (read: us) use Libretime for playout of full, 2 hour shows, which wouldn't have a consistent BPM to analyze, making the conversion a waste of computing resources on a live production server.

I agree that getting BPM from a 2 hour show, or 1 hour audio mix doesn't make much sens, this is more useful when working with a lot of music tracks, so this could simply be limited to music tracks smaller than 15 minutes (should be configurable).

I am no expert, but I thought that bpm detection software would need to work with raw audio anyway ? We are are just seeing this step compared to other software.

@paddatrapper
Copy link
Contributor

I agree that getting BPM from a 2 hour show, or 1 hour audio mix doesn't make much sens, this is more useful when working with a lot of music tracks, so this could simply be limited to music tracks smaller than 15 minutes (should be configurable).

Or tracks uploaded as a specific track type (default to Music)

@jooola
Copy link
Contributor Author

jooola commented Sep 15, 2021

Also, ideally the bpm detection should be done while tagging the audio library before upload, so we could also skip track with existing BPM tags. But I think the feature is nice-to-have.

@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had activity in the last 5 months. It will be closed if no activity occurs in the next month.
Please chat to us on discourse or ask for help on our chat if you have any questions or need further support with getting this issue resolved.
You may also label an issue as pinned if you would like to make sure that it does not get closed by this bot.

@stale stale bot added the status: stalled This issue or pull request is stalled label Apr 16, 2022
@jooola jooola added the status: pinned This issue or pull request won't stale label Apr 16, 2022
@stale stale bot removed the status: stalled This issue or pull request is stalled label Apr 16, 2022
@jooola jooola self-assigned this Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is: feature-request status: pinned This issue or pull request won't stale
Projects
None yet
Development

No branches or pull requests

3 participants