Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to embedding twitch-dl in a python application? #107

Open
mateusfmcota opened this issue Aug 24, 2022 · 5 comments
Open

Is there a way to embedding twitch-dl in a python application? #107

mateusfmcota opened this issue Aug 24, 2022 · 5 comments

Comments

@mateusfmcota
Copy link

I'm trying to build an application which gets info from a channel like twitch-dl videos <channel_name> -j --all and then to filter this metadata list and download some of the videos twitch-dl download <id> -q source -o <some_format> of the list.

I tried to play a bit with the source code to see if i can get something working but I couldn't, so I was wondering if there's some way for me to do it?

@mateusfmcota mateusfmcota changed the title Is there a way to use twitch-dl in a python application? Is there a way to embedding twitch-dl in a python application? Aug 26, 2022
@ZedAU
Copy link

ZedAU commented Aug 30, 2022

My friend and my solution was to copy some of the private? functions in download.py into our own file and call them when needed

Being able to import it like ytdl and pass options for like quality/target filename/format would be handy though EMBEDDING YOUTUBE-DL

@maskym
Copy link

maskym commented Sep 3, 2022

It might be ugly but I use os.system functions to call twitch-dl through shell

@FunnyPocketBook
Copy link
Contributor

FunnyPocketBook commented Sep 8, 2022

This is available as a Python package, so if you do pip install twitch-dl, you can then in your code for example import twitchdl.commands.download as download to use the VOD download function

import twitchdl.commands.download as download

class DotDict(dict):
    """dot.notation access to dictionary attributes"""

    __getattr__ = dict.get
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

download_args = DotDict(
            {
                "videos": ["1584666791"],
                "format": "mkv",
                "keep": False,
                "quality": "720p60",
                "max_workers": 20,
                "no_join": False,
                "overwrite": True,
                "start": None,
                "end": None,
                "output": "{date}_{channel_login}_{id}.{format}",
            }
        )
download(download_args)

I'm using DotDict here because the download arguments need to be accessible in dot notation, you can also create an arguments class and create a new instance the class for your arguments and pass that through. You need to provide all arguments and set the ones that you don't need to their default value, as specified in the docs/help page, or set them to None

@ihabunek
Copy link
Owner

ihabunek commented Sep 9, 2022

As you may have guessed, twitch-dl was not built to be used as a library. This is something I'd like to change, but requires a non-trivial effort so I can't tell you when it's going to happen. The suggestions by @FunnyPocketBook and @maskym will work in a pinch.

@Barnacules
Copy link

There is some wicked magic going on with Twitch-DL to be able to download the video streams from Twitch since that isn't functionality, they expose in the Helix API anymore. I do everything else with the Helix API though using twitchAPI lib for Python (pip install twitchAPI) because it's pretty well documented despite all the async stuff being a little annoying.

When I want to download a file, I just create a subprocess to call twitch-dl and make sure it's in the system path and it works just fine. So I would say leave this as an EXE for now instead of making it work directly from Python because if you do that Twitch might re-roll the dice and stop people from downloading again.


import subprocess

def Download(clip_url):
quality = "1080p"
subprocess.run(f"twitch-dl.exe download -q {quality} {clip_url}")

Just make sure twitch-dl is in the system path and it will work perfectly for downloading files which is the one thing this program does that the Twitch API doesn't technically allow 😉

Also, if anyone does know how to get the actual .mp4 file download location from the returned block of HTML that comes down with get_clip please let me know because that is literally the only hurdle I've encountered and for now I'm just using Twitch-DL but I would like it to be all in process for flexibility and performance in the future since I already got all the clip data from the API so it's duplicating effort right now.

I want to be able to have my audience create a clip and it automatically downloads and replays on command when they want it too 😉 LIke an instant reply 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants