How to Identify a Song Using Shazamio in a Virtual Environment

This guide walks you through setting up a Python virtual environment, installing the Shazamio library, and identifying a song from a recording.

Prerequisites

  • Python 3.7 or newer installed
  • A .mp3, .wav, or .m4a recording of the song you want to identify

Step 1: Create and Activate a Virtual Environment

Open a terminal (or command prompt) and run:

# Create a new folder for your project (optional)
mkdir shazam_project && cd shazam_project
 
# Create the virtual environment
python -m venv venv
 
# Activate it
source venv/bin/activate

Step 2: Install ShazamIO

Once the virtual environment is activated:

pip install shazamio

Step 3: Identify a Song from a Recording (Using a Command-Line Argument)

Create a Python script (e.g. identify_song.py) with the following content:

identify_song.py
import asyncio
import sys
from shazamio import Shazam
 
async def main(audio_path):
    shazam = Shazam()
    out = await shazam.recognize_song(audio_path)
    if "track" in out:
        print(f"Title: {out['track']['title']}")
        print(f"Artist: {out['track']['subtitle']}")
    else:
        print("Song could not be identified.")
 
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python identify_song.py <path_to_audio_file>")
        sys.exit(1)
    asyncio.run(main(sys.argv[1]))

To run the script, use:

python identify_song.py your_audio_file.mp3

Make sure to replace your_audio_file.mp3 with the actual file path.