-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (156 loc) · 4.68 KB
/
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# By Abdullah As-Sadeed
if __name__ == "__main__":
from tempfile import NamedTemporaryFile
import io
import os
import subprocess
import sys
import time
if len(sys.argv) != 5:
print(
"Usage: python main.py <image_path> <icecast_host> <icecast_port> <icecast_password>"
)
sys.exit(1)
try:
subprocess.run(
["ffmpeg", "-version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
print("FFmpeg is available.")
except subprocess.CalledProcessError:
print("FFmpeg could not be checked!")
sys.exit(1)
except FileNotFoundError:
print("FFmpeg has not been found in the PATH!")
sys.exit(1)
required_packages = ["Pillow", "pydub", "pysstv"]
def install_package(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
def safe_import(package_name):
try:
if package_name == "Pillow":
package = __import__("PIL")
else:
package = __import__(package_name)
print(f"The {package_name} package is available.")
return package
except ImportError:
print(f"Could not find the {package_name} package. Installing ...")
install_package(package_name)
if package_name == "Pillow":
package = __import__("PIL")
else:
package = __import__(package_name)
print(f"The {package_name} package has been installed and is available.")
return package
PIL = safe_import("Pillow")
pysstv = safe_import("pysstv")
AudioSegment = safe_import("pydub").AudioSegment
from PIL import Image
from pysstv.color import (
MartinM1,
MartinM2,
PD120,
PD160,
PD180,
PD240,
PD290,
PD90,
PasokonP3,
PasokonP5,
PasokonP7,
Robot36,
ScottieDX,
ScottieS1,
ScottieS2,
WraaseSC2120,
WraaseSC2180,
)
from pysstv.grayscale import Robot8BW, Robot24BW
SSTV_MODES = [
MartinM1,
MartinM2,
PD120,
PD160,
PD180,
PD240,
PD290,
PD90,
PasokonP3,
PasokonP5,
PasokonP7,
Robot24BW,
Robot36,
Robot8BW,
ScottieDX,
ScottieS1,
ScottieS2,
WraaseSC2120,
WraaseSC2180,
]
image_path = sys.argv[1]
icecast_host = sys.argv[2]
icecast_port = sys.argv[3]
icecast_password = sys.argv[4]
try:
if not os.path.isfile(image_path):
raise FileNotFoundError(f'Could not find the "{image_path}" file!')
sys.exit(1)
image = Image.open(image_path)
print(f"Opened the image: {image_path}")
except FileNotFoundError as error:
print(error)
sys.exit(1)
except OSError as error:
print(error)
sys.exit(1)
SAMPLES_PER_SECOND = 48000
BITS_PER_SAMPLE = 16
CHANNELS = 1
VOX_ENABLED = True
progress = 0
for mode_class in SSTV_MODES:
mode_name = mode_class.__name__
print(f"Generating {mode_name} ...")
wav_buffer = io.BytesIO()
sstv = mode_class(
image=image, samples_per_sec=SAMPLES_PER_SECOND, bits=BITS_PER_SAMPLE
)
sstv.nchannels = CHANNELS
sstv.vox_enabled = VOX_ENABLED
sstv.write_wav(wav_buffer)
wav_buffer.seek(0)
audio_segment = AudioSegment.from_wav(wav_buffer)
print(f"Streaming {mode_name} ...")
with NamedTemporaryFile(suffix=".ogg", delete=True) as temporary_file:
audio_segment.export(temporary_file.name, format="opus")
stream_command = [
"ffmpeg",
"-re",
"-i",
temporary_file.name,
"-c:a",
"libopus",
"-content_type",
"audio/ogg",
"-f",
"opus",
"-vbr",
"on",
"-vn",
f"icecast://source:{icecast_password}@{icecast_host}:{icecast_port}/{mode_name}_{int(time.time())}",
"-loglevel",
"info",
]
result = subprocess.run(stream_command)
if result.returncode != 0:
print(
f"Error streaming {mode_name}. FFmpeg returned code {result.returncode}."
)
break
progress += 1
print(f"\nDone with {progress} SSTV modes.")