modules.core.playlist

Playlist Manager

A script that creates and manages M3U playlists from the audio library database.

Functions

connect_to_database(db_path)

Connect to the SQLite database and return connection.

create_m3u_playlist(songs, playlist_path[, ...])

Create M3U playlist file from a list of songs.

create_playlist_from_inputs(inputs, ...[, ...])

Create playlist from list of files and folders.

extract_metadata(file_path)

Extract metadata from audio file using mutagen.

format_song_info(song)

Format song information for display.

get_relative_path(file_path, playlist_path)

Convert file path to relative path from playlist location.

get_songs_from_database(conn[, filters])

Get songs from database based on filters.

load_m3u_playlist(playlist_path)

Load songs from M3U playlist file.

main()

Main function for playlist management command-line interface.

scan_directory(directory_path)

Scan directory recursively for audio files.

modules.core.playlist.connect_to_database(db_path)[source]

Connect to the SQLite database and return connection.

Parameters:

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

Returns:

Database connection object, or None if connection fails.

Return type:

sqlite3.Connection or None

modules.core.playlist.get_songs_from_database(conn, filters=None)[source]

Get songs from database based on filters.

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

  • filters (dict, optional) – Dictionary with filter criteria. Supported keys: ‘artist’, ‘album’, ‘genre’ (all use partial matching).

Returns:

List of song records as sqlite3.Row objects.

Return type:

list

modules.core.playlist.format_song_info(song)[source]

Format song information for display.

Parameters:

song (dict or sqlite3.Row) – Song record with metadata.

Returns:

Formatted song information string with track number, artist, title, album, and duration.

Return type:

str

modules.core.playlist.get_relative_path(file_path, playlist_path)[source]

Convert file path to relative path from playlist location.

Parameters:
  • file_path (str) – Path to the audio file.

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

Returns:

Relative path from playlist directory to audio file.

Return type:

str

modules.core.playlist.create_m3u_playlist(songs, playlist_path, use_absolute_paths=False, playlist_name='Playlist')[source]

Create M3U playlist file from a list of songs.

Parameters:
  • songs (list) – List of song dictionaries or database records.

  • playlist_path (str) – Path where the playlist file will be saved.

  • use_absolute_paths (bool, optional) – Use absolute paths instead of relative. Defaults to False.

  • playlist_name (str, optional) – Name of the playlist. Defaults to “Playlist”.

Returns:

True if playlist created successfully, False otherwise.

Return type:

bool

modules.core.playlist.load_m3u_playlist(playlist_path)[source]

Load songs from M3U playlist file.

Parameters:

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

Returns:

List of song dictionaries parsed from the playlist.

Return type:

list

modules.core.playlist.extract_metadata(file_path)[source]

Extract metadata from audio file using mutagen.

Parameters:

file_path (str) – Path to the audio file.

Returns:

Dictionary containing extracted metadata, or None if extraction fails.

Keys include: url, title, artist, album, albumartist, length, track, disc, year, genre.

Return type:

dict or None

modules.core.playlist.scan_directory(directory_path)[source]

Scan directory recursively for audio files.

Parameters:

directory_path (str) – Path to the directory to scan.

Returns:

List of audio file paths found in the directory.

Return type:

list

modules.core.playlist.create_playlist_from_inputs(inputs, playlist_path, use_absolute_paths=False, playlist_name='Playlist')[source]

Create playlist from list of files and folders.

Parameters:
  • inputs (list) – List of file paths and/or directory paths.

  • playlist_path (str) – Path where the playlist file will be saved.

  • use_absolute_paths (bool, optional) – Use absolute paths instead of relative. Defaults to False.

  • playlist_name (str, optional) – Name of the playlist. Defaults to “Playlist”.

Returns:

True if playlist created successfully, False otherwise.

Return type:

bool

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

Main function for playlist management command-line interface.

Parses command-line arguments and performs playlist operations including creating playlists from database queries, files/directories, or loading existing playlists.

Examples

Create playlist from database with artist filter:

python playlist.py –name “My Playlist” –artist “Pink Floyd” –output playlists/

Create playlist from files and directories:

python playlist.py –name “Files” –inputs song1.mp3 song2.flac /path/to/music/

Create playlist from input file:

python playlist.py –name “From File” –input-file mylist.txt –output playlists/

Load and display existing playlist:

python playlist.py –load existing_playlist.m3u