modules.core.database

Audio Library Analyzer

A script that analyzes audio files in a directory and stores metadata in SQLite database.

Functions

analyze_directory(directory_path, db_path)

Analyze audio files in directory and store information in SQLite database.

create_database(db_path)

Create a new SQLite database with tables for music library.

extract_metadata(filepath)

Extract metadata from audio file using mutagen.

get_file_hash(filepath)

Generate a simple hash for the file based on path and size.

load_playlist_to_database(playlist_path, conn)

Load songs from M3U playlist and add to database.

main()

Main function to handle command line arguments and analyze directory.

scan_directory(directory_path, conn)

Scan directory for audio files and add to database.

modules.core.database.create_database(db_path)[source]

Create a new SQLite database with tables for music library.

Creates a comprehensive database schema based on Strawberry Music Player for storing music metadata, file information, and library structure.

Parameters:

db_path (str) – Path where the database file will be created.

Returns:

True if database created successfully, False otherwise.

Return type:

bool

modules.core.database.get_file_hash(filepath)[source]

Generate a simple hash for the file based on path and size.

Parameters:

filepath (str) – Path to the file to hash.

Returns:

MD5 hash based on file path, size, and modification time.

Return type:

str

modules.core.database.extract_metadata(filepath)[source]

Extract metadata from audio file using mutagen.

Parameters:

filepath (str) – Path to the audio file.

Returns:

Dictionary containing extracted metadata, or None if extraction fails.

Keys include: title, artist, album, albumartist, track, disc, year, originalyear, genre, composer, performer, grouping, comment, lyrics, length, bitrate, samplerate, bitdepth, compilation, art_embedded.

Return type:

dict or None

modules.core.database.scan_directory(directory_path, conn)[source]

Scan directory for audio files and add to database.

Recursively scans the specified directory for audio files, extracts metadata, and adds them to the database.

Parameters:
  • directory_path (str) – Path to the directory to scan.

  • conn (sqlite3.Connection) – Database connection object.

Returns:

(files_added, files_updated, errors) - counts of operation results.

Return type:

tuple

modules.core.database.load_playlist_to_database(playlist_path, conn)[source]

Load songs from M3U playlist and add to database.

Parameters:
  • playlist_path (str) – Path to the M3U playlist file.

  • conn (sqlite3.Connection) – Database connection object.

Returns:

True if playlist loaded successfully, False otherwise.

Return type:

bool

modules.core.database.analyze_directory(directory_path, db_path)[source]

Analyze audio files in directory and store information in SQLite database.

Parameters:
  • directory_path (str) – Path to the directory to analyze.

  • db_path (str) – Path to the SQLite database file.

Returns:

True if analysis completed successfully, False otherwise.

Return type:

bool

modules.core.database.main()[source]

Main function to handle command line arguments and analyze directory.

Parses command-line arguments and initiates database creation, directory scanning, or playlist loading operations.

Examples

Scan directory and create database:

python database.py /path/to/music

Scan directory with custom database path:

python database.py /path/to/music –db-path ~/music.db

Load playlist into database:

python database.py –playlist myplaylist.m3u –db-path ~/music.db

Scan with test directory:

python database.py ../../testing_files/