Package: pylivestream
Python scripted livestreaming using FFmpegStreams to one ormultiplestreaming sites simultaneously, using pure object-oriented Python (no extra packages) and FFmpeg.
Tested withflake8,mypytype checking andpytest.
Python >= 3.9 is recommended for full capabilities.visual_tests.pyis a quick check of several command line scripting scenarios on your laptop.
FFmpeg is used from Pythonsubprocessto stream to sites including:Facebook Live  (requires FFmpeg >= 4.2 due to mandatory RTMPS)YouTube LiveTwitchalso Ustream, Vimeo, Restream.io and more for streaming broadcasts.TroubleshootingPyLivestream benefitsPython 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 limitationsdoesnotauto-restart if network connection glitchesis intended as a bare minimum command generator to run the FFmpeg programis not intended for bidirectional robust streaming--consider a program/system based on Jitsi for that.Design rationaleWhy 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 librariesonce you get a setup working once, you don't even need Python anymore--just copy and paste the command lineAlternativesOther projects using FFmpeg from Python include:python-ffmpeglower level use of FFmpeg with Python asyncioasyncio-subprocess-ffpmegsimple asyncio subprocess example that could also be used as a template for general asyncio subprocess Python use.ffmpyFFmpeg subprocess without asyncioInstallRequires FFmpeg ≥ 3.0 (≥ 4.2 for Facebook Live RTMPS)Latest release:python3-mpipinstallPyLivestreamDevelopment version:gitclonehttps://github.com/scivision/PyLivestreamcdPyLivestream

python3-mpipinstall-e.Configuration: pylivestream.jsonYou 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 examplepylivestream.jsonand 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 capturevideo_kbps: override automatic video bitrate in kbpsaudio_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:veryfastorultrafastif 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 aresys.platformspecific parameters.Seek help in FFmpeg documentation, try capturing to a file first and then update ~/pylivestream.json forsys.platform.Deduce inputsEach computer will need distinct pylivestream.json device input parameters:audio_chan: audio devicecamera_chan: camera devicescreen_chan: desktop capture software port nameLoopback 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.Windowsffmpeg-list_devicestrue-fdshow-idummyMacOSffmpeg-favfoundation-list_devicestrue-i""Linuxv4l2-ctl--list-devicesAPIThere are two ways to start a stream (assuming you've configured as per following sections).
Both do the same thing.command linepython -m pylivestream.globpython -m pylivestream.screenpython -m pylivestream.loopfilepython -m pylivestream.screen2diskpython -m pylivestream.camerapython -m pylivestream.microphoneimport pylivestream.api as plsfrom within your Python script. For more information typehelp(pls)orhelp(pls.stream_microphone)pls.stream_file()pls.stream_microphone()pls.stream_camera()AuthenticationThe 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 LiveconfigureYouTube Live.Edit "pylivestream.json" to have the YouTube streamidRun Python script and chosen input will stream on YouTube Live.python-mpylivestream.screenyoutube./pylivestream.jsonFacebook LiveFacebook Live requires FFmpeg >= 4.2 due to mandatory RTMPSconfigure your Facebook Live streamPutstream IDinto the JSON fileRun Python script for Facebook with chosen inputpython-mpylivestream.screenfacebook./pylivestream.jsonTwitterTODOTwitchCreate stream fromTwitch Dashboard.
Edit pylivestream.json file with "url" and "streamid" for Twitch.
Run Python script for Twitch with chosen input:python-mpylivestream.screentwitch./pylivestream.jsonUsageDue 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 providedpylivestream.jsonand edit with values you determine.File-StreamingCameraNote: your system may not have a camera, particularly if it's a virtual machine.JSON:camera_size: camera resolution -- find fromv4l2-ctl --list-formats-extor camera spec sheet.camera_fps: camera fps -- found from command above or camera spec sheetStream to multiple sites, in this example Facebook Live and YouTube Live simultaneously:python-mpylivestream.camerayoutubefacebook./pylivestream.jsonScreen Share LivestreamStream to multiple sites, in this example Facebook Live and YouTube Live simultaneously:python-mpylivestream.screenyoutubefacebook./pylivestream.jsonImage + Audio LivestreamMicrophone audio + static image is accomplished bypython-mpylivestream.microphoneyoutubefacebook./pylivestream.json-imagedoc/logo.jpgor wherever your image file is.Audio-only LivestreamAudio-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:python-mpylivestream.microphonelocalhost./pylivestream.jsonScreen capture to diskThis script saves your screen capture to a file on your disk:python-mpylivestream.screen2diskmyvid.avi./pylivestream.jsonUtilitiesPyLivestream.get_framerate(vidfn)gives the frames/sec of a video file.PyLivestream.get_resolution(vidfn)gives the resolution (width x height) of video file.NotesLinux requires X11, not Wayland (choose at login)x11grabwas deprecated in FFmpeg 3.3, was previously replaced byxcbgrabReferencewebpageFFmpeg ReferencesstreamingcameraCameraoverlayWindowsgdigrabDirectShow didn't work for me on Windows 10, so I used gdigrab instead.DirectShowdevice selectionDirectShowexamplesStream ReferencesTwitch parametersTwitchingest serversTwitchencodingTwitter Live parametersYouTube Live parametersFacebook Live parametersUstream parametersVimeoconfigVimeoparametersLogo CreditsOwl PC: Creative Commons no attrib. commercialYouTube: YouTube Brand ResourcesFacebook: Wikimedia Commons