本地音乐随机播放

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pydub import AudioSegment
from pydub.playback import play
from pathlib import Path
import os
import random

d='D:/music'

def play_m(mn):
if mn.endswith('.wav'):
song=AudioSegment.from_wav(mn)
elif mn.endswith('.mp3'):
song=AudioSegment.from_mp3(mn)
elif mn.endswith('.flac'):
song=AudioSegment.from_file(mn,'flac')
elif mn.endswith('.m4a'):
song=AudioSegment.from_file(mn,'aac')
elif mn.endswith('.m4b'):
song=AudioSegment.from_file(mn,'aac')
play(song-15)

mns=[]
for entry in Path(d).rglob('*'):
if entry.suffix in ['.wav','.mp3','.flac','.m4a','.m4b']:
mns.append(entry.__str__())
print(mns)
with open('current_music_name.txt',encoding='utf8') as f:
cmn=f.read()
random.shuffle(mns)
if cmn in mns:
i=mns.index(cmn)
else:
i=0
i=(i+1)%len(mns)
while True:
print(f'playing {mns[i]}')
with open('current_music_name.txt','w',encoding='utf8') as f:
f.write(mns[i])
play_m(mns[i])
i=(i+1)%len(mns)

https://zhuanlan.zhihu.com/p/409775989 python调整音频(MP3)的音量和切片

https://blog.csdn.net/pengranxindong/article/details/90606279 用python播放声音文件(mp3、wav、m4a等)

https://blog.csdn.net/orangefly0214/article/details/81387077 Python 获取list中指定元素的索引

2312051147