-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio_main.py
90 lines (62 loc) · 2.75 KB
/
radio_main.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
import mutagen
import os
import random
import sys
import subprocess
import time
import song_updater
import radio_config
config = radio_config.get_config()
radio_rootdir = config["radio-root"]
icecast_source_creds = config["icecast-source"]
icecast_address = config["icecast-address"]
mountpoint = ""
mountpoint_path = ""
icecast_mountpoint = ""
lines = []
force_change_format = (".jpg", ".webp")
def get_file_length(file):
return file.info.length
def get_files_and_shuffle(rootdir):
for root, subdirs, files in os.walk(rootdir):
for name in files:
if os.path.splitext(name)[1] == ".mp3":
filepath = os.path.join(root, name)
lines.append(filepath)
# unifying formats, it may break something but whatever
if os.path.splitext(name)[1] in force_change_format:
old_name = os.path.join(root, name)
new_name = os.path.join(root, name.split(".")[0] + ".png")
os.rename(old_name, new_name)
random.shuffle(lines)
def prepare_files(rootdir):
ret_songs = []
global_duration = 0
af = open(os.path.join(rootdir, "audiofiles.txt"), "a+")
for filepath in lines:
filename = os.path.basename(filepath)
songname = os.path.splitext(filename)[0]
duration = get_file_length(mutagen.File(filepath))
ret_songs.append((global_duration, songname))
af.write("file '{}'\n".format(filepath))
global_duration += duration
af.close()
return ret_songs
def remove_prev_files(rootdir):
audiofiles = os.path.join(rootdir, "audiofiles.txt")
if os.path.exists(audiofiles):
os.remove(audiofiles)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("No endpoint specified")
exit(1)
mountpoint = sys.argv[1]
mountpoint_path = os.path.join(radio_rootdir, mountpoint)
audiofiles_path = os.path.join(mountpoint_path, "audiofiles.txt")
icecast_mountpoint = "icecast://{}@{}/{}".format(icecast_source_creds, icecast_address, mountpoint)
remove_prev_files(mountpoint_path)
get_files_and_shuffle(mountpoint_path)
songs = prepare_files(mountpoint_path)
#os.system("ffmpeg -re -f concat -safe 0 -i {} -c:a libmp3lame -ar 44100 -ac 2 -vn -f mp3 -map_metadata 0 -content_type 'audio/mpeg' {} &".format(audiofiles_path, icecast_mountpoint))
proc = subprocess.Popen("ffmpeg -re -f concat -safe 0 -i {} -c:a libmp3lame -ar 44100 -ac 2 -vn -f mp3 -map_metadata 0 -content_type 'audio/mpeg' {}".format(audiofiles_path, icecast_mountpoint), shell=True, start_new_session=True) # stderr=subprocess.STDOUT)
song_updater.title_updater_start(lines, songs, mountpoint, proc, config)