shazamio / ShazamIO

🎵 Is a free asynchronous library from reverse engineered Shazam API written in Python 3.8+ with asyncio and aiohttp.
MIT License
449 stars 64 forks source link

Call recognize on AudioSegment #103

Open Andrew3019 opened 3 months ago

Andrew3019 commented 3 months ago

Hello, it seems after the update from recognize_song to recognize, you can no longer pass an AudioSegment as an argument. Is there any workaround for this? I used AudioSegment to separate different tracks in an audio file with many different songs in it.

I have tried out = await shazam.recognize(segment.raw_data) which should be type "bytes" but this error occurs: signature = await self.core_recognizer.recognize_bytes(data) SignatureError: FFmpeg not found or failed to convert audio

dotX12 commented 2 months ago
@pytest.mark.asyncio
async def test_recognize_audio_segment_cut_half(song_bytes: bytes):
    audio_data = BytesIO(song_bytes)
    audio_segment = AudioSegment.from_file(audio_data, format="ogg")
    half_duration = len(audio_segment) // 2
    half_audio = audio_segment[:half_duration]

    buffer = half_audio.export(format="wav")
    audio_bytes = buffer.read()

    shazam = Shazam()
    out = await shazam.recognize(data=audio_bytes)

    assert out.get("matches") != []
    assert out["track"]["key"] == "53982678"

@Andrew3019,

obj.raw_data doesn't work for me, but it works great

buffer = half_audio.export(format="wav")
audio_bytes = buffer.read()
============================================================================== test session starts ==============================================================================
platform win32 -- Python 3.11.4, pytest-8.1.2, pluggy-1.5.0
rootdir: C:\Users\ecelo\PycharmProjects\ShazamIO
configfile: pyproject.toml
plugins: anyio-4.3.0, asyncio-0.23.6
asyncio: mode=Mode.AUTO
collected 8 items                                                                                                                                                                

tests\test_21_issue.py .
tests\test_about_artist.py .
tests\test_get_next_signature.py ..
tests\test_peak_spreading_numpy.py .
tests\test_recognize.py ...

=============================================================================== 8 passed in 5.12s ===============================================================================