%global _empty_manifest_terminate_build 0 Name: python-pylivestream Version: 2.0.0 Release: 1 Summary: Livestream using FFmpeg to YouTube Live, Twitter, Facebook Live, Twitch, and more License: Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. URL: https://pypi.org/project/pylivestream/ Source0: https://mirrors.nju.edu.cn/pypi/web/packages/64/4a/70231a42eb2ed429a4f3c4dc74c8a54275f0623fa3c93700632b923e6012/pylivestream-2.0.0.tar.gz BuildArch: noarch Requires: python3-tinytag Requires: python3-flake8 Requires: python3-flake8-bugbear Requires: python3-flake8-builtins Requires: python3-flake8-blind-except Requires: python3-mypy Requires: python3-pytest Requires: python3-pytest-timeout %description # Python scripted livestreaming using FFmpeg [![DOI](https://zenodo.org/badge/91214767.svg)](https://zenodo.org/badge/latestdoi/91214767) ![Actions Status](https://github.com/scivision/pylivestream/workflows/ci/badge.svg) [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/scivision/PyLivestream.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/scivision/PyLivestream/context:python) [![pypi versions](https://img.shields.io/pypi/pyversions/PyLivestream.svg)](https://pypi.python.org/pypi/PyLivestream) [![PyPi Download stats](https://static.pepy.tech/badge/pylivestream)](https://pepy.tech/project/pylivestream) Streams to one or **multiple** streaming sites simultaneously, using pure object-oriented Python (no extra packages) and FFmpeg. Tested with `flake8`, `mypy` type checking and `pytest`. Python >= 3.9 is recommended for full capabilities. `visual_tests.py` is a quick check of several command line scripting scenarios on your laptop. FFmpeg is used from Python `subprocess` to stream to sites including: * Facebook Live (requires FFmpeg >= 4.2 due to mandatory RTMPS) * YouTube Live * Twitch * also Ustream, Vimeo, Restream.io and more for streaming broadcasts. ![PyLivestream diagram showing screen capture or camera simultaneously livestreaming to multiple services.](./doc/logo.png) [Troubleshooting](./Troubleshooting.md) ## PyLivestream benefits * Python scripts compute good streaming parameters, and emit the command used to copy and paste if desired. * Works on any OS (Mac, Linux, Windows) and computing platform, including PC, Mac, and Raspberry Pi. * Uses single JSON file pylivestream.json to adjust parameters. ### PyLivestream limitations * does *not* auto-restart if network connection glitches * is intended as a bare minimum command generator to run the FFmpeg program * is not intended for bidirectional robust streaming--consider a program/system based on Jitsi for that. ### Design rationale Why not do things without the command line, via linking libffmpeg, libgstreamer or libav? * the command-line approach does not require a compiler or OS-dependent libraries * once you get a setup working once, you don't even need Python anymore--just copy and paste the command line ### Alternatives Other projects using FFmpeg from Python include: * [python-ffmpeg](https://github.com/jonghwanhyeon/python-ffmpeg) lower level use of FFmpeg with Python asyncio * [asyncio-subprocess-ffpmeg](https://github.com/scivision/asyncio-subprocess-ffmpeg) simple asyncio subprocess example that could also be used as a template for general asyncio subprocess Python use. * [ffmpy](https://github.com/Ch00k/ffmpy) FFmpeg subprocess without asyncio ## Install Requires FFmpeg ≥ 3.0 (≥ 4.2 for Facebook Live RTMPS) Latest release: ```sh python3 -m pip install PyLivestream ``` Development version: ```sh git clone https://github.com/scivision/PyLivestream cd PyLivestream python3 -m pip install -e . ``` ## Configuration: pylivestream.json You can skip past this section to "stream start" if it's confusing. The defaults might work to get you started. The pylivestream.json file you create has parameters relevant to the live stream. We suggest copying the example [pylivestream.json](./src/pylivestream/data/pylivestream.json) and editing, then specify it for your streams. * `screencap_origin`: origin (upper left corner) of screen capture region in pixels. * `screencap_size`: resolution of screen capture (area to capture, starting from origin) * `screencap_fps`: frames/sec of screen capture * `video_kbps`: override automatic video bitrate in kbps * `audio_rate`: audio sampling frequency. Typically 44100 Hz (CD quality). * `audio_bps`: audio data rate--**leave blank if you want no audio** (usually used for "file", to make an animated GIF in post-processing) * `preset`: `veryfast` or `ultrafast` if CPU not able to keep up. * `exe`: override path to desired FFmpeg executable. In case you have multiple FFmpeg versions installed (say, from Anaconda Python). Next are `sys.platform` specific parameters. Seek help in FFmpeg documentation, try capturing to a file first and then update ~/pylivestream.json for `sys.platform`. ### Deduce inputs Each computer will need distinct pylivestream.json device input parameters: * audio_chan: audio device * camera_chan: camera device * screen_chan: desktop capture software port name Loopback devices that let you "record what you hear" are operating system dependent. You may need to search documentation for your operating system to enable such a virtual loopback device. #### Windows ```sh ffmpeg -list_devices true -f dshow -i dummy ``` #### MacOS ```sh ffmpeg -f avfoundation -list_devices true -i "" ``` #### Linux ```sh v4l2-ctl --list-devices ``` ## API There are two ways to start a stream (assuming you've configured as per following sections). Both do the same thing. * command line * python -m pylivestream.glob * python -m pylivestream.screen * python -m pylivestream.loopfile * python -m pylivestream.screen2disk * python -m pylivestream.camera * python -m pylivestream.microphone * `import pylivestream.api as pls` from within your Python script. For more information type `help(pls)` or `help(pls.stream_microphone)` * pls.stream_file() * pls.stream_microphone() * pls.stream_camera() ## Authentication The program loads a JSON file with the stream URL and hexadecimal stream key for the website(s) used. The user must specify this JSON file location. ### YouTube Live 1. [configure](https://www.youtube.com/live_dashboard) YouTube Live. 2. Edit "pylivestream.json" to have the YouTube streamid 3. Run Python script and chosen input will stream on YouTube Live. ```sh python -m pylivestream.screen youtube ./pylivestream.json ``` ### Facebook Live Facebook Live requires FFmpeg >= 4.2 due to mandatory RTMPS 1. configure your Facebook Live stream 2. Put [stream ID](https://www.facebook.com/live/create) into the JSON file 3. Run Python script for Facebook with chosen input ```sh python -m pylivestream.screen facebook ./pylivestream.json ``` ### Twitter TODO ### Twitch Create stream from [Twitch Dashboard](https://dashboard.twitch.tv/settings/channel#stream-preferences). Edit pylivestream.json file with "url" and "streamid" for Twitch. Run Python script for Twitch with chosen input: ```sh python -m pylivestream.screen twitch ./pylivestream.json ``` ## Usage Due to the complexity of streaming and the non-specific error codes FFmpeg emits, the default behavior is that if FFmpeg detects one stream has failed, ALL streams will stop streaming and the program ends. Setup a pylivestream.json for computer and desired parameters. Copy the provided [pylivestream.json](./src/pylivestream/data/pylivestream.json) and edit with values you determine. [File-Streaming](./File-Streaming.md) ### Camera Note: your system may not have a camera, particularly if it's a virtual machine. JSON: * `camera_size`: camera resolution -- find from `v4l2-ctl --list-formats-ext` or camera spec sheet. * `camera_fps`: camera fps -- found from command above or camera spec sheet Stream to multiple sites, in this example Facebook Live and YouTube Live simultaneously: ```sh python -m pylivestream.camera youtube facebook ./pylivestream.json ``` ### Screen Share Livestream Stream to multiple sites, in this example Facebook Live and YouTube Live simultaneously: ```sh python -m pylivestream.screen youtube facebook ./pylivestream.json ``` ### Image + Audio Livestream Microphone audio + static image is accomplished by ```sh python -m pylivestream.microphone youtube facebook ./pylivestream.json -image doc/logo.jpg ``` or wherever your image file is. ### Audio-only Livestream Audio-only streaming is not typically allowed by the Video streaming sites. It may fail to work altogether, or may fail when one file is done and another starts. That's not something we can fix within the scope of this project. You can test it to your own computer by: ```sh python -m pylivestream.microphone localhost ./pylivestream.json ``` ### Screen capture to disk This script saves your screen capture to a file on your disk: ```sh python -m pylivestream.screen2disk myvid.avi ./pylivestream.json ``` ## Utilities * `PyLivestream.get_framerate(vidfn)` gives the frames/sec of a video file. * `PyLivestream.get_resolution(vidfn)` gives the resolution (width x height) of video file. ## Notes * Linux requires X11, not Wayland (choose at login) * `x11grab` was deprecated in FFmpeg 3.3, was previously replaced by `xcbgrab` * Reference [webpage](https://www.scivision.dev/youtube-live-ffmpeg-livestream/) ### FFmpeg References * [streaming](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites) * [camera](https://trac.ffmpeg.org/wiki/Capture/Webcam) * Camera [overlay](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites#Withwebcamoverlay) ### Windows * [gdigrab](https://ffmpeg.org/ffmpeg-devices.html#gdigrab) DirectShow didn't work for me on Windows 10, so I used gdigrab instead. * [DirectShow](https://trac.ffmpeg.org/wiki/DirectShow) device selection * DirectShow [examples](https://ffmpeg.org/ffmpeg-devices.html#Examples-4) ### Stream References * [Twitch parameters](https://help.twitch.tv/customer/portal/articles/1253460-broadcast-requirements) * Twitch [ingest servers](https://stream.twitch.tv/ingests/) * Twitch [encoding](https://stream.twitch.tv/encoding/) * [Twitter Live parameters](https://help.twitter.com/en/using-twitter/twitter-live) * [YouTube Live parameters](https://support.google.com/youtube/answer/2853702) * [Facebook Live parameters](https://www.facebook.com/facebookmedia/get-started/live) * [Ustream parameters](https://support.ustream.tv/hc/en-us/articles/207852117-Internet-connection-and-recommended-encoding-settings) * Vimeo [config](https://help.vimeo.com/hc/en-us/articles/115012811168) * Vimeo [parameters](https://help.vimeo.com/hc/en-us/articles/115012811208-Encoder-guides) ### Logo Credits * Owl PC: Creative Commons no attrib. commercial * YouTube: YouTube Brand Resources * Facebook: Wikimedia Commons %package -n python3-pylivestream Summary: Livestream using FFmpeg to YouTube Live, Twitter, Facebook Live, Twitch, and more Provides: python-pylivestream BuildRequires: python3-devel BuildRequires: python3-setuptools BuildRequires: python3-pip %description -n python3-pylivestream # Python scripted livestreaming using FFmpeg [![DOI](https://zenodo.org/badge/91214767.svg)](https://zenodo.org/badge/latestdoi/91214767) ![Actions Status](https://github.com/scivision/pylivestream/workflows/ci/badge.svg) [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/scivision/PyLivestream.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/scivision/PyLivestream/context:python) [![pypi versions](https://img.shields.io/pypi/pyversions/PyLivestream.svg)](https://pypi.python.org/pypi/PyLivestream) [![PyPi Download stats](https://static.pepy.tech/badge/pylivestream)](https://pepy.tech/project/pylivestream) Streams to one or **multiple** streaming sites simultaneously, using pure object-oriented Python (no extra packages) and FFmpeg. Tested with `flake8`, `mypy` type checking and `pytest`. Python >= 3.9 is recommended for full capabilities. `visual_tests.py` is a quick check of several command line scripting scenarios on your laptop. FFmpeg is used from Python `subprocess` to stream to sites including: * Facebook Live (requires FFmpeg >= 4.2 due to mandatory RTMPS) * YouTube Live * Twitch * also Ustream, Vimeo, Restream.io and more for streaming broadcasts. ![PyLivestream diagram showing screen capture or camera simultaneously livestreaming to multiple services.](./doc/logo.png) [Troubleshooting](./Troubleshooting.md) ## PyLivestream benefits * Python scripts compute good streaming parameters, and emit the command used to copy and paste if desired. * Works on any OS (Mac, Linux, Windows) and computing platform, including PC, Mac, and Raspberry Pi. * Uses single JSON file pylivestream.json to adjust parameters. ### PyLivestream limitations * does *not* auto-restart if network connection glitches * is intended as a bare minimum command generator to run the FFmpeg program * is not intended for bidirectional robust streaming--consider a program/system based on Jitsi for that. ### Design rationale Why not do things without the command line, via linking libffmpeg, libgstreamer or libav? * the command-line approach does not require a compiler or OS-dependent libraries * once you get a setup working once, you don't even need Python anymore--just copy and paste the command line ### Alternatives Other projects using FFmpeg from Python include: * [python-ffmpeg](https://github.com/jonghwanhyeon/python-ffmpeg) lower level use of FFmpeg with Python asyncio * [asyncio-subprocess-ffpmeg](https://github.com/scivision/asyncio-subprocess-ffmpeg) simple asyncio subprocess example that could also be used as a template for general asyncio subprocess Python use. * [ffmpy](https://github.com/Ch00k/ffmpy) FFmpeg subprocess without asyncio ## Install Requires FFmpeg ≥ 3.0 (≥ 4.2 for Facebook Live RTMPS) Latest release: ```sh python3 -m pip install PyLivestream ``` Development version: ```sh git clone https://github.com/scivision/PyLivestream cd PyLivestream python3 -m pip install -e . ``` ## Configuration: pylivestream.json You can skip past this section to "stream start" if it's confusing. The defaults might work to get you started. The pylivestream.json file you create has parameters relevant to the live stream. We suggest copying the example [pylivestream.json](./src/pylivestream/data/pylivestream.json) and editing, then specify it for your streams. * `screencap_origin`: origin (upper left corner) of screen capture region in pixels. * `screencap_size`: resolution of screen capture (area to capture, starting from origin) * `screencap_fps`: frames/sec of screen capture * `video_kbps`: override automatic video bitrate in kbps * `audio_rate`: audio sampling frequency. Typically 44100 Hz (CD quality). * `audio_bps`: audio data rate--**leave blank if you want no audio** (usually used for "file", to make an animated GIF in post-processing) * `preset`: `veryfast` or `ultrafast` if CPU not able to keep up. * `exe`: override path to desired FFmpeg executable. In case you have multiple FFmpeg versions installed (say, from Anaconda Python). Next are `sys.platform` specific parameters. Seek help in FFmpeg documentation, try capturing to a file first and then update ~/pylivestream.json for `sys.platform`. ### Deduce inputs Each computer will need distinct pylivestream.json device input parameters: * audio_chan: audio device * camera_chan: camera device * screen_chan: desktop capture software port name Loopback devices that let you "record what you hear" are operating system dependent. You may need to search documentation for your operating system to enable such a virtual loopback device. #### Windows ```sh ffmpeg -list_devices true -f dshow -i dummy ``` #### MacOS ```sh ffmpeg -f avfoundation -list_devices true -i "" ``` #### Linux ```sh v4l2-ctl --list-devices ``` ## API There are two ways to start a stream (assuming you've configured as per following sections). Both do the same thing. * command line * python -m pylivestream.glob * python -m pylivestream.screen * python -m pylivestream.loopfile * python -m pylivestream.screen2disk * python -m pylivestream.camera * python -m pylivestream.microphone * `import pylivestream.api as pls` from within your Python script. For more information type `help(pls)` or `help(pls.stream_microphone)` * pls.stream_file() * pls.stream_microphone() * pls.stream_camera() ## Authentication The program loads a JSON file with the stream URL and hexadecimal stream key for the website(s) used. The user must specify this JSON file location. ### YouTube Live 1. [configure](https://www.youtube.com/live_dashboard) YouTube Live. 2. Edit "pylivestream.json" to have the YouTube streamid 3. Run Python script and chosen input will stream on YouTube Live. ```sh python -m pylivestream.screen youtube ./pylivestream.json ``` ### Facebook Live Facebook Live requires FFmpeg >= 4.2 due to mandatory RTMPS 1. configure your Facebook Live stream 2. Put [stream ID](https://www.facebook.com/live/create) into the JSON file 3. Run Python script for Facebook with chosen input ```sh python -m pylivestream.screen facebook ./pylivestream.json ``` ### Twitter TODO ### Twitch Create stream from [Twitch Dashboard](https://dashboard.twitch.tv/settings/channel#stream-preferences). Edit pylivestream.json file with "url" and "streamid" for Twitch. Run Python script for Twitch with chosen input: ```sh python -m pylivestream.screen twitch ./pylivestream.json ``` ## Usage Due to the complexity of streaming and the non-specific error codes FFmpeg emits, the default behavior is that if FFmpeg detects one stream has failed, ALL streams will stop streaming and the program ends. Setup a pylivestream.json for computer and desired parameters. Copy the provided [pylivestream.json](./src/pylivestream/data/pylivestream.json) and edit with values you determine. [File-Streaming](./File-Streaming.md) ### Camera Note: your system may not have a camera, particularly if it's a virtual machine. JSON: * `camera_size`: camera resolution -- find from `v4l2-ctl --list-formats-ext` or camera spec sheet. * `camera_fps`: camera fps -- found from command above or camera spec sheet Stream to multiple sites, in this example Facebook Live and YouTube Live simultaneously: ```sh python -m pylivestream.camera youtube facebook ./pylivestream.json ``` ### Screen Share Livestream Stream to multiple sites, in this example Facebook Live and YouTube Live simultaneously: ```sh python -m pylivestream.screen youtube facebook ./pylivestream.json ``` ### Image + Audio Livestream Microphone audio + static image is accomplished by ```sh python -m pylivestream.microphone youtube facebook ./pylivestream.json -image doc/logo.jpg ``` or wherever your image file is. ### Audio-only Livestream Audio-only streaming is not typically allowed by the Video streaming sites. It may fail to work altogether, or may fail when one file is done and another starts. That's not something we can fix within the scope of this project. You can test it to your own computer by: ```sh python -m pylivestream.microphone localhost ./pylivestream.json ``` ### Screen capture to disk This script saves your screen capture to a file on your disk: ```sh python -m pylivestream.screen2disk myvid.avi ./pylivestream.json ``` ## Utilities * `PyLivestream.get_framerate(vidfn)` gives the frames/sec of a video file. * `PyLivestream.get_resolution(vidfn)` gives the resolution (width x height) of video file. ## Notes * Linux requires X11, not Wayland (choose at login) * `x11grab` was deprecated in FFmpeg 3.3, was previously replaced by `xcbgrab` * Reference [webpage](https://www.scivision.dev/youtube-live-ffmpeg-livestream/) ### FFmpeg References * [streaming](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites) * [camera](https://trac.ffmpeg.org/wiki/Capture/Webcam) * Camera [overlay](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites#Withwebcamoverlay) ### Windows * [gdigrab](https://ffmpeg.org/ffmpeg-devices.html#gdigrab) DirectShow didn't work for me on Windows 10, so I used gdigrab instead. * [DirectShow](https://trac.ffmpeg.org/wiki/DirectShow) device selection * DirectShow [examples](https://ffmpeg.org/ffmpeg-devices.html#Examples-4) ### Stream References * [Twitch parameters](https://help.twitch.tv/customer/portal/articles/1253460-broadcast-requirements) * Twitch [ingest servers](https://stream.twitch.tv/ingests/) * Twitch [encoding](https://stream.twitch.tv/encoding/) * [Twitter Live parameters](https://help.twitter.com/en/using-twitter/twitter-live) * [YouTube Live parameters](https://support.google.com/youtube/answer/2853702) * [Facebook Live parameters](https://www.facebook.com/facebookmedia/get-started/live) * [Ustream parameters](https://support.ustream.tv/hc/en-us/articles/207852117-Internet-connection-and-recommended-encoding-settings) * Vimeo [config](https://help.vimeo.com/hc/en-us/articles/115012811168) * Vimeo [parameters](https://help.vimeo.com/hc/en-us/articles/115012811208-Encoder-guides) ### Logo Credits * Owl PC: Creative Commons no attrib. commercial * YouTube: YouTube Brand Resources * Facebook: Wikimedia Commons %package help Summary: Development documents and examples for pylivestream Provides: python3-pylivestream-doc %description help # Python scripted livestreaming using FFmpeg [![DOI](https://zenodo.org/badge/91214767.svg)](https://zenodo.org/badge/latestdoi/91214767) ![Actions Status](https://github.com/scivision/pylivestream/workflows/ci/badge.svg) [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/scivision/PyLivestream.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/scivision/PyLivestream/context:python) [![pypi versions](https://img.shields.io/pypi/pyversions/PyLivestream.svg)](https://pypi.python.org/pypi/PyLivestream) [![PyPi Download stats](https://static.pepy.tech/badge/pylivestream)](https://pepy.tech/project/pylivestream) Streams to one or **multiple** streaming sites simultaneously, using pure object-oriented Python (no extra packages) and FFmpeg. Tested with `flake8`, `mypy` type checking and `pytest`. Python >= 3.9 is recommended for full capabilities. `visual_tests.py` is a quick check of several command line scripting scenarios on your laptop. FFmpeg is used from Python `subprocess` to stream to sites including: * Facebook Live (requires FFmpeg >= 4.2 due to mandatory RTMPS) * YouTube Live * Twitch * also Ustream, Vimeo, Restream.io and more for streaming broadcasts. ![PyLivestream diagram showing screen capture or camera simultaneously livestreaming to multiple services.](./doc/logo.png) [Troubleshooting](./Troubleshooting.md) ## PyLivestream benefits * Python scripts compute good streaming parameters, and emit the command used to copy and paste if desired. * Works on any OS (Mac, Linux, Windows) and computing platform, including PC, Mac, and Raspberry Pi. * Uses single JSON file pylivestream.json to adjust parameters. ### PyLivestream limitations * does *not* auto-restart if network connection glitches * is intended as a bare minimum command generator to run the FFmpeg program * is not intended for bidirectional robust streaming--consider a program/system based on Jitsi for that. ### Design rationale Why not do things without the command line, via linking libffmpeg, libgstreamer or libav? * the command-line approach does not require a compiler or OS-dependent libraries * once you get a setup working once, you don't even need Python anymore--just copy and paste the command line ### Alternatives Other projects using FFmpeg from Python include: * [python-ffmpeg](https://github.com/jonghwanhyeon/python-ffmpeg) lower level use of FFmpeg with Python asyncio * [asyncio-subprocess-ffpmeg](https://github.com/scivision/asyncio-subprocess-ffmpeg) simple asyncio subprocess example that could also be used as a template for general asyncio subprocess Python use. * [ffmpy](https://github.com/Ch00k/ffmpy) FFmpeg subprocess without asyncio ## Install Requires FFmpeg ≥ 3.0 (≥ 4.2 for Facebook Live RTMPS) Latest release: ```sh python3 -m pip install PyLivestream ``` Development version: ```sh git clone https://github.com/scivision/PyLivestream cd PyLivestream python3 -m pip install -e . ``` ## Configuration: pylivestream.json You can skip past this section to "stream start" if it's confusing. The defaults might work to get you started. The pylivestream.json file you create has parameters relevant to the live stream. We suggest copying the example [pylivestream.json](./src/pylivestream/data/pylivestream.json) and editing, then specify it for your streams. * `screencap_origin`: origin (upper left corner) of screen capture region in pixels. * `screencap_size`: resolution of screen capture (area to capture, starting from origin) * `screencap_fps`: frames/sec of screen capture * `video_kbps`: override automatic video bitrate in kbps * `audio_rate`: audio sampling frequency. Typically 44100 Hz (CD quality). * `audio_bps`: audio data rate--**leave blank if you want no audio** (usually used for "file", to make an animated GIF in post-processing) * `preset`: `veryfast` or `ultrafast` if CPU not able to keep up. * `exe`: override path to desired FFmpeg executable. In case you have multiple FFmpeg versions installed (say, from Anaconda Python). Next are `sys.platform` specific parameters. Seek help in FFmpeg documentation, try capturing to a file first and then update ~/pylivestream.json for `sys.platform`. ### Deduce inputs Each computer will need distinct pylivestream.json device input parameters: * audio_chan: audio device * camera_chan: camera device * screen_chan: desktop capture software port name Loopback devices that let you "record what you hear" are operating system dependent. You may need to search documentation for your operating system to enable such a virtual loopback device. #### Windows ```sh ffmpeg -list_devices true -f dshow -i dummy ``` #### MacOS ```sh ffmpeg -f avfoundation -list_devices true -i "" ``` #### Linux ```sh v4l2-ctl --list-devices ``` ## API There are two ways to start a stream (assuming you've configured as per following sections). Both do the same thing. * command line * python -m pylivestream.glob * python -m pylivestream.screen * python -m pylivestream.loopfile * python -m pylivestream.screen2disk * python -m pylivestream.camera * python -m pylivestream.microphone * `import pylivestream.api as pls` from within your Python script. For more information type `help(pls)` or `help(pls.stream_microphone)` * pls.stream_file() * pls.stream_microphone() * pls.stream_camera() ## Authentication The program loads a JSON file with the stream URL and hexadecimal stream key for the website(s) used. The user must specify this JSON file location. ### YouTube Live 1. [configure](https://www.youtube.com/live_dashboard) YouTube Live. 2. Edit "pylivestream.json" to have the YouTube streamid 3. Run Python script and chosen input will stream on YouTube Live. ```sh python -m pylivestream.screen youtube ./pylivestream.json ``` ### Facebook Live Facebook Live requires FFmpeg >= 4.2 due to mandatory RTMPS 1. configure your Facebook Live stream 2. Put [stream ID](https://www.facebook.com/live/create) into the JSON file 3. Run Python script for Facebook with chosen input ```sh python -m pylivestream.screen facebook ./pylivestream.json ``` ### Twitter TODO ### Twitch Create stream from [Twitch Dashboard](https://dashboard.twitch.tv/settings/channel#stream-preferences). Edit pylivestream.json file with "url" and "streamid" for Twitch. Run Python script for Twitch with chosen input: ```sh python -m pylivestream.screen twitch ./pylivestream.json ``` ## Usage Due to the complexity of streaming and the non-specific error codes FFmpeg emits, the default behavior is that if FFmpeg detects one stream has failed, ALL streams will stop streaming and the program ends. Setup a pylivestream.json for computer and desired parameters. Copy the provided [pylivestream.json](./src/pylivestream/data/pylivestream.json) and edit with values you determine. [File-Streaming](./File-Streaming.md) ### Camera Note: your system may not have a camera, particularly if it's a virtual machine. JSON: * `camera_size`: camera resolution -- find from `v4l2-ctl --list-formats-ext` or camera spec sheet. * `camera_fps`: camera fps -- found from command above or camera spec sheet Stream to multiple sites, in this example Facebook Live and YouTube Live simultaneously: ```sh python -m pylivestream.camera youtube facebook ./pylivestream.json ``` ### Screen Share Livestream Stream to multiple sites, in this example Facebook Live and YouTube Live simultaneously: ```sh python -m pylivestream.screen youtube facebook ./pylivestream.json ``` ### Image + Audio Livestream Microphone audio + static image is accomplished by ```sh python -m pylivestream.microphone youtube facebook ./pylivestream.json -image doc/logo.jpg ``` or wherever your image file is. ### Audio-only Livestream Audio-only streaming is not typically allowed by the Video streaming sites. It may fail to work altogether, or may fail when one file is done and another starts. That's not something we can fix within the scope of this project. You can test it to your own computer by: ```sh python -m pylivestream.microphone localhost ./pylivestream.json ``` ### Screen capture to disk This script saves your screen capture to a file on your disk: ```sh python -m pylivestream.screen2disk myvid.avi ./pylivestream.json ``` ## Utilities * `PyLivestream.get_framerate(vidfn)` gives the frames/sec of a video file. * `PyLivestream.get_resolution(vidfn)` gives the resolution (width x height) of video file. ## Notes * Linux requires X11, not Wayland (choose at login) * `x11grab` was deprecated in FFmpeg 3.3, was previously replaced by `xcbgrab` * Reference [webpage](https://www.scivision.dev/youtube-live-ffmpeg-livestream/) ### FFmpeg References * [streaming](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites) * [camera](https://trac.ffmpeg.org/wiki/Capture/Webcam) * Camera [overlay](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites#Withwebcamoverlay) ### Windows * [gdigrab](https://ffmpeg.org/ffmpeg-devices.html#gdigrab) DirectShow didn't work for me on Windows 10, so I used gdigrab instead. * [DirectShow](https://trac.ffmpeg.org/wiki/DirectShow) device selection * DirectShow [examples](https://ffmpeg.org/ffmpeg-devices.html#Examples-4) ### Stream References * [Twitch parameters](https://help.twitch.tv/customer/portal/articles/1253460-broadcast-requirements) * Twitch [ingest servers](https://stream.twitch.tv/ingests/) * Twitch [encoding](https://stream.twitch.tv/encoding/) * [Twitter Live parameters](https://help.twitter.com/en/using-twitter/twitter-live) * [YouTube Live parameters](https://support.google.com/youtube/answer/2853702) * [Facebook Live parameters](https://www.facebook.com/facebookmedia/get-started/live) * [Ustream parameters](https://support.ustream.tv/hc/en-us/articles/207852117-Internet-connection-and-recommended-encoding-settings) * Vimeo [config](https://help.vimeo.com/hc/en-us/articles/115012811168) * Vimeo [parameters](https://help.vimeo.com/hc/en-us/articles/115012811208-Encoder-guides) ### Logo Credits * Owl PC: Creative Commons no attrib. commercial * YouTube: YouTube Brand Resources * Facebook: Wikimedia Commons %prep %autosetup -n pylivestream-2.0.0 %build %py3_build %install %py3_install install -d -m755 %{buildroot}/%{_pkgdocdir} if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi pushd %{buildroot} if [ -d usr/lib ]; then find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/lib64 ]; then find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/bin ]; then find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/sbin ]; then find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst fi touch doclist.lst if [ -d usr/share/man ]; then find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst fi popd mv %{buildroot}/filelist.lst . mv %{buildroot}/doclist.lst . %files -n python3-pylivestream -f filelist.lst %dir %{python3_sitelib}/* %files help -f doclist.lst %{_docdir}/* %changelog * Thu May 18 2023 Python_Bot - 2.0.0-1 - Package Spec generated