Skip to main content

nextsong is a library and command line executable to support creating media playlists with a complex nested structure.

Note: This project is theoretically platform agnostic, but isn't tested outside of Linux. If you encounter a problem using it on another platform please feel free to open an issue.

Features

  • Recursive tree-based structure, where each item in the playlist is itself a playlist with various options for sampling songs
  • XML format to save and load playlists
  • Command-line executable to get the next song in the playlist
  • ezstream integration

Usage

Basic example

First create a playlist and save it to an XML file:

from nextsong import Playlist

Playlist(
    "my_favorite_song.mp3",
    "artist1/album1/*.mp3",
    loop=True,
).save_xml()

This creates a file named nextsong.xml describing the playlist:

<nextsong>
  <meta/>
  <playlist loop="true">
    <path>my_favorite_song.mp3</path>
    <path>artist1/album1/*.mp3</path>
  </playlist>
</nextsong>

After creating the XML file, invoke nextsong from the command line to get the next track in the playlist

$ nextsong
/home/myusername/media/music/my_favorite_song.mp3
$ nextsong
/home/myusername/media/music/artist1/album1/01_hello_world.mp3
$ nextsong
/home/myusername/media/music/artist1/album1/02_foobar.mp3

The nextsong command will print the absolute path of the next track to standard output or print an empty line once the end of the playlist has been reached. In this example, the playlist is set to loop, so it will never end. The state of iteration through the playlist is maintained in a pickle file named state.pickle. Note that changes to the playlist XML file will only take effect by deleting this pickle file.

The filepaths in this example and other options such as the root media directory can be configured within the Python script or via environment variables.

Ezstream integration

First create the playlist XML file using this package as described above.

To update the ezstream XML file see the ezstream man page for the most fleshed out and up to date details. You need to create a program intake that runs nextsong. Overall your intakes element should look something like this:

<intakes>
  <intake>
    <type>program</type>
    <filename>nextsong</filename>
  </intake>
</instakes>

When running nextsong through ezstream you can use environment variables to adjust the configuration. For example, to set nextsong's media_root config, run ezstream with NEXTSONG_MEDIA_ROOT set to the desired value

$ NEXTSONG_MEDIA_ROOT=~/music ezstream -c ~/ezstream.xml

Details on config values and their corresponding environment variables can be found in the nextsong.config docstring, which can be viewed in the Python interpreter by calling help(nextsong.config).

Local playback example with vlc

While actually playing the media is outside this library's scope, it's fairly straightforward to write a script that does media playback by invoking nextsong in a loop and feeding the result into a media player. For example, here's a bash script using vlc to play the playlist:

trap break INT
while true
do
    TRACK="$(nextsong)"
    if [ -z "$TRACK" ]
    then
        printf "End of playlist\n"
        break
    fi
    printf "Playing %s\n" "$TRACK"
    cvlc --play-and-exit "$TRACK" >& /dev/null
done

Learning more

Any module, class, or function can be passed into the builtin help function for detailed information. See tests/cases/examples for complete usage examples. For help on the command line tool, invoke

$ nextsong --help

Please feel free to open an issue for any further questions.

Installation

Requires Python 3.7 or higher

From PyPI

Install using pip

$ python3 -m pip install nextsong

From source

First install build dependencies

$ python3 -m pip install build

Building the distribution

$ git clone https://gitlab.com/samflam/nextsong.git
$ cd nextsong
$ make

To install, you can pip install the built wheel in dist or simply run

$ make install

Testing

There are some additional dependencies for testing

  • black: format checker
  • pylint: linter

From the top level, do

$ make test

Metadata

Release files for nextsong 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nextsong 1.1.1
File Size Uploaded
nextsong-1.1.1.tar.gz 15.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nextsong 1.1.1
File Interpreter ABI Platform
nextsong-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.0 kB

Release files / nextsong-1.1.1.tar.gz

Download URL nextsong-1.1.1.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4c8e662cce86e676a5d4ae0ba95e74ae389d1bba9c1c90e74bd696be34e26b3c
BLAKE2b-256 checksum
How to use checksums
fc3c058f004381b267e61440e18e1f8c600344c3c045e640f9bbd9a66de72075
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

Release files / nextsong-1.1.1-py3-none-any.whl

Download URL nextsong-1.1.1-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
756f42c2e5e7e2af3fe16c9085bb7e857f63f20d547ac95a07f68213ff6a2453
BLAKE2b-256 checksum
How to use checksums
69696fd32d08b203c26d21f02bde30443a70db88fd726825fad38eb2d77c565a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

Release history Release notifications | RSS feed

1.2.1

2 release files

1.2.0

2 release files

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page