-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
396 lines (294 loc) · 11.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from genericpath import exists
from posixpath import split
import queue
import threading
import cv2
import subprocess as sp
import time
from numpy.core.fromnumeric import size
from numpy.lib import add_docstring
import pyaudio
import wave
from tqdm import tqdm
from PIL import Image, ImageDraw
import numpy as np
import os
import ffmpeg
class Live(object):
def __init__(self, inputUrl="", rtmpUrl=""):
self.frame_queue = queue.Queue()
self.video_queue = queue.Queue()
self.playing_video = None
self.playing_image = None
self.playing_text = None
self.command = ""
self.rtmpUrl = rtmpUrl
self.camera_path = inputUrl
self.width = None
self.hight = None
self.audio = Audio("test.wav")
self.audio.add_radio("test1.wav")
self.connect()
def connect(self):
print("Open Source")
if self.camera_path:
self.cap = cv2.VideoCapture(self.camera_path)
else:
self.cap = cv2.VideoCapture(0)
self.rtmp_fps = int(self.cap.get(cv2.CAP_PROP_FPS))
self.rtmp_width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.rtmp_height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
self.hight = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
self.run_mmpeg()
def run_mmpeg(self):
self.command = ['ffmpeg',
'-y',
'-f', 'rawvideo',
'-vcodec','rawvideo',
'-pix_fmt', 'bgr24',
'-s', "{}x{}".format(self.rtmp_width, self.rtmp_height),
'-r', str(self.rtmp_fps),
'-i', '-',
'-c:v', 'libx264',
'-c:a', 'aac',
'-ar', '44100',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'flv',
self.rtmpUrl
]
while True:
if len(self.command) > 0:
self.p = sp.Popen(self.command, stdin=sp.PIPE)
break
def read_frame(self):
while(self.cap.isOpened()):
try:
ret, frame = self.cap.read()
if not ret:
print("Opening camera is failed")
break
self.frame_queue.put(frame)
except Exception as e:
print("error, will retry : ", str(e))
time.sleep(1)
self.connect()
def push_frame(self):
while True:
if self.frame_queue.empty() != True:
frame = self.frame_queue.get()
frame = self.merge_image(frame, scale_rate=0.3, position=[100,100])
frame = self.merge_text(frame, position=[10, 200])
add_frame = self.get_video_frame()
if type(add_frame) == np.ndarray:
frame = self.merge_frame(frame, add_frame)
try:
self.p.stdin.write(frame.tostring())
except Exception as e:
print("write rtmp faile, retry %s", str(e))
time.sleep(1)
self.run_mmpeg()
def merge_frame(self, frame, addFrame, scale_rate=0.3, position=[0, 0]):
img1 = Image.fromarray(frame)
img2 = Image.fromarray(addFrame)
img2 = img2.resize((int(self.width * scale_rate), int(self.hight * scale_rate)))
img1.paste(img2, position)
return np.asarray(img1)
def merge_image(self, frame, scale_rate=0.3, position=None):
if not self.playing_image:
return frame
o = Image.open(self.playing_image)
o = o.resize((int(self.width * scale_rate), int(self.hight * scale_rate)))
img1 = Image.fromarray(frame)
if isinstance(position, list) and len(position) == 2:
img1.paste(o, position)
out = np.asarray( img1)
return out
def merge_text(self, frame, position=None):
if not self.playing_text:
return frame
img1 = Image.fromarray(frame)
image_editable = ImageDraw.Draw(img1)
if not isinstance(position, list) or len(position) == 2:
position = [10, 200]
image_editable.text(position, self.playing_text, (237, 230, 211), align='center', size=100)
out = np.asarray( img1)
return out
def add_text(self, text):
self.playing_text = text
def remove_text(self):
self.playing_text = None
def add_image(self, name):
self.playing_image = name
def remove_image(self):
self.playing_image = None
def remove_video(self):
self.playing_video = None
def add_video(self, video_path=None):
if video_path and os.path.exists(video_path):
cap = cv2.VideoCapture(video_path)
self.video_queue.put(cap)
def get_video_frame(self):
if self.playing_video and self.playing_video.isOpened():
ret, frame = self.playing_video.read()
if ret == True:
return frame
if self.playing_video:
self.playing_video.release()
self.playing_video = None
if not self.video_queue.empty():
self.playing_video = self.video_queue.get()
return self.get_video_frame()
return None
def exec(self):
while True:
i = input("Please enter the command:add|del jpg|video|text value\n ")
cs = i.split()
if len(cs) != 3:
print("Wrong input!")
continue
if cs[0] == "add":
if cs[1] == "jpg":
self.add_image(cs[2])
continue
elif cs[1] == "video":
self.add_video(cs[2])
continue
elif cs[2] == "text":
self.add_text(cs[2])
continue
else:
print("Wrong input!")
continue
elif cs[0] == "del":
if cs[1] == "jpg":
self.remove_image(cs[2])
continue
elif cs[1] == "video":
self.remove_video(cs[2])
continue
elif cs[2] == "text":
self.remove_text(cs[2])
continue
print("Wrong input!!")
continue
def run(self):
threads = [
threading.Thread(target=Live.read_frame, args=(self,)),
threading.Thread(target=Live.push_frame, args=(self,))
]
[thread.setDaemon(True) for thread in threads]
[thread.start() for thread in threads]
self.exec()
for i in threads:
i.join()
class Audio(object):
def __init__(self,wave_out_path):
self.waiting_play_queue = queue.Queue()
self.playing = None
self.p = pyaudio.PyAudio()
self.CHUNK = 1024
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 44100
self.wf = wave.open(wave_out_path, 'wb')
self.wf.setnchannels(self.CHANNELS)
self.wf.setsampwidth(self.p.get_sample_size(self.FORMAT))
self.wf.setframerate(self.RATE)
def __del__(self):
self.wf.close()
def add_radio(self, radio_path):
if radio_path and os.path.exists(radio_path):
wf = wave.open(radio_path, 'rb')
self.waiting_play_queue.put(wf)
def get_add_frame(self, frame_count):
if self.playing:
data2 = self.playing.readframes(frame_count)
if not data2:
self.playing = None
else:
return data2
if not self.waiting_play_queue.empty():
self.playing = self.waiting_play_queue.get()
return self.get_add_frame(frame_count)
def record_audio(self,record_second):
stream = None
def callback(in_data, frame_count, time_info, status):
add_frame = self.get_add_frame(frame_count)
if add_frame:
decoded_add = np.frombuffer(add_frame, np.int16)
decoded_in = np.frombuffer(in_data, np.int16)
newdata = (decoded_in * 0.7 + decoded_add* 0.3).astype(np.int16)
self.wf.writeframes(newdata)
return (newdata.tostring(), status)
else:
self.wf.writeframes(in_data)
return (in_data, status)
stream = self.p.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK,
stream_callback=callback)
print("* recording")
stream.start_stream()
print("* done recording")
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
self.p.terminate()
self.wf.close()
def mac(self):
print(pyaudio.PaMacCoreStreamInfo().get_channel_map())
def get_audio_devices_info(self):
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
dev = p.get_device_info_by_index(i)
print((i,dev['name'],dev['maxInputChannels']))
class ReadRTMP(object):
def ReadFromRTMP(self):
self.source = ffmpeg.input("rtmp://127.0.0.1:3000/live/cc")
audio = self.source.audio.filter("aecho", 0.8, 0.9, 1000, 0.3)
video = self.source.video.hflip()
out = ffmpeg.output(audio, video, "xx.mp4")
ffmpeg.run(out)
def get_viedo_width_height(self,path):
cap = cv2.VideoCapture(path)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
return width, height
def split_av(self):
width, height = self.get_viedo_width_height("test.mp4")
process1 = (
ffmpeg
.input("test.mp4")
.output('pipe:', format='rawvideo')
.run_async(pipe_stdout=True)
)
process2 = (
ffmpeg
.input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width, height))
.output("zz.mp4", pix_fmt='yuv420p')
.overwrite_output()
.run_async(pipe_stdin=True)
)
while True:
in_bytes = process1.stdout.read(width * height * 3)
if not in_bytes:
break
in_frame = (
np
.frombuffer(in_bytes, np.uint8)
.reshape([height, width, 3])
)
out_frame = in_frame * 0.3
process2.stdin.write(in_bytes)
process2.stdin.close()
process1.wait()
process2.wait()
if __name__ == "__main__":
url = "test.mp4"
live = Live(inputUrl=url, rtmpUrl="rtmp://yourIP/live/")
live.run()