-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcolor.py
More file actions
104 lines (94 loc) · 4.13 KB
/
color.py
File metadata and controls
104 lines (94 loc) · 4.13 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
104
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This is a small example which creates a twitch stream to connect with
and changes the color of the video according to the colors provided in
the chat.
"""
from __future__ import print_function
from twitchstream.outputvideo import TwitchBufferedOutputStream
from twitchstream.chat import TwitchChatStream
import argparse
import time
import numpy as np
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
required = parser.add_argument_group('required arguments')
required.add_argument('-u', '--username',
help='twitch username',
required=True)
required.add_argument('-o', '--oauth',
help='twitch oauth '
'(visit https://twitchapps.com/tmi/ '
'to create one for your account)',
required=True)
required.add_argument('-s', '--streamkey',
help='twitch streamkey',
required=True)
args = parser.parse_args()
# load two streams:
# * one stream to send the video
# * one stream to interact with the chat
with TwitchBufferedOutputStream(
twitch_stream_key=args.streamkey,
width=640,
height=480,
fps=30.,
enable_audio=True,
verbose=False) as videostream, \
TwitchChatStream(
username=args.username,
oauth=args.oauth,
verbose=False) as chatstream:
# Send a chat message to let everybody know you've arrived
chatstream.send_chat_message("Taking requests!")
frame = np.zeros((480, 640, 3))
frequency = 100
last_phase = 0
# The main loop to create videos
while True:
# Every loop, call to receive messages.
# This is important, when it is not called,
# Twitch will automatically log you out.
# This call is non-blocking.
received = chatstream.twitch_receive_messages()
# process all the messages
if received:
for chat_message in received:
print("Got a message '%s' from %s" % (
chat_message['message'],
chat_message['username']
))
if chat_message['message'] == "red":
frame[:, :, :] = np.array(
[1, 0, 0])[None, None, :]
elif chat_message['message'] == "green":
frame[:, :, :] = np.array(
[0, 1, 0])[None, None, :]
elif chat_message['message'] == "blue":
frame[:, :, :] = np.array(
[0, 0, 1])[None, None, :]
elif chat_message['message'].isdigit():
frequency = int(chat_message['message'])
# If there are not enough video frames left,
# add some more.
if videostream.get_video_frame_buffer_state() < 30:
videostream.send_video_frame(frame)
# If there are not enough audio fragments left,
# add some more, but take care to stay in sync with
# the video! Audio and video buffer separately,
# so they will go out of sync if the number of video
# frames does not match the number of audio samples!
elif videostream.get_audio_buffer_state() < 30:
x = np.linspace(last_phase,
last_phase +
frequency*2*np.pi/videostream.fps,
int(44100 / videostream.fps) + 1)
last_phase = x[-1]
audio = np.sin(x[:-1])
videostream.send_audio(audio, audio)
# If nothing is happening, it is okay to sleep for a while
# and take some pressure of the CPU. But not too long, if
# the buffers run dry, audio and video will go out of sync.
else:
time.sleep(.001)