-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcapture.py
More file actions
103 lines (91 loc) · 2.82 KB
/
capture.py
File metadata and controls
103 lines (91 loc) · 2.82 KB
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
import os
import socket
import subprocess
import time
import sys
class Streamer:
command = 'ffmpeg ' \
'-y ' \
'-f avfoundation ' \
'-r 30 ' \
'-pixel_format bgr0 ' \
'-s 640x480 ' \
'-video_device_index 0 ' \
'-i ":0" ' \
'-c:v libvpx ' \
'-b:v 1M ' \
'-c:a libvorbis ' \
'-b:a 96k ' \
'-deadline realtime ' \
'-flags +global_header ' \
'-cpu-used 1 ' \
'-threads 8 ' \
'-f segment ' \
'-f webm ' \
'-'
__connected = False
def __init__(self, host, port):
self.server = None
self.host = host
self.port = port
def connect(self):
while True:
try:
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Connecting to {}:{}...'.format(self.host, self.port))
self.server.connect((self.host, self.port))
self.__connected = True
print('Connected.\n')
break
except ConnectionRefusedError:
print('Connection refused. Retrying in 3 seconds.\n')
time.sleep(3)
def stream(self):
p = subprocess.Popen(self.command.split(), stdin=open(os.devnull), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print('Streaming...\n')
while True:
data = p.stdout.read(1024)
if len(data) == 0:
err = p.stderr.readlines()
if len(err) > 0:
print('Error')
print(''.join([l.decode() for l in err]))
break
try:
self.server.send(data)
except BrokenPipeError:
print('Disconnected from server. Reconnecting in 3 seconds\n')
time.sleep(3)
self.connect()
print('Streaming...\n')
def close(self):
if not self.connected:
return
print('Disconnected.')
self.server.close()
@property
def connected(self):
return self.__connected
if __name__ == '__main__':
try:
host = sys.argv[1]
except IndexError:
host = 'localhost'
try:
port = int(sys.argv[2])
except IndexError:
port = 8889
except ValueError:
print('Invalid port.')
exit(1)
streamer = Streamer(host, port)
try:
streamer.connect()
streamer.stream()
except KeyboardInterrupt:
print('Exiting...')
except OSError as e:
print(e)
exit(1)
finally:
streamer.close()