Skip to main content

Python scripted livestreaming using FFmpeg

DOI Actions Status pypi versions PyPi Download stats

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. 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 IBM Live Video, Vimeo, Restream.io and more for streaming broadcasts.

PyLivestream diagram showing screen capture or camera simultaneously livestreaming to multiple services.

Troubleshooting

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 lower level use of FFmpeg with Python asyncio
  • asyncio-subprocess-ffpmeg simple asyncio subprocess example that could also be used as a template for general asyncio subprocess Python use.
  • ffmpy FFmpeg subprocess without asyncio

Install

Requires FFmpeg ≥ 4.2 for RTMPS as required or recommended by Facebook Live, YouTube Live, etc.

Latest release:

python3 -m pip install PyLivestream

Development version:

git clone https://github.com/scivision/PyLivestream

cd PyLivestream

python3 -m pip install -e .

FFmpeg can be obtained like:

  • Windows: winget install ffmpeg
  • Linux: sudo apt install ffmpeg
  • MacOS: brew install ffmpeg

If errors result from FFmpeg not in PATH environment variable, optionally set environment variable "FFMPEG_ROOT" to the directory containing FFmpeg executable.

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

ffmpeg -list_devices true -f dshow -i dummy

MacOS

ffmpeg -f avfoundation -list_devices true -i ""

Linux

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.fglob
    • 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 YouTube Live.
  2. Edit "pylivestream.json" to have the YouTube streamid
  3. Run Python script and chosen input will stream on YouTube Live.
python -m pylivestream.screen youtube ./pylivestream.json

Facebook Live

Facebook Live requires FFmpeg ≥ 4.2 due to mandatory RTMPS.

  1. configure a Facebook Live stream
  2. Put stream ID into the JSON file
  3. Run Python script for Facebook with chosen input
python -m pylivestream.screen facebook ./pylivestream.json

Twitch

Create stream from Twitch Dashboard. Edit pylivestream.json file with "url" and "streamid" for Twitch. Run Python script for Twitch with chosen input:

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 and edit with values you determine.

File-Streaming

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. This uses FFmpeg -f tee. For example, Facebook Live and YouTube Live simultaneously:

python -m pylivestream.camera youtube facebook ./pylivestream.json

Screen Share Livestream

Stream to multiple sites, in this example Facebook Live and YouTube Live simultaneously:

python -m pylivestream.screen youtube facebook ./pylivestream.json

Image + Audio Livestream

Microphone audio + static image is accomplished by

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:

python -m pylivestream.microphone localhost ./pylivestream.json

Screen capture to disk

This script saves your screen capture to a file on your disk:

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). If Wayland is required, consider OBS Studio. x11grab is used for screen capture on Linux.

Windows

DirectShow didn't seem to work on Windows, so we used gdigrab instead.

Stream References

Logo Credits

  • Owl PC: Creative Commons no attrib. commercial
  • YouTube: YouTube Brand Resources
  • Facebook: Wikimedia Commons

Release files for pylivestream 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pylivestream 2.1.1
File Size Uploaded
pylivestream-2.1.1.tar.gz 786.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pylivestream 2.1.1
File Interpreter ABI Platform
pylivestream-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 1.6 MB

Release files / pylivestream-2.1.1.tar.gz

Download URL pylivestream-2.1.1.tar.gz
Size 786.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7f345dd2719f31c8885fdf4d720a25b84f348b65c710df70108964c07556dbeb
BLAKE2b-256 checksum
How to use checksums
f6529759fc734fe913de3d41c5c00c21badf29f05dfdabe37b9be0d9cc03fca2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 8, 2025.

Transparency log

Release files / pylivestream-2.1.1-py3-none-any.whl

Download URL pylivestream-2.1.1-py3-none-any.whl
Size 787.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de4581dc3bdcfdbe646035bf3af245c560225dfc94f2f56316ec4be283775695
BLAKE2b-256 checksum
How to use checksums
4b2a283b99496fc2c1d18c3fe73e05eb30d756f81d636c56fdb8675b835d5d15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 8, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.11.4

1 release file

1.11.3

1 release file

1.11.2

1 release file

1.11.1

1 release file

1.11.0

1 release file

1.10.0

1 release file

1.9.7

1 release file

1.9.6

1 release file

1.9.5

1 release file

1.9.4

1 release file

1.9.3.0

1 release file

1.9.2.0

1 release file

1.9.1.1

1 release file

1.9.1

1 release file

1.9.0

1 release file

1.8.0

1 release file

1.7.3

1 release file

1.7.2

1 release file

1.7.1

2 release files

1.7.0

2 release files

1.6.6

2 release files

1.6.5

2 release files

1.6.4

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.6

2 release files

1.4.5

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page