[max@neo-c] $ ls /

[max@neo-c] $ ./site-safety.sh
### This site is rated 14 and up, unless otherwise stated ###


[NOTICE]last pageedit
<2023-03-14>

A webradio made using libVLC and python

new writeup!

Script outline

my script is laid out like this:
  • play_jingle method (jingle_name='') - plays a random or specific jingle
  • check_timings method () - checks whether a jingle should be played
  • play_media method (media_path) - plays a peice of media through libVLC from a path
  • music_collections dictionary - full of collections of music
  • collection dictionary - laid out with genres, which will be randomly selected between based on what they like
  • genres dictionary - has a name, an array of music file or folder paths and an array of other genres that this genre likes
  • play_collection method (music_collection) - runs through a music collection infinitely
  • main method () - just starts play_collection()

useful snippets:

global vlc instance
import vlc
instance = vlc.Instance()
player = instance.media_player_new()

play some media + output to a http stream
media = instance.media_new(media_path)
media.add_option(":sout=#http{mux=ffmpeg{mux=mp3},dst=:8080/radio_1.ogg}")
media.add_option("no-video")
player.set_media(media)
media.parse()
player.play()
time.sleep(media.get_duration() / 1000)
player.stop()

what collections looks like
music_list = {
    'daytime_collections': [
        {
            'name': 'genre1',
            'music': [
                '/path/to/music/folder/',
                '/path/to/music/file.mp3'
            ],
            'likes': ['genre2']
        },
        {
            'name': 'genre2',
            'music': [
                '/path/to/music/folder/',
                '/path/to/music/folder/',
                '/path/to/music/file.mp3'
            ],
            'likes': ['genre1']
        },
    ],
    'nighttime_collections': [
        {
            'name': 'genre1',
            'music': [
                '/path/to/music/folder/',
                '/path/to/music/file.mp3'
            ],
            'likes': ['genre2']
        },
        {
            'name': 'genre2',
            'music': [
                '/path/to/music/folder/',
                '/path/to/music/folder/',
                '/path/to/music/file.mp3'
            ],
            'likes': ['genre1', 'genre3']
        },
        {
            'name': 'genre3',
            'music': [
                '/path/to/music/folder/',
                '/path/to/music/file.mp3',
                '/path/to/music/file.mp3'
            ],
            'likes': ['genre2', 'genre1']
        },
    ]
}

play a collection
def play_collection(music_collection):
    collection = {}

    playlist_memory = []  # holds 30 items

    previous_collection_name = ''
    completed_n_playlists = 0
    selected_collection_name = 'Rock'

    night_shift_done = False

    # for number in range(0, 50):
    while True:
        if 8 > datetime.datetime.now().hour > 18 and not night_shift_done:
            night_shift_done = True
            play_jingle('nighttime')  # nighttime shift jingle
            collections = 'nighttime_collections'  # nighttime mode
        else:
            play_jingle('daytime')  # daytime shift jingle
            collections = 'daytime_collections'
            night_shift_done = False

        print('\nSelecting collection with name: ' + str(selected_collection_name) + '...')
        for i in music_collection[collections]:
            if i['name'] == selected_collection_name:
                collection = i

        if collection == {}:
            print('Selecting collection failed! Falling back to previous collection...')
            selected_collection_name = previous_collection_name
            continue

        previous_collection_name = selected_collection_name

        music = collection['music'].copy()
        for i in range(0, len(music)):
            if len(playlist_memory) >= 30:
                del playlist_memory[0]

            playlist = random.choice(music)
            music.remove(playlist)

            if playlist in playlist_memory:
                print('### Skipping Playlist ' + str(playlist))
                continue
            else:
                playlist_memory.append(playlist)

            print('#### Loaded Playlist ' + str(playlist))
            playlist_path = pathlib.Path(base_music_directory, playlist)
            if playlist_path.is_dir():
                for music_file in sorted(playlist_path.rglob('*.*')):
                    if music_file.suffix in ['.jpg', '.png', '.url', '.txt']:
                        continue
                    if 'flac' in str(music_file).lower().replace(music_file.suffix, ''):
                        continue
                    print('## Playing File: ' + str(music_file))
                    play_media(music_file)
                    completed_n_playlists += 1
            else:
                print('## Playing File: ' + str(playlist_path))
                play_media(playlist_path)
                completed_n_playlists += 1

            if random.randint(0,
                              3) == 0 or completed_n_playlists > 20:  # 25% chance of moving to a different collection
                completed_n_playlists = 0
                break

        selected_collection_name = random.choice(collection['likes'])
        collection = {}


end notes

you can use this code how ever you want (with credit please)
i have no idea if this works / how it works with something like icecast

useful links:
https://stackoverflow.com/questions/64500860/can-i-stream-a-file-with-python-vlc