Skip to content

Commit

Permalink
Allow passing extra arguments to downloader
Browse files Browse the repository at this point in the history
* Use argparse.
* Pass extra arguments to streamlink which can be useful to authenticate
  and avoid mid-roll ads.
  • Loading branch information
glubsy committed Apr 8, 2023
1 parent c6cf4fe commit 82fec46
Showing 1 changed file with 60 additions and 26 deletions.
86 changes: 60 additions & 26 deletions save_livestream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import time
import subprocess
import argparse
from time import gmtime, strftime
from random import uniform

Expand Down Expand Up @@ -54,15 +55,18 @@ def get_term_seq(char_type):
TERM_SEQ[key] = get_term_seq(key)


def download(link, quality="best"):
def download(args, extra=None, quality="best"):
current_date_time = strftime("%Y-%m-%d %H-%M-%S", gmtime())
base_filename = r"{time:%Y%m%d-%H-%M-%S} [" + sys.argv[1] + r"] {title} [" + f"{quality}" + r"]_{id}.ts"
cmd = ["streamlink", "--twitch-disable-hosting", "--twitch-disable-ads",
"--hls-live-restart", "--stream-segment-timeout", "30",
"--stream-segment-attempts", "10",
"-o", base_filename,
link, quality
]
filename = r"{time:%Y%m%d %H-%M-%S} [" + args.author_name + r"] {title} [" + f"{quality}" + r"][{id}].ts"
cmd = [
"streamlink", "--twitch-disable-hosting", "--twitch-disable-ads",
"--hls-live-restart", "--stream-segment-timeout", "30",
"--stream-segment-attempts", "10"
]
if extra:
cmd.extend(extra)

cmd.extend(["-o", filename, args.URI, quality])

full_output = ""
try:
Expand Down Expand Up @@ -111,23 +115,53 @@ def download(link, quality="best"):
print(" " * term_width, end='\r')


def parse_args():
parser = argparse.ArgumentParser(
description=(
"Monitor a Twitch channel for any active live stream and record them"
),
epilog=(
"Extra arguments can be passed to streamlink. "
"This is useful to pass login token as to avoid mid-roll ads "
"which may corrupt the final output due to stream discontinuities. "
"Example: \"--twitch-api-header 'Authorization=OAuth <auth-token>'\""
)
)
parser.add_argument(
"--author-name", type=str,
help="The name of the channel's author for the output filename",
required=True
)
parser.add_argument(
"URI", metavar="URI",
help="The URI to the channel to monitor OR video to download",
)
return parser.parse_known_args()


def main():
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <base filename> <link to Twitch>\n")
print(f"Example for Twitch: {sys.argv[0]} sovietwomble https://www.twitch.tv/sovietwomble/")
print(f"Example for Twitch: {sys.argv[0]} sovietwomble https://www.twitch.tv/videos/571088399")
exit(1)

if "twitch.tv" in sys.argv[2]:
if '/videos/' in sys.argv[2]:
download(sys.argv[2])
else:
while True:
download(sys.argv[2])
try:
time.sleep(uniform(MIN_WAIT, MAX_WAIT))
except KeyboardInterrupt:
print()
exit()
args, extra = parse_args()

# Usage: <author_name> <Twitch_URI
# Example for Twitch: script sovietwomble https://www.twitch.tv/sovietwomble/"
# Example for Twitch: script sovietwomble https://www.twitch.tv/videos/571088399"

if "twitch.tv" not in args.URI:
print("Not a twitch.tv URI. Aborting.")
return 1

if '/videos/' in args.URI:
download(args, extra)
return 0

while True:
download(args, extra)
try:
time.sleep(uniform(MIN_WAIT, MAX_WAIT))
except KeyboardInterrupt:
print("Interrupt asked by user. Exiting.")
return 0


if __name__ == '__main__':
main()
exit(main())

0 comments on commit 82fec46

Please sign in to comment.