-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.py
129 lines (91 loc) · 2.98 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Multi site streaming
"""
YOUTUBE_URL = "rtmp://a.rtmp.youtube.com/live2"
TWITCH_URL = "rtmp://twitch.com"
DASH_URL = "rtmp://localhost/dash"
SOURCE_URL = "rtmp://localhost/stream"
processes = []
import sys, subprocess, signal, MySQLdb
class Database():
"""Object contenant la connexion à une base SQL"""
def __init__(self, db_name):
try:
self.db_sql = MySQLdb.connect('localhost', 'username', 'password', db_name)
print("Connexion à la base %s établie" % db_name)
except MySQLdb.Error as error:
print("Erreur %d: %s" % (error.args[0], error.args[1]))
sys.exit(1)
self.cursor = self.db_sql.cursor(MySQLdb.cursors.DictCursor)
def query(self, secret):
"""Exécution d'une query"""
self.cursor.execute(" SELECT pseudo FROM users WHERE secret = %s", (secret,))
rows = self.cursor.fetchall()
return rows
def execute(self, statement):
"""Exécution d'un statement"""
cursor = self.db_sql.cursor()
try:
cursor.execute(statement)
except MySQLdb.Error as error:
print("Erreur %d: %s" % (error.args[0], error.args[1]))
finally:
print("Number of rows updated:", cursor.rowcount)
def parse_name(name):
"""Parse the name to get the destination streams"""
destinations = name.split(",")
streams = []
for destination in destinations:
if len(destination) < 2:
continue
streams.append(destination)
return streams
def get_username(secret):
"""Get the username from the secret"""
db = Database('gign')
user = db.query(secret)
if len(user) == 1:
return user[0]['pseudo']
return 'th3o.smith'
def get_url(stream):
"""Get the url to send to from the stream"""
prefix = stream[0:2]
if prefix == "d:":
username = get_username(stream[2:])
if username is None:
return None
return DASH_URL + "/" + username
elif prefix == "t:":
return TWITCH_URL + "/" + stream[2:]
elif prefix == "y:":
return YOUTUBE_URL + "/" + stream[2:]
def spawn(destination, source):
"""Spawn a ffmpeg to redirect the flow"""
print("Spawning " + destination)
args = ["avconv", "-i", source, "-codec", "copy", "-f", "flv", destination]
p = subprocess.Popen(args)
return p
def main():
"""Entry Point"""
name = ""
if len(sys.argv) > 1:
name = sys.argv[1]
streams = parse_name(name)
source = SOURCE_URL + "/" + name
for s in streams:
dest = get_url(s)
if dest is None:
continue
processes.append(spawn(dest, source))
for p in processes:
p.wait()
def signal_term_handler(signal, frame):
print('got SIGTERM')
for p in processes:
p.kill()
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGTERM, signal_term_handler)
main()