-
Notifications
You must be signed in to change notification settings - Fork 161
/
stream.py
371 lines (331 loc) · 16.5 KB
/
stream.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, argparse, time, subprocess, shlex, logging, os, re
from bigbluebutton_api_python import BigBlueButton, exception
from bigbluebutton_api_python import util as bbbUtil
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import JavascriptException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime
from distutils.util import strtobool
import time
browser = None
selenium_timeout = 30
connect_timeout = 5
logging.basicConfig(level=os.environ.get('LOGLEVEL', 'INFO' if not os.environ.get('DEBUG') else 'DEBUG'))
parser = argparse.ArgumentParser()
parser.add_argument("-s","--server", help="Big Blue Button Server URL")
parser.add_argument("-p","--secret", help="Big Blue Button Secret")
parser.add_argument("-i","--id", help="Big Blue Button Meeting ID")
parser.add_argument("-I","--intro", help="Intro file to play before streaming")
parser.add_argument("-B","--beginIntroAt", help="begin intro at position (e.g. 00:01:05)")
parser.add_argument("-E","--endIntroAt", help="End intro at position (e.g. 01:00:04)")
parser.add_argument("-l","--stream", help="live stream a BigBlueButton meeting",action="store_true")
parser.add_argument("-d","--download", help="download / save a BigBlueButton meeting",action="store_true")
parser.add_argument("-S","--startMeeting", help="start the meeting if not running",action="store_true")
parser.add_argument("-A","--attendeePassword", help="attendee password (required to create meetings)")
parser.add_argument("-M","--moderatorPassword", help="moderator password (required to create a meeting)")
parser.add_argument("-T","--meetingTitle", help="meeting title (required to create a meeting)")
parser.add_argument("-u","--user", help="Name to join the meeting",default="Live")
parser.add_argument("-t","--target", help="RTMP Streaming URL")
parser.add_argument("--chatUrl", help="Streaming URL to display in the chat", default=False)
parser.add_argument("--chatMsg", nargs='+', help="Message to display in the chat before Streaming URL", default=False)
parser.add_argument("-c","--chat", help="Show the chat",action="store_true")
parser.add_argument("-r","--resolution", help="Resolution as WxH", default='1920x1080')
parser.add_argument('--ffmpeg-stream-threads', help='Threads to use for ffmpeg streaming', type=int,
default=os.environ.get('FFMPEG_STREAM_THREADS', '') or 0)
parser.add_argument('--ffmpeg-stream-video-bitrate', help='Video birate to use for ffmpeg streaming (in k)', type=int,
default=os.environ.get('FFMPEG_STREAM_VIDEO_BITRATE', '') or 4000)
parser.add_argument(
'--ffmpeg-stream-options',
help='ffmpeg stream options (can be set using env FFMPEG_STREAM_OPTIONS)',
default=os.environ.get('FFMPEG_STREAM_OPTIONS', '') or '\
-c:a aac -b:a 160k -ar 44100 \
-threads "$FFMPEG_STREAM_THREADS" \
-c:v libx264 -x264-params "nal-hrd=cbr" -profile:v high -level:v 4.2 -vf format=yuv420p \
-b:v "${FFMPEG_STREAM_VIDEO_BITRATE}k" -maxrate "${FFMPEG_STREAM_VIDEO_BITRATE}k" -minrate "${FFMPEG_STREAM_VIDEO_BITRATE/2}k" -bufsize "${FFMPEG_STREAM_VIDEO_BITRATE*2}k" -g 60 \
-preset ultrafast \
')
parser.add_argument(
'--ffmpeg-download-options',
help='ffmpeg download options (can be set using env FFMPEG_DOWNLOAD_OPTIONS)',
default=os.environ.get('FFMPEG_DOWNLOAD_OPTIONS', '') or '-c:v libx264rgb -crf 0 -preset ultrafast'
)
parser.add_argument(
'--ffmpeg-input-thread-queue-size',
type=int,
help='ffmpeg thread_queue_size options to be applied to all inputs (can be set using env)',
default=os.environ.get('FFMPEG_INPUT_THREAD_QUEUE_SIZE', '1024')
)
parser.add_argument(
'--browser-disable-dev-shm-usage', action='store_true', default=False,
help='do not use /dev/shm',
)
parser.add_argument(
'--bbb-hide-meeting-title',
type=bool,
help='hide the meetings title in the top bar (can be set using env)',
default=bool(strtobool(os.environ.get('BBB_HIDE_MEETING_TITLE', '0')))
)
parser.add_argument(
'--bbb-hide-who-talks',
type=bool,
help='hide the annotation who is currently talking (can be set using env)',
default=bool(strtobool(os.environ.get('BBB_HIDE_WHO_TALKS', '0')))
)
parser.add_argument(
'--bbb-background-color',
help='override background color by a CSS color, e.g., "black" or "#ffffff" (can be set using env)',
default=os.environ.get('BBB_BACKGROUND_COLOR', '')
)
parser.add_argument(
'--logo',
help='add a logo to the video, passed as an image URL (can be set using env)',
default=os.environ.get('BBB_LOGO_URL', '')
)
parser.add_argument(
'--logo-position',
help='corner where to place the logo: "top/left", "top/right" (default), "bottom/left" or "bottom/right" (can be set using env)',
default=os.environ.get('BBB_LOGO_POS', 'top/right')
)
args = parser.parse_args()
# some ugly hacks for additional options
args.ffmpeg_stream_options = args.ffmpeg_stream_options.replace(
'$FFMPEG_STREAM_THREADS', str(args.ffmpeg_stream_threads),
).replace(
'${FFMPEG_STREAM_VIDEO_BITRATE}', str(args.ffmpeg_stream_video_bitrate),
).replace(
'${FFMPEG_STREAM_VIDEO_BITRATE/2}', str(args.ffmpeg_stream_video_bitrate // 2),
).replace(
'${FFMPEG_STREAM_VIDEO_BITRATE*2}', str(args.ffmpeg_stream_video_bitrate * 2),
)
bbb = BigBlueButton(args.server,args.secret)
bbbUB = bbbUtil.UrlBuilder(args.server,args.secret)
def set_up():
global browser
assert re.fullmatch(r'\d+x\d+', args.resolution)
options = Options()
options.add_argument('--disable-infobars')
options.add_argument('--no-sandbox')
options.add_argument('--kiosk')
options.add_argument('--window-size=%s' % args.resolution.replace('x', ','))
options.add_argument('--window-position=0,0')
options.add_experimental_option("excludeSwitches", ['enable-automation'])
options.add_experimental_option('prefs', {'intl.accept_languages':'{locale}'.format(locale='en_US.UTF-8')})
options.add_argument('--start-fullscreen')
options.add_argument('--autoplay-policy=no-user-gesture-required')
if args.browser_disable_dev_shm_usage:
options.add_argument('--disable-dev-shm-usage')
else:
dev_shm_size = int(subprocess.run('df /dev/shm/ --block-size=1M --output=size | tail -n 1', shell=True, stdout=subprocess.PIPE).stdout or '0')
required_dev_shm_size = 500 # in MB, 1024MB is recommended
if dev_shm_size < required_dev_shm_size:
logging.error(
'The size of /dev/shm/ is %sMB (minimum recommended is %sMB), '
'consider increasing the size of /dev/shm/ (shm-size docker parameter) or disabling /dev/shm usage '
'(see --browser-disable-dev-shm-usage or BROWSER_DISABLE_DEV_SHM_USAGE env variable).',
dev_shm_size, required_dev_shm_size
)
sys.exit(2)
logging.info('Starting browser!!')
browser = webdriver.Chrome(service=Service('./chromedriver'), options=options)
def bbb_browser():
global browser
logging.info('Open BBB and hide elements!!')
if args.startMeeting is True:
try:
logging.info("create_meeting...")
create_meeting()
except exception.bbbexception.BBBException as ERR:
logging.info(ERR)
logging.info("get_join_url...")
join_url = get_join_url()
logging.info(join_url)
browser.get(join_url)
time.sleep(10)
try:
# Wait for the input element to appear
logging.info("Waiting for chat input window to appear.")
element = EC.presence_of_element_located((By.ID, 'message-input'))
WebDriverWait(browser, selenium_timeout).until(element)
element = browser.find_element(By.ID, 'message-input')
chat_send = browser.find_elements(By.CSS_SELECTOR, '[aria-label="Send message"]')[0]
# ensure chat is enabled (might be locked by moderator)
if element.is_enabled() and chat_send.is_enabled():
tmp_chatMsg = os.environ.get('BBB_CHAT_MESSAGE', "This meeting is streamed to")
tmp_chatCustomMsg = os.environ.get('BBB_CHAT_CUSTOM_MESSAGE', "This meeting is streamed to")
if not tmp_chatMsg in [ 'false', 'False', 'FALSE' ]:
if args.target is not None:
tmp_chatUrl = args.target.partition('//')[2].partition('/')[0]
if args.chatUrl:
tmp_chatUrl = args.chatUrl
if args.chatMsg:
tmp_chatMsg = ' '.join(args.chatMsg).strip('"')
tmp_chatMsg = "{0}: {1}".format(tmp_chatMsg, tmp_chatUrl)
elif tmp_chatCustomMsg != '':
tmp_chatMsg = tmp_chatCustomMsg
else:
tmp_chatMsg = "Recording in progress!"
element.send_keys(tmp_chatMsg)
chat_send.click()
else:
logging.info("chat is not enabled")
if args.chat:
try:
browser.execute_script("document.querySelector('[aria-label=\"User list\"]').parentElement.style.display='none';")
except JavaScriptException:
browser.execute_script("document.querySelector('[aria-label=\"Users list\"]').parentElement.style.display='none';")
else:
element = browser.find_elements(By.ID, 'chat-toggle-button')[0]
if element.is_enabled():
element.click()
except NoSuchElementException:
# ignore (chat might be disabled)
logging.warn("could not find chat input or chat toggle")
except ElementClickInterceptedException as e:
# ignore (chat might be disabled)
logging.warn("could not find chat input or chat toggle")
logging.warn(e, exc_info=True)
if not args.chat:
try:
element = browser.find_elements(By.CSS_SELECTOR, 'button[aria-label^="Users and messages toggle"]')[0]
if element.is_enabled():
element.click()
except NoSuchElementException:
logging.info("could not find users and messages toggle")
except ElementClickInterceptedException:
logging.info("could not find users and messages toggle")
# Remove everything from the top bar, except the meeting's title.
browser.execute_script("document.querySelector('[class^=\"navbar\"] > div[class^=\"top\"] > div[class^=\"left\"]').style.display='none';")
browser.execute_script("document.querySelectorAll('[class^=\"navbar\"] > div[class^=\"top\"] > div[class^=\"center\"] > :not(h1)').forEach((ele) => ele.style.display='none');")
browser.execute_script("document.querySelector('[class^=\"navbar\"] > div[class^=\"top\"] > div[class^=\"right\"]').style.display='none';")
if args.bbb_hide_meeting_title:
browser.execute_script("document.querySelector('[class^=\"navbar\"] > div[class^=\"top\"]').style.display='none';")
if args.bbb_hide_who_talks:
browser.execute_script("document.querySelector('[class^=\"navbar\"] > div[class^=\"bottom\"]').style.display='none';")
if args.bbb_hide_meeting_title and args.bbb_hide_who_talks:
browser.execute_script("document.querySelector('[class^=\"navbar\"]').style.height='0px';")
browser.execute_script("document.querySelector('[aria-label=\"Actions bar\"]').style.display='none';")
browser.execute_script("""
const hideDecoratorsStyle = document.createElement("style");
hideDecoratorsStyle.innerText = `
/* Presentation hide minus button */
button[aria-label="Hide presentation"],
/* Fullscreen button, both for presentations and webcams */
button[aria-label^="Make "][aria-label$=" fullscreen"],
/* Drop down menu next to user names for webcam videos */
div[class^="videoCanvas"] [class^="dropdownTrigger"]::after,
/* Interactive poll window */
div[class^="pollingContainer"],
/* Notification toasts */
div[class="Toastify"] {
display: none;
}
`;
document.head.appendChild(hideDecoratorsStyle);
""")
if args.logo != '':
[logo_pos_vertical, logo_pos_horizontal] = args.logo_position.split('/')
browser.execute_script("""
const navbarHeader = document.querySelector('[class^="navbar"]');
const logoImg = document.createElement("img");
logoImg.style = "position: absolute; %s: 5px; %s: 5px;";
logoImg.src = "%s";
logoImg.height = 0.1 * window.innerHeight;
navbarHeader.appendChild(logoImg);
""" % (logo_pos_vertical, logo_pos_horizontal, args.logo))
try:
browser.execute_script("document.getElementById('container').setAttribute('style','margin-bottom:30px');")
except JavascriptException:
browser.execute_script("document.getElementById('app').setAttribute('style','margin-bottom:30px');")
if args.bbb_background_color:
browser.execute_script("document.querySelector('body').setAttribute('style','background-color: %s;');" % args.bbb_background_color)
def create_meeting():
create_params = {}
if args.moderatorPassword:
create_params['moderatorPW'] = args.moderatorPassword
if args.attendeePassword:
create_params['attendeePW'] = args.attendeePassword
if args.meetingTitle:
create_params['name'] = args.meetingTitle
return bbb.create_meeting(args.id, params=create_params)
def get_join_url():
minfo = bbb.get_meeting_info(args.id)
pwd = minfo.get_meetinginfo().get_attendeepw()
joinParams = {}
joinParams['meetingID'] = args.id
joinParams['fullName'] = args.user
joinParams['password'] = pwd
joinParams['userdata-bbb_auto_join_audio'] = "true"
joinParams['userdata-bbb_enable_video'] = 'true'
joinParams['userdata-bbb_listen_only_mode'] = "true"
joinParams['userdata-bbb_force_listen_only'] = "true"
joinParams['userdata-bbb_skip_check_audio'] = 'true'
joinParams['joinViaHtml5'] = 'true'
return bbbUB.buildUrl("join", params=joinParams)
def stream_intro():
introBegin = ""
if args.beginIntroAt:
introBegin = "-ss %s"%(args.beginIntroAt)
introEnd = ""
if args.endIntroAt:
introEnd = "-to %s"%(args.endIntroAt)
ffmpeg_stream = 'ffmpeg -re %s %s -thread_queue_size "%s" -i %s -thread_queue_size %s -f pulse -i default -ac 2 %s -f flv "%s"' % (
introBegin, introEnd, args.ffmpeg_input_thread_queue_size, args.intro, args.ffmpeg_input_thread_queue_size, args.ffmpeg_stream_options, args.target
)
logging.debug('Preparing to execute %r' % ffmpeg_stream)
ffmpeg_args = shlex.split(ffmpeg_stream)
logging.info("streaming intro...")
p = subprocess.call(ffmpeg_args)
def stream():
ffmpeg_outputs = []
if args.download:
downloadFile = "/video/meeting-%s.mkv" % fileTimeStamp
ffmpeg_outputs.extend(shlex.split(args.ffmpeg_download_options))
ffmpeg_outputs.append(downloadFile)
logging.info("saving meeting as %s" % downloadFile)
if args.stream:
ffmpeg_outputs.extend(shlex.split(args.ffmpeg_stream_options))
ffmpeg_outputs.extend([
"-f", "flv",
"-flvflags", "no_duration_filesize",
args.target,
])
logging.info("streaming meeting...")
ffmpeg_cmd = [
"ffmpeg",
"-thread_queue_size", str(args.ffmpeg_input_thread_queue_size),
"-f", "x11grab",
"-draw_mouse", "0",
"-s", args.resolution,
"-i", ":122",
"-thread_queue_size", str(args.ffmpeg_input_thread_queue_size),
"-f", "pulse",
"-i", "default",
"-ac", "2",
] + ffmpeg_outputs
logging.debug('Preparing to execute %r' % ffmpeg_cmd)
p = subprocess.call(ffmpeg_cmd)
if args.startMeeting is False:
while bbb.is_meeting_running(args.id).is_meeting_running() != True:
logging.info("Meeting isn't running. We will try again in %d seconds!" % connect_timeout)
time.sleep(connect_timeout)
# current date and time
now = datetime.now()
fileTimeStamp = now.strftime("%Y%m%d%H%M%S")
set_up()
if args.stream and args.intro:
stream_intro()
if args.stream or args.download:
bbb_browser()
stream()
if browser:
browser.quit()