scivision / PyLivestream

Pure Python FFmpeg-based live video / audio streaming to YouTube, Facebook, Periscope, Twitch, and more
Apache License 2.0
707 stars 156 forks source link
facebook-live ffmpeg livestream periscope twitch vimeo youtube youtube-live

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:

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

Troubleshooting

PyLivestream benefits

PyLivestream limitations

Design rationale

Why not do things without the command line, via linking libffmpeg, libgstreamer or libav?

Alternatives

Other projects using FFmpeg from Python include:

Install

Requires FFmpeg ≥ 3.0 (≥ 4.2 for Facebook Live RTMPS)

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:

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.

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:

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.

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

Twitter

TODO

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:

Stream to multiple sites, in this 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

Notes

FFmpeg References

Windows

DirectShow didn't work for me on Windows 10, so I used gdigrab instead.

Stream References

Logo Credits