0

This is my first ever question on Stack Overflow, I am terribly sorry if this has been repeated. I have been searching for the last month or so for some code, to create a random playlist that would run each night on my Raspberry Pi (Raspbian) using Python. But have had no luck!

The playlist would be made up of content from 2 folders. Music in 1 (about 200 files) Ads/Jingles (5 files) in the other. I want to be able to create a playlist (m3u format) that would randomize the music each day but still have an ad/jingle playing every 5 songs. So the only thing that should repeat each day is the ads/jingles.

I am currently running Kodi for the music player, as I want movies as well.

Is there anyone that would be able to help me with this?

Pang
  • 9,564
  • 146
  • 81
  • 122
Ben
  • 1
  • 2
  • You're probably going to need to show some attempts at this to get someone to help you. You may want to look here for how to ask a good question: http://stackoverflow.com/help/how-to-ask – Robert Moskal Nov 09 '15 at 05:42

2 Answers2

0

Hope this helps a little. Not too sure on the music player. I assume you have a method to open up the file and play it. The list of files denoted in today's playlist can be iterated on as needed.

from os import listdir
from os.path import isfile, join
music_file_path = "music"
jingle_file_path = "ads/jingles"
jingle_files = [ f for f in listdir(music_file_path) if isfile(join(music_file_path,f)) ]
music_files = [ f for f in listdir(jingle_files) if isfile(join(jingle_files,f)) ]

music_files.shuffle()
jingle_files.shuffle()

todays_playlist = []
for i in range(len(music_files)):
    todays_playlist.append(music_files[i])
    if i % 5 == 0:
        todays_play_list.append(jingle_files[ (i // 5) % len(jingle_files)])
mattsap
  • 3,790
  • 1
  • 15
  • 36
  • I would also add that (1) `m3u` files have relatively simple format (see Wikipedia), so it shouldn't be a problem to create it now. (2) Universal approach to open a file in associated application in Python is `import webbrowser; webbrouser.open(fname)`. – u354356007 Nov 09 '15 at 06:02
  • Thank you very much for replying. Unfortunately the code didn't work, I am extremely new to Python so I am terribly sorry if this is a common problem. I receive an error at music_files = TypeError: Can't convert 'list' object to str implicitly. – Ben Nov 16 '15 at 01:23
0

For others wanting to do this, I found a good way around it using bash. Got this from: https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=63568

#!/bin/bash
if [ -f /home/pi/music.lock ]; then
echo "Lock Exists, exiting"
exit 0
fi
touch /home/pi/music.lock
target="21"
cur=$(date '+%H')
while [ $target != $cur ]
do
cd /home/pi/music
mpg321 "$(ls *.mp3 | shuf -n1)"
mpg321 "$(ls *.mp3 | shuf -n1)"
mpg321 "$(ls *.mp3 | shuf -n1)"
mpg321 "$(ls *.mp3 | shuf -n1)"
mpg321 "$(ls *.mp3 | shuf -n1)"
cd /home/pi/messages
mpg321 "$(ls *.mp3 | shuf -n1)"
cur=$(date '+%H')
done
rm /home/pi/music.lock
Ben
  • 1
  • 2