Automatically monitor a YouTube channel for livestreams and restream them to another YouTube channel or any other streaming service.
Please do not restream copyrighted material.
- Python 3.6+
- FFmpeg (tested with version 4.3+)
Download the repository, setup the virtual environment, and install the dependencies
$ git clone https://github.com/vachau/youtube-restreamer.git
$ cd youtube-restreamer
$ python3 -m venv env
$ source env/bin/activate
$ pip install -r requirements.txt
$ python youtube_restreamer.py
Create a configuration file config.json
specifying the ID of the channel you want to monitor. This can be located on the channel page in the format https://www.youtube.com/channel/[channel_id]
{
"channel_id": "UCE_M8A5yxnLfW0KghEeajjw"
}
The application can also automatically create and stream to live broadcasts on YouTube. To enable this you must get OAuth credentials here.
- Create a new project in Google Cloud if one doesn't already exist
- Under "Library" in the sidebar
- Search for "YouTube Data API v3"
- Enable it for your project
- Under "OAuth consent screen" in the sidebar
- Create a consent screen, the more detailed the better. Google is known to disable projects for looking "suspicious"
- Under "Scopes" add the scopes
../auth/youtube.force-ssl
and../auth/youtube
- Under "Test users" add the Google accounts that own the YouTube channels you plan on streaming from
- Under "Credentials" in the sidebar
- Click "Create credentials" at the top
- Select: "OAuth client ID"
- Select application type: "Desktop app"
- Once finished, click the download symbol next to the credentials you created
- Add the file to your
config.json
{
"youtube_oauth": {
"secrets_file": "client_secret.json",
}
}
When running the application for the first time follow the instructions to login with the YouTube channel you would like to stream to.
$ python youtube_restreamer.py
Please visit this URL to authorize this application: https://...
Enter the authorization code:
To switch accounts run with the option --reset-oauth
It's recommended to give the application a dedicated channel to prevent it possibly interfering with your other uploads and streams.
Currently streams are directly uploaded without reencoding. This may not work properly depending on the required encoder settings for your site
Restreaming is supported to any site that supports RTMP. Simply specify a nickname for the service and the RTMP url and key in your config.json
{
"services": {
"twitch": {
"rtmp_url": "rtmp://twitch.tv/live",
"rtmp_key": "foo"
}
}
}
Then run the application
$ python youtube_restreamer.py twitch
Options should be specified in the JSON file config.json
{
"channel_id": "UCE_M8A5yxnLfW0KghEeajjw",
"youtube_oauth": {
"secrets_file": "client_secret.json",
"token_file": "token.json"
},
"restream_privacy": "unlisted",
"restream_title_format": "Mirror: {title}",
"restream_description_format": "This is a restream of {title}. Original stream: {url}",
"restream_start_delay": 10,
"services": {
"twitch": {
"rtmp_url": "rtmp://twitch.tv/live",
"rtmp_key": "foo"
}
},
"youtube_search_interval": 60,
"ffmpeg_bin": "/path/to/ffmpeg",
"ffprobe_bin": "/path/to/ffprobe",
"ffmpeg_log_dir": "logs/"
}
token_file
: Specify a different JSON file to store OAuth tokens inrestream_privacy
: Visibility of YouTube restreams ("public" (default) | "unlisted" | "private")restream_title_format
: Title of YouTube restreamsrestream_description_format
: Description of YouTube restreamyoutube_search_interval
: How often in seconds to fetch the list of streams from channel_id (don't recommend setting this lower than 1 minute)ffmpeg_bin
: Specify a different location for the ffmpeg binaryffmpeg_bin
: Specify a different location for the ffprobe binaryffmpeg_log_dir
: Enable logging for ffmpeg subprocessesrestream_start_delay
: How long in seconds to let the source stream downloader buffer before uploading a restream
For the _format
options there are several placeholders that can be replaced with source stream information:
{title}
: Source stream title{url}
: Full link to the source YouTube broadast{channel_name}
: Source stream channel name{channel_url}
: Full link to the source YouTube channel
There are also several command-line only options:
-c CONFIG
,--config CONFIG
: Specify a different JSON config file than the defaultconfig.json
--reset-oauth
: Ignore any saved OAuth tokens. Used to switch YouTube accounts--log-level
: Set the log level used by Python's logging module. Default is INFO; WARNING is useful for hiding all normal status messages--quiet
: Don't print any output (overrides log level)--end-broadcasts
: Attempt to force end all YouTube live broadcasts
The application can also be imported as a Python module. Options are provided as a dictionary instead (the keys are the same as above):
from youtube_restreamer import Restreamer
options = {
"channel_id": "UCE_M8A5yxnLfW0KghEeajjw"
"youtube_oauth": {
"secrets_file": "client_secret.json"
}
}
restreamer = Restreamer(options)
restreamer.restream()
Additional functionality:
>>> restreamer = Restreamer(options, reset_oauth=True)
>>> restreamer.restream("twitch")
>>> restreamer.end_broadcasts()
- The YouTube API limits your request quota to 10,000 "units" a day. Based on the cost of creating and deleting broadcasts, you should be able to create a maximum of ~100 YouTube restreams each day.
- There may be a ~5 minute delay between source streams starting and the API detecting them.
- It's possible for a channel to create multiple concurrent livestreams. Currently this will result in only the most recently created one being restreamed.
- Youtube-dl errors
- Check that your channel_id is valid
- Youtube changes their site quite often and youtube-dl is constantly updated to keep up. Try updating it with
pip install -r requirements.txt
- HttpErrors
- Make sure livestreaming is enabled on your YouTube channel
- Misc. YouTube
- Run with
--end-broadcasts
to attempt to end any live broadcasts - Manually end any running live broadcasts on your channel
- Run with
- Misc. RTMP
- Ensure that the endpoint is still available and that streams will start automatically on receiving data