<commit_before>.. image:: https://readthedocs.org/projects/python-twitch-stream/badge/
    :target: http://python-twitch-stream.readthedocs.org/en/latest/

.. image:: https://travis-ci.org/317070/python-twitch-stream.svg
    :target: https://travis-ci.org/317070/python-twitch-stream

.. image:: https://coveralls.io/repos/317070/python-twitch-stream/badge.svg
    :target: https://coveralls.io/github/317070/python-twitch-stream

.. image:: https://img.shields.io/badge/license-MIT-green.svg
    :target: https://github.com/Lasagne/Lasagne/blob/master/LICENSE

Python-Twitch-Stream
====================

Python-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.


Installation
------------

In short, you can install a known compatible version of ffmpeg and
 the latest stable version over pip.

.. code-block:: bash

  pip install python-twitch-stream
  sudo add-apt-repository ppa:mc3man/trusty-media
  sudo apt-get update && sudo apt-get install ffmpeg

Or alternatively, install the latest
python-twitch-stream development version via:

.. code-block:: bash

  pip install git+https://github.com/317070/python-twitch-stream
  sudo add-apt-repository ppa:mc3man/trusty-media
  sudo apt-get update && sudo apt-get install ffmpeg

The ffmpeg library needs to be very recent. There are plenty of bugs when
running a stream using older versions of ffmpeg and avconv, including but
not limited too 6GB of memory use, problems with the audio and
synchronization of the audio and the video.

Documentation
-------------

Documentation is available online: http://python-twitch-stream.readthedocs.org/

For support, please use the github issues on `the repository
<https://github.com/317070/python-twitch-stream/issues>`_.


Example
-------

This is a small example which creates a twitch stream which
changes the color of the video according to the colors provided in
the chat.

.. code-block:: python

    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()
        # TODO: merge two with statements
        with TwitchChatStream(username=args.username,
                              oauth=args.oauth,
                              verbose=False) as chatstream:

            with TwitchBufferedOutputStream(
                    twitch_stream_key=args.streamkey,
                    width=640,
                    height=480,
                    fps=30.) as videostream:

                chatstream.send_chat_message("Taking requests!")

                frame = np.zeros((480, 640, 3))

                while True:
                    received = chatstream.twitch_receive_messages()
                    if received:
                        if received[0]['message'] == "black":
                            frame[:, :, :] = np.array(
                                [0, 0, 0])[None, None, :]
                        elif received[0]['message'] == "red":
                            frame[:, :, :] = np.array(
                                [1, 0, 0])[None, None, :]
                        elif received[0]['message'] == "green":
                            frame[:, :, :] = np.array(
                                [0, 1, 0])[None, None, :]
                        elif received[0]['message'] == "blue":
                            frame[:, :, :] = np.array(
                                [0, 0, 1])[None, None, :]
                        elif received[0]['message'] == "cyan":
                            frame[:, :, :] = np.array(
                                [0, 1, 1])[None, None, :]
                        elif received[0]['message'] == "magenta":
                            frame[:, :, :] = np.array(
                                [1, 0, 1])[None, None, :]
                        elif received[0]['message'] == "yellow":
                            frame[:, :, :] = np.array(
                                [1, 1, 0])[None, None, :]
                        elif received[0]['message'] == "white":
                            frame[:, :, :] = np.array(
                                [1, 1, 1])[None, None, :]

                    videostream.send_frame(frame)
                    time.sleep(1.0 / videostream.fps)



For a fully-functional example, see `examples/color.py <examples/color.py>`_,
and check the `Tutorial
<http://317070.github.io/python/>`_ for in-depth
explanations of the same. More examples are maintained in the `examples directory
<examples>`_.


Development
-----------

Python-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.<commit_msg>Updating the readme with the changes in color.py<commit_after>.. image:: https://readthedocs.org/projects/python-twitch-stream/badge/
    :target: http://python-twitch-stream.readthedocs.org/en/latest/

.. image:: https://travis-ci.org/317070/python-twitch-stream.svg
    :target: https://travis-ci.org/317070/python-twitch-stream

.. image:: https://coveralls.io/repos/317070/python-twitch-stream/badge.svg
    :target: https://coveralls.io/github/317070/python-twitch-stream

.. image:: https://img.shields.io/badge/license-MIT-green.svg
    :target: https://github.com/Lasagne/Lasagne/blob/master/LICENSE

Python-Twitch-Stream
====================

Python-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.


Installation
------------

In short, you can install a known compatible version of ffmpeg and
 the latest stable version over pip.

.. code-block:: bash

  pip install python-twitch-stream
  sudo add-apt-repository ppa:mc3man/trusty-media
  sudo apt-get update && sudo apt-get install ffmpeg

Or alternatively, install the latest
python-twitch-stream development version via:

.. code-block:: bash

  pip install git+https://github.com/317070/python-twitch-stream
  sudo add-apt-repository ppa:mc3man/trusty-media
  sudo apt-get update && sudo apt-get install ffmpeg

The ffmpeg library needs to be very recent. There are plenty of bugs when
running a stream using older versions of ffmpeg and avconv, including but
not limited too 6GB of memory use, problems with the audio and
synchronization of the audio and the video.

Documentation
-------------

Documentation is available online: http://python-twitch-stream.readthedocs.org/

For support, please use the github issues on `the repository
<https://github.com/317070/python-twitch-stream/issues>`_.


Example
-------

This is a small example which creates a twitch stream which
changes the color of the video according to the colors provided in
the chat.

.. code-block:: python

    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 segments!
                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)


For a fully-functional example, see `examples/color.py <examples/color.py>`_,
and check the `Tutorial
<http://317070.github.io/python/>`_ for in-depth
explanations of the same. More examples are maintained in the `examples directory
<examples>`_.


Development
-----------

Python-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.
{'fraction_non_alphanumeric': 0.10301097020150937, 'fraction_numerical': 0.01423792110791255, 'mean_word_length': 1.7506954846993366, 'pattern_counts': {'":': 13, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1810817', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3293, 'n_words': 1029}