Python 从多个文件夹创建播放列表的代码
Python code to create playlist from multiple folders
这是我在 Stack Overflow 上的第一个问题,如果重复出现,我深表歉意。我一直在寻找上个月左右的一些代码,以创建一个随机播放列表,该列表将 运行 每晚在我的 Raspberry Pi (Raspbian) 上使用 Python。但是没有运气!
播放列表将由 2 个文件夹中的内容组成。音乐在 1(约 200 个文件)Ads/Jingles(5 个文件)中。我希望能够创建一个播放列表(m3u 格式),每天随机播放音乐,但仍然每 5 首歌曲播放 ad/jingle。所以每天唯一应该重复的是 ads/jingles.
我目前 运行正在为音乐播放器使用 Kodi,因为我也想要电影。
有没有人可以帮助我解决这个问题?
希望对您有所帮助。不太确定音乐播放器。我假设您有打开文件并播放它的方法。可以根据需要迭代今天播放列表中表示的文件列表。
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)])
对于其他想要这样做的人,我使用 bash 找到了一个很好的解决方法。来自: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
这是我在 Stack Overflow 上的第一个问题,如果重复出现,我深表歉意。我一直在寻找上个月左右的一些代码,以创建一个随机播放列表,该列表将 运行 每晚在我的 Raspberry Pi (Raspbian) 上使用 Python。但是没有运气!
播放列表将由 2 个文件夹中的内容组成。音乐在 1(约 200 个文件)Ads/Jingles(5 个文件)中。我希望能够创建一个播放列表(m3u 格式),每天随机播放音乐,但仍然每 5 首歌曲播放 ad/jingle。所以每天唯一应该重复的是 ads/jingles.
我目前 运行正在为音乐播放器使用 Kodi,因为我也想要电影。
有没有人可以帮助我解决这个问题?
希望对您有所帮助。不太确定音乐播放器。我假设您有打开文件并播放它的方法。可以根据需要迭代今天播放列表中表示的文件列表。
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)])
对于其他想要这样做的人,我使用 bash 找到了一个很好的解决方法。来自: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