Skip to content

Commit

Permalink
fix: native streamlink/ffmpeg stream downloading
Browse files Browse the repository at this point in the history
Fixes #212
  • Loading branch information
biodrone committed Jan 28, 2022
1 parent 0593dda commit ed6a036
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
31 changes: 30 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PyYAML = "6.0"
requests = "2.27.1"
streamlink = "^3.1.1"
yt-dlp = "^2022.1.21"
ffmpeg-python = "^0.2.0"

[tool.poetry.dev-dependencies]
pytest = "*"
Expand Down
31 changes: 11 additions & 20 deletions streamdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pathlib import Path
from streamlink import Streamlink, StreamError, PluginError, NoPluginError
import subprocess
import ffmpeg

# set up manager functions
mgr = Manager()
Expand Down Expand Up @@ -308,6 +309,9 @@ def twitch_download(url, user, outdir):
"""

session = Streamlink()
session.set_plugin_option("twitch", "twitch-disable-ads", True)
session.set_plugin_option("twitch", "twitch-disable-reruns", True)
session.set_plugin_option("twitch", "twitch-disable-hosting", True)

try:
# use this to check for live streams
Expand All @@ -317,6 +321,7 @@ def twitch_download(url, user, outdir):
logger.warning(f"No streams found for user {user}")
return False
else:
# TODO: Make this log message better
logger.debug(
"{}/{}/{}/{} - {}.mp4".format(
outdir.rsplit("/", 1)[0],
Expand All @@ -327,6 +332,7 @@ def twitch_download(url, user, outdir):
)
)
# create dir because streamlink is incapable of doing so apparently
# TODO: Do this natively
subprocess.call(
[
"mkdir",
Expand All @@ -337,27 +343,12 @@ def twitch_download(url, user, outdir):
]
)
# download video with streamlink
subprocess.call(
[
"streamlink",
"-Q",
"-f",
"-4",
"-o",
"{} - {}.mp4".format(user, datetime.utcnow().date()),
"--twitch-disable-ads",
"--twitch-disable-reruns",
"--twitch-disable-hosting",
"{}/{}".format(url, user),
"best",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd="{}/{}/{}".format(
outdir.rsplit("/", 1)[0], url.upper().split(".")[0], user
),
(
ffmpeg
.input(stream["best"].url)
.output("test.mp4")
.run()
)
# streamlink -o test.mp4 --twitch-disable-ads --twitch-disable-reruns --twitch-disable-hosting https://www.twitch.tv/classykatie best
return True
except NoPluginError:
logger.warning("Streamlink is unable to handle the URL '{0}'".format(url))
Expand Down

0 comments on commit ed6a036

Please sign in to comment.