Record, concatenate and synchronize Twitch live streams
Project description
Python utils for Twitch
pip3 install tdh-twitch-utils[all]
This module supports partial installation:
Package | Scripts | Dependencies |
---|---|---|
tdh-twitch-utils | concat | FFmpeg |
tdh-twitch-utils[record] | concat, record | FFmpeg, streamlink |
tdh-twitch-utils[offset] | concat, offset | FFmpeg, praat-parselmouth |
tdh-twitch-utils[mute] | concat, mute | FFmpeg, tensorflow, spleeter |
tdh-twitch-utils[all] | all of the above | all of the above |
You can also provide a list of extras in a single command:
pip3 install tdh-twitch-utils[record,offset]
concat
This script uses MPEG-TS timestamps to concatenate multiple video segments into a single file without re-encoding. It is most useful for assembling partial stream recordings in case of interruption or error during stream download. Overlapping parts will be removed precisely with ffmpeg's concat demuxer.
Example
# download two overlapping segments (60 seconds each)
VOD="YOUR VOD ID"
streamlink -o 1.ts --hls-duration 60 "twitch.tv/videos/$VOD" best
streamlink -o 2.ts --hls-start-offset 30 --hls-duration 60 "twitch.tv/videos/$VOD" best
# concatenate two segments into one video
twitch_utils concat 1.ts 2.ts -o result.mp4
# create one segment
twitch_utils concat 1.ts 2.ts -o result.ts
# pipe concatenated MPEG-TS stream to other applications
twitch_utils concat 1.ts 2.ts -o - | ffmpeg -i - -c copy result.mp4
record
This script can be used to record live streams without waiting for them to end. It starts to record live stream immediately, then downloads VOD and concatenates them into full stream recording.
Obviously, this script requires channel to have public VODs.
Algorithm:
- Check if channel is live and VOD for current stream already exists;
- Get live VOD ID from Twitch API;
- Start downloading live stream into file
VOD.end.ts
; - Wait 1 minute and start downloading VOD into file
VOD.start.ts
; - Wait for VOD download to finish;
- Check the possibility of concatenation and redownload VOD if timeline is not complete;
- Wait for stream to finish;
- Concatenate two parts via
concat
script (see above).
Note: Since Nov 2019 you have to provide your Twitch OAuth token in the command. Otherwise the script will not be able to detect the ID of the live VOD and download the beginning of the stream. At the moment, you will need to extract OAuth token from Twitch's cookie "auth-token". Other options such as providing your own Client-ID and token are not implemented yet.
This script is just a proof of concept and probably should not be relied upon.
Example
# Record live stream of channel 'blackufa' using 2 threads
twitch_utils record --oauth=YOUR_TOKEN blackufa -j 2
offset
This script performs cross-correlation of two audio files to find
offset between them. First argument is cropped and used as template.
Second argument can have any duration -- it will be divided into
separate chunks to reduce memory usage (otherwise it wouldn't be
possible to use exceptionally big files). Both arguments can be
videos or audio files -- audio track will be extracted and converted.
You can even use HTTP links if ffprobe
is able to correctly determine
second argument's duration.
Example
# Cut small segment from big video file (offset: 123 seconds)
ffmpeg -ss 123 -i YOUR_FILE.mp4 -t 60 -c copy template.mp4
# Find offset of template.mp4 within YOUR_FILE.mp4
twitch_utils offset template.mp4 YOUR_FILE.mp4
# ... returns 122.99997732426303
# Same command, but result will be rounded to nearest integer
twitch_utils offset template.mp4 YOUR_FILE.mp4 --round
# ... returns 123
mute
This script attempts to separate streamer's voice from background music by using Spleeter. Only specified time ranges are affected. Output contains the same video, but without music in these parts.
The main purpose of this script is to remove automated Content-ID claims from the video on YouTube without muting the whole section.
The result is similar to "Mute song only (beta)" in YouTube Studio, but this script is much faster and can handle multiple time ranges at once.
Note: Prebuilt binaries of Tensorflow require CPUs with AVX support.
Example
# Remove music from 5:00 to 8:00 and from 1:00:00 to 1:05:00
twitch_utils mute input.mp4 5:00~8:00 1:00:00~1:05:00 -o output.mp4
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for tdh_twitch_utils-1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02abf5a0e71aad04da1f99846d6b3637ad1e4e268963ce77bd7895d2028cb66d |
|
MD5 | cee0df25e1413d1e9585743a2d41448d |
|
BLAKE2b-256 | 3ea7c3079c57270ef67ee541fb871652e0b690b0a83633793c91ba68fb717cf3 |