-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaymp3files.py
165 lines (115 loc) · 3.58 KB
/
playmp3files.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Example that shows how the new Python 2 socket client can be used.
using in combination with local webfs to play out audioclips to chromecast using Freifunk Mesh Network
"""
from __future__ import print_function
import time
import sys
import tagpy
import tagpy.id3v2 as id3v2
import logging
import pychromecast
import pychromecast.controllers.youtube as youtube
from netifaces import interfaces, ifaddresses, AF_INET
id3v2.FrameFactory.instance().setDefaultTextEncoding(tagpy.StringType.UTF8)
def ip4_addresses():
ip_list = []
for interface in interfaces():
for link in ifaddresses(interface)[AF_INET]:
ip_list.append(link['addr'])
return ip_list
if '--show-debug' in sys.argv:
logging.basicConfig(level=logging.DEBUG)
global numclips
numclips=10
global activeclip
activeclip=0
global port
port = '8000'
global mp3path
mp3path ="/srv/ftp/freifunk/music/"
myadresses=ip4_addresses()
print ("IP-Adressen lokal: ",myadresses)
# hard coded - TODO - check for the one with 10.xxx.xxx.xxx
wireless = myadresses[2]
print ("My wireless IP: ", wireless)
# Chromecasts
# MAC friendly name
#
# 6C:AD:F8:B1:57:01 Freifunk Chromecast 01
#
cast = pychromecast.get_chromecast("Freifunk Chromecast 01")
yt = youtube.YouTubeController()
cast.register_handler(yt)
cast.wait()
print()
print(cast.device)
time.sleep(1)
print()
print(cast.status)
print()
print(cast.media_controller.status)
print()
if '--show-status-only' in sys.argv:
sys.exit()
if not cast.is_idle:
print("Killing current running app")
cast.quit_app()
time.sleep(5)
url="http://"+wireless+":"+port+"/freifunk/music/freifunk"+str(activeclip)+".mp3"
# get mp3 metadata from file
localname=mp3path+"freifunk"+str(activeclip)+".mp3"
fileref = tagpy.FileRef(localname)
file = fileref.file()
tag = file.ID3v2Tag()
if tag == None:
# No ID3v2.4.0
print('No ID3v2.4.0')
if 'APIC' in tag.frameListMap().keys():
for frame in tag.frameListMap()['APIC']:
if str(frame.type()) == 'FrontCover':
print ("FrontCover gefunden.")
print (tag.artist)
print (tag.title)
print (tag.album)
print (tag.year)
print (tag.comment)
print("Playing: ", url)
cast.play_media((url), "audio/mpeg")
t = 0
activeclip = 1
while True:
try:
t += 1
if activeclip > numclips:
activeclip = 1
if t > 10 and t % 3 == 0:
print("Media status", cast.media_controller.status)
# print("Player state", cast.media_controller.status.player_state)
if t > 30 and not cast.media_controller.status.player_state == "PLAYING":
t = 0
url = "http://"+wireless+":"+port+"/freifunk/music/freifunk"+str(activeclip)+".mp3"
# get mp3 metadata from file
localname=mp3path+"freifunk"+str(activeclip)+".mp3"
fileref = tagpy.FileRef(localname)
file = fileref.file()
tag = file.ID3v2Tag()
if tag == None:
# No ID3v2.4.0
print('No ID3v2.4.0')
if 'APIC' in tag.frameListMap().keys():
for frame in tag.frameListMap()['APIC']:
if str(frame.type()) == 'FrontCover':
print ("FrontCover gefunden.")
print (tag.artist)
print (tag.title)
print (tag.album)
print (tag.year)
print (tag.comment)
print ("Playing: ",url)
cast.play_media((url), "audio/mpeg")
activeclip = activeclip + 1
time.sleep(1)
except KeyboardInterrupt:
break
cast.quit_app()