{"id":18076213,"url":"https://github.com/scivision/pylivestream","last_synced_at":"2025-03-08T19:49:12.591Z","repository":{"id":44387294,"uuid":"91214767","full_name":"scivision/PyLivestream","owner":"scivision","description":"Pure Python FFmpeg-based live video / audio streaming to YouTube, Facebook, Periscope, Twitch, and more","archived":false,"fork":false,"pushed_at":"2025-01-09T20:47:56.000Z","size":6930,"stargazers_count":732,"open_issues_count":6,"forks_count":162,"subscribers_count":27,"default_branch":"main","last_synced_at":"2025-03-01T12:04:14.992Z","etag":null,"topics":["facebook-live","ffmpeg","livestream","periscope","twitch","vimeo","youtube","youtube-live"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scivision.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION","codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-14T02:26:40.000Z","updated_at":"2025-02-28T02:55:04.000Z","dependencies_parsed_at":"2024-06-21T05:43:37.157Z","dependency_job_id":"8887344d-3f8e-404c-8ac4-6d1c8330c69d","html_url":"https://github.com/scivision/PyLivestream","commit_stats":{"total_commits":339,"total_committers":6,"mean_commits":56.5,"dds":0.06194690265486724,"last_synced_commit":"23285ba868720ae9034ac44aae7d9e319353bb00"},"previous_names":[],"tags_count":46,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2FPyLivestream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2FPyLivestream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2FPyLivestream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2FPyLivestream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scivision","download_url":"https://codeload.github.com/scivision/PyLivestream/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242602247,"owners_count":20156376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["facebook-live","ffmpeg","livestream","periscope","twitch","vimeo","youtube","youtube-live"],"created_at":"2024-10-31T11:09:15.209Z","updated_at":"2025-03-08T19:49:12.575Z","avatar_url":"https://github.com/scivision.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python scripted livestreaming using FFmpeg\n\n[![DOI](https://zenodo.org/badge/91214767.svg)](https://zenodo.org/badge/latestdoi/91214767)\n![Actions Status](https://github.com/scivision/pylivestream/workflows/ci/badge.svg)\n[![PyPI versions](https://img.shields.io/pypi/pyversions/PyLivestream.svg)](https://pypi.python.org/pypi/PyLivestream)\n[![PyPI Download stats](https://static.pepy.tech/badge/pylivestream)](https://pepy.tech/project/pylivestream)\n\nNOTE: This project is archived effectively. Use OBS Studio etc. for livestreaming.\n\nEmits command to run FFmpeg for livestreaming video services, using pure object-oriented Python.\nTested with `flake8`, `mypy` type checking and `pytest`.\n`visual_tests.py` is a quick check of several command line scripting scenarios on your laptop.\nFFmpeg is used from Python `subprocess` to stream to sites including:\n\n* Facebook Live  (requires FFmpeg \u003e= 4.2 due to mandatory RTMPS)\n* YouTube Live\n* Twitch\n* also IBM Live Video, Vimeo, Restream.io and more for streaming broadcasts.\n\n![PyLivestream diagram showing screen capture or camera livestreaming](./doc/logo.png)\n\n[Troubleshooting](./Troubleshooting.md)\n\n## PyLivestream benefits\n\n* Python scripts compute good streaming parameters, and emit the command used to copy and paste if desired.\n* Works on any OS (Mac, Linux, Windows) and computing platform, including PC, Mac, and Raspberry Pi.\n* Uses single JSON file pylivestream.json to adjust parameters.\n\n### PyLivestream limitations\n\n* does *not* auto-restart if network connection glitches\n* is intended as a bare minimum command generator to run the FFmpeg program\n* is not intended for bidirectional robust streaming--consider a program/system based on Jitsi for that.\n\n### Design rationale\n\nWhy not do things without the command line, via linking libffmpeg, libgstreamer or libav?\n\n* the command-line approach does not require a compiler or OS-dependent libraries\n* once you get a setup working once, you don't even need Python anymore--just copy and paste the command line\n\n### Alternatives\n\nOther projects using FFmpeg from Python include:\n\n* [python-ffmpeg](https://github.com/jonghwanhyeon/python-ffmpeg) lower level use of FFmpeg with Python asyncio\n* [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.\n* [ffmpy](https://github.com/Ch00k/ffmpy) FFmpeg subprocess without asyncio\n\n## Install\n\nLatest release:\n\n```sh\npython3 -m pip install PyLivestream\n```\n\nDevelopment version:\n\n```sh\ngit clone https://github.com/scivision/PyLivestream\n\ncd PyLivestream\n\npython3 -m pip install -e .\n```\n\nFFmpeg can be obtained like:\n\n* Windows: `winget install ffmpeg`\n* Linux: `sudo apt install ffmpeg`\n* MacOS: `brew install ffmpeg`\n\nIf errors result from FFmpeg not in PATH environment variable, optionally set environment variable \"FFMPEG_ROOT\" to the directory containing FFmpeg executable.\n\n## Configuration: pylivestream.json\n\nYou can skip past this section to \"stream start\" if it's confusing.\nThe defaults might work to get you started.\n\nThe pylivestream.json file you create has parameters relevant to the live stream.\nWe suggest copying the example\n[pylivestream.json](./src/pylivestream/data/pylivestream.json)\nand editing, then specify it for your streams.\n\n* `screencap_origin`: origin (upper left corner) of screen capture region in pixels.\n* `screencap_size`: resolution of screen capture (area to capture, starting from origin)\n* `screencap_fps`: frames/sec of screen capture\n* `video_kbps`: override automatic video bitrate in kbps\n* `audio_rate`: audio sampling frequency. Typically 44100 Hz (CD quality).\n* `audio_bps`: audio data rate--**leave blank if you want no audio** (usually used for \"file\", to make an animated GIF in  post-processing)\n* `preset`: `veryfast` or `ultrafast` if CPU not able to keep up.\n* `exe`: override path to desired FFmpeg executable. In case you have multiple FFmpeg versions installed (say, from Anaconda Python).\n\nNext are `sys.platform` specific parameters.\n\nSeek help in FFmpeg documentation, try capturing to a file first and then update ~/pylivestream.json for `sys.platform`.\n\n### Deduce inputs\n\nEach computer will need distinct pylivestream.json device input parameters:\n\n* audio_chan: audio device\n* camera_chan: camera device\n* screen_chan: desktop capture software port name\n\nLoopback devices that let you \"record what you hear\" are operating system dependent.\nYou may need to search documentation for your operating system to enable such a virtual loopback device.\n\n#### Windows\n\n```sh\nffmpeg -list_devices true -f dshow -i dummy\n```\n\n#### MacOS\n\n```sh\nffmpeg -f avfoundation -list_devices true -i \"\"\n```\n\n#### Linux\n\n```sh\nv4l2-ctl --list-devices\n```\n\n## API\n\n* command line\n  * python -m pylivestream.fglob\n  * python -m pylivestream.screen\n  * python -m pylivestream.loopfile\n  * python -m pylivestream.screen2disk\n  * python -m pylivestream.camera\n  * python -m pylivestream.microphone\n* `import pylivestream.api as pls` from within your Python script. For more information type `help(pls)` or `help(pls.stream_microphone)`\n  * pls.stream_file()\n  * pls.stream_microphone()\n  * pls.stream_camera()\n\n## Authentication\n\nThe program loads a JSON file with the stream URL and hexadecimal stream key for the website(s) used.\nThe user must specify this JSON file location.\n\n### YouTube Live\n\n1. [configure](https://www.youtube.com/live_dashboard) YouTube Live.\n2. Edit \"pylivestream.json\" to have the YouTube streamid\n3. Run Python script for YouTube with chosen input\n\n```sh\npython -m pylivestream.screen youtube ./pylivestream.json\n```\n\n### Facebook Live\n\nFacebook Live requires FFmpeg \u0026ge; 4.2 due to mandatory RTMPS.\n\n1. configure a Facebook Live stream\n2. Put [stream ID](https://www.facebook.com/live/create) into the JSON file\n3. Run Python script for Facebook with chosen input\n\n```sh\npython -m pylivestream.screen facebook ./pylivestream.json\n```\n\n### Twitch\n\nCreate stream from\n[Twitch Dashboard](https://dashboard.twitch.tv/settings/channel#stream-preferences).\nEdit pylivestream.json file with \"url\" and \"streamid\" for Twitch.\nRun Python script for Twitch with chosen input:\n\n```sh\npython -m pylivestream.screen twitch ./pylivestream.json\n```\n\n## Usage\n\nSetup a pylivestream.json for computer and desired parameters.\nCopy the provided\n[pylivestream.json](./src/pylivestream/data/pylivestream.json)\nand edit with values you determine.\n\nSingle video file:\n\n```sh\npython -m pylivestream.loopfile videofile youtube\n```\n\n### Camera\n\nNote: your system may not have a camera, particularly if it's a virtual machine.\n\nJSON:\n\n* `camera_size`: camera resolution -- find from `v4l2-ctl --list-formats-ext` or camera spec sheet.\n* `camera_fps`: camera fps -- found from command above or camera spec sheet\n\n```sh\npython -m pylivestream.camera youtube ./pylivestream.json\n```\n\nScreenshare Livestream:\n\n```sh\npython -m pylivestream.screen facebook ./pylivestream.json\n```\n\nMicrophone audio + static image is accomplished by:\n\n```sh\npython -m pylivestream.microphone youtube ./pylivestream.json -image doc/logo.jpg\n```\n\n### Audio-only Livestream\n\nAudio-only streaming is not typically allowed by the Video streaming sites.\nIt may fail to work altogether, or may fail when one file is done and another starts.\n\n```sh\npython -m pylivestream.microphone localhost ./pylivestream.json\n```\n\nSaves screen capture to a file on your disk:\n\n```sh\npython -m pylivestream.screen2disk myvid.avi ./pylivestream.json\n```\n\n## Utilities\n\n* `PyLivestream.get_framerate(vidfn)` gives the frames/sec of a video file.\n* `PyLivestream.get_resolution(vidfn)` gives the resolution (width x height) of video file.\n\n## Notes\n\nLinux requires X11, not Wayland (choose at login).\nIf Wayland is required, consider OBS Studio.\n[x11grab](https://ffmpeg.org/ffmpeg-devices.html#x11grab) is used for screen capture on Linux.\n\n* [FFmpeg streaming](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites)\n* [FFmpeg camera](https://trac.ffmpeg.org/wiki/Capture/Webcam)\n* Camera [FFmpeg overlay](https://trac.ffmpeg.org/wiki/EncodingForStreamingSites#Withwebcamoverlay)\n\n### Windows\n\n* [gdigrab](https://ffmpeg.org/ffmpeg-devices.html#gdigrab)\n\nDirectShow didn't seem to work on Windows, so we used gdigrab instead.\n\n* [DirectShow](https://trac.ffmpeg.org/wiki/DirectShow) device selection\n* DirectShow [examples](https://ffmpeg.org/ffmpeg-devices.html#Examples-4)\n\n### Stream References\n\n* [X Live](https://help.x.com/en/using-x/x-live) doesn't give any specific settings.\n\n* Twitch [ingest servers](https://dev.twitch.tv/docs/video-broadcast/)\n* Twitch [encoding](https://help.twitch.tv/s/article/broadcasting-guidelines)\n\n* [YouTube Live parameters](https://support.google.com/youtube/answer/2853702)\n* [Facebook Live parameters](https://www.facebook.com/business/help/162540111070395)\n* [Owncast parameters](https://owncast.online/docs/codecs/)\n* [IBM Cloud Video parameters](https://support.video.ibm.com/hc/en-us/articles/207852117-Internet-connection-and-recommended-encoding-settings)\n* [Vimeo](https://help.vimeo.com/hc/en-us/articles/12426924775953-Encoder-guide)\n\n### Logo Credits\n\n* Owl PC: Creative Commons no attrib. commercial\n* YouTube: YouTube Brand Resources\n* Facebook: Wikimedia Commons\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Fpylivestream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscivision%2Fpylivestream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Fpylivestream/lists"}