Git Product home page Git Product logo

twitch-utils's Introduction

Python utils for Twitch PyPI version

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

# preview result in mpv with seeking
twitch_utils concat 1.ts 2.ts -o - -f edl | mpv -
# or...
mpv $(twitch_utils concat 1.ts 2.ts -o - -f edl_uri)

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.

Main features:

  • Start recording at any time - stream will be recorded from the beginning;
  • Monitor streamlink process for errors and skipped segments - and fix them;
  • Resume recording after restart of the script - it is possible as long as the stream is still online and already downloaded parts are accessible;
  • Skip ads even without paid subscription - ad segments will be ignored by streamlink and missing original segments will be downloaded from VOD;

Algorithm:

  1. Check if channel is live and VOD for current stream already exists;
  2. Get live VOD ID from Twitch API;
  3. Start downloading live stream into file VOD.0.ts;
  4. Wait 1 minute and start downloading VOD into file VOD.1.ts;
  5. Wait for VOD download to finish;
  6. Check the possibility of concatenation and download missing parts of the timeline;
  7. Wait for stream to finish;
  8. Restart stream recording in case of errors or skipped segments, wait 1 minute and go to step 6;
  9. Concatenate all parts via concat script (see above).

Example

# Record live stream of channel 'blackufa' using 2 threads
twitch_utils record -j 2 blackufa

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

Thanks

  • TwitchRecover for finding a way to download unlisted/broken VODs

twitch-utils's People

Contributors

thedrhax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

twitch-utils's Issues

TypeError: argument of type 'NoneType' is not iterable

C:\Users\Administrator\Desktop\New folder (6)>twitch_utils record dodgeballcanada -j 2 -b 2M
Checking if channel dodgeballcanada is active...
Stream is online! Found the ID of live VOD: 534927868
Starting to record the live stream...
Downloading https://twitch.tv/dodgeballcanada into 534927868.end.ts
Process Process-1:
Traceback (most recent call last):
File "e:\users\administrator\appdata\local\programs\python\python36\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "e:\users\administrator\appdata\local\programs\python\python36\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "e:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\twitch_utils\record.py", line 57, in download
sl_proc = Popen(sl_cmd, **sl_kwargs)
File "e:\users\administrator\appdata\local\programs\python\python36\lib\subprocess.py", line 729, in init
restore_signals, start_new_session)
File "e:\users\administrator\appdata\local\programs\python\python36\lib\subprocess.py", line 991, in _execute_child
args = list2cmdline(args)
File "e:\users\administrator\appdata\local\programs\python\python36\lib\subprocess.py", line 481, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'NoneType' is not iterable

[record] Check start.ts before finishing end.ts

It is possible to run concat on start.ts and end.ts while end.ts is still being downloaded. Concat should successfully build a complete timeline of the stream. An error would mean that start.ts is too short and should be downloaded again.

[feature request] command to make a clip

Sometime channels disable clips. I thought it would be interesting while you record a stream, to have a command to make a clip.

Also, a bug if stream is live and I start recording, but that there's no VOD, program won't start recording.

โžœ  ~ twitch_utils record --oauth=add_oauth pommed0r12345 -j 2
Traceback (most recent call last):
  File "/usr/local/bin/twitch_utils", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/dist-packages/twitch_utils/__init__.py", line 29, in main
    record_main(argv)
  File "/usr/local/lib/python3.7/dist-packages/twitch_utils/record.py", line 292, in main
    v = api.find_vod(channel)
  File "/usr/local/lib/python3.7/dist-packages/twitch_utils/twitch.py", line 46, in find_vod
    return stream['archiveVideo']['id']
TypeError: 'NoneType' object is not subscriptable

[pip] Split package into multiple extras

Some dependencies such as praat-parselmouth or spleeter are heavy, and are only required by one script from the whole package. Users must be able to install this package partially by using extras_require from setuptools.

Package name Dependencies
tdh-twitch-utils requests, python-dateutil, docopt
tdh-twitch-utils[record] streamlink
tdh-twitch-utils[offset] praat-parselmouth
tdh-twitch-utils[mute] tensorflow, spleeter
tdh-twitch-utils[all] all of the above

[record] Custom Client-ID and integrated OAuth

Instead of using Twitch's OAuth token, we can create our own. It would be enough for detecting stream and live VOD, but streamlink would still require first-party token, that can be configured separately.

  • Add custom Client-ID support to allow users to specify their own Client-ID - OAuth token pairs;
  • Create Client-ID for this script;
  • Implement OAuth implicit code flow;
  • Add global configuration file and store token in it;

[record] Custom output file format

User should be able to choose output container for concatenated stream. It is already implemented in concat, so we just need to pass output file name to it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.