Package: twitch-stream.py
Python-Twitch-StreamPython-twitch-stream is a simple lightweight library, which you can use to
send your python video to twitch and react with the chat in real time.
Its main features are:Supports sending of audio and video in a thread safe way to your twitch
channel.Allows to interact with the chat of your channel by sending chat messages
and reading what other users post.InstallationIn short, you can install a known compatible version of ffmpeg and
the latest stable version over pip.pipinstallpython-twitch-streamMake sure to also install a recent ffmpeg version:sudoadd-apt-repositoryppa:mc3man/trusty-mediasudoapt-getupdate&&sudoapt-getinstallffmpegThe ffmpeg library needs to be very recent (written in october 2015).
There are plenty of bugs when
running a stream using older versions of ffmpeg or avconv, including but
not limited to 6GB of memory use, problems with the audio and
synchronization of the audio and the video.Or alternatively, install the latest
python-twitch-stream development version via:pipinstallgit+https://github.com/317070/python-twitch-streamDocumentationDocumentation is available online:http://python-twitch-stream.readthedocs.org/For support, please use the github issues onthe repository.ExampleThis is a small example which creates a twitch stream which
changes the color of the video according to the colors provided in
the chat.from__future__importprint_functionfromtwitchstream.outputvideoimportTwitchBufferedOutputStreamfromtwitchstream.chatimportTwitchChatStreamimportargparseimporttimeimportnumpyasnpif__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 chatwithTwitchBufferedOutputStream(twitch_stream_key=args.streamkey,width=640,height=480,fps=30.,enable_audio=True,verbose=False)asvideostream,\TwitchChatStream(username=args.username.lower(),# Must provide a lowercase username.oauth=args.oauth,verbose=False)aschatstream:# Send a chat message to let everybody know you've arrivedchatstream.send_chat_message("Taking requests!")frame=np.zeros((480,640,3))frequency=100last_phase=0# The main loop to create videoswhileTrue:# 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 messagesifreceived:forchat_messageinreceived:print("Got a message '%s' from%s"%(chat_message['message'],chat_message['username']))ifchat_message['message']=="red":frame[:,:,:]=np.array([1,0,0])[None,None,:]elifchat_message['message']=="green":frame[:,:,:]=np.array([0,1,0])[None,None,:]elifchat_message['message']=="blue":frame[:,:,:]=np.array([0,0,1])[None,None,:]elifchat_message['message'].isdigit():frequency=int(chat_message['message'])# If there are not enough video frames left,# add some more.ifvideostream.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!elifvideostream.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)For a fully-functional example, seeexamples/color.py,
and check theTutorialfor in-depth
explanations of the same. More examples are maintained in theexamples directory.DevelopmentPython-twitch-stream is a work in progress, but is stable. Feel free to ask
for features or add pull-requests with updates on the code.Changelog1.0 (2015-10-30)First release.
Features:Sending Twitch streams (video and audio)Reading and sending Twitch chats.core contributors, in alphabetical order:Jonas Degrave (@317070)Special thanks toFrederik Tejner Witte for hisgreat tutorial!