设置livestreamer以自动记录抽搐流

时间:2016-10-12 04:02:26

标签: python twitch

我一直在寻找一种录制流的方式,而不是在家里,因为过去的广播对我来说并不令人满意。我找到了一个网站,以帮助我开始,但当我去评论寻求帮助和包括我的脚本的图像后检查回复我看到我的帖子被删除而不是回答。但我离题了。

我知道如何在流生效时从命令提示符手动启动livestreamer,但正如上所述,我正在寻找一个解决方案,以便我不在家。

我正在分享我所拥有的东西,希望有人能够找出错误并帮助我纠正所有问题以使其发挥作用。

#This script checks if a user on twitch is currently streaming and then records the stream via livestreamer

from urllib.request import urlopen
from urllib.error import URLError
from threading import Timer
import time
import datetime
import json
import sys
import subprocess
import datetime
import os
import configparser

def check_user(user):
    """ returns 0: online, 1: offline, 2: not found, 3: error """
    global info
    url = 'https://api.twitch.tv/kraken/streams/' + user +"/?client_id=###"
    try:
        info = json.loads(urlopen(url, timeout = 15).read().decode('utf-8'))
        if info['stream'] == None:
            status = 1
        else:
            status = 0
    except URLError as e:
        if e.reason == 'Not Found' or e.reason == 'Unprocessable Entity':
            status = 2
        else:
            status = 3
    return status

def format_filename(fname):
# Removes invalid characters from filename
    fname = fname.replace("/","")
    fname = fname.replace("?","")
    fname = fname.replace(":","-")
    fname = fname.replace("\\","")
    fname = fname.replace("<","")
    fname = fname.replace(">","")
    fname = fname.replace("*","")
    fname = fname.replace("\"","")
    fname = fname.replace("|","")
    fname = fname.replace(" ","")
    return fname


def loopcheck():
    while True:
        status = check_user(user)
        if status == 2:
            print("username not found. invalid username?")
        elif status == 3:
            print(datetime.datetime.now().strftime("%Hh%Mm%Ss")," ","unexpected error. will try again in 5 minutes.")
            time.sleep(300)
        elif status == 1:
            print(user,"currently offline, checking again in",refresh,"seconds")
            time.sleep(refresh) # 15 seconds
        elif status == 0:
            print(user,"online. stop.")
            filename = user+" - "+datetime.datetime.now().strftime("%Y-%m-%d %Hh%Mm%Ss")+" - "+(info['stream']).get("channel").get("status")+".mp4"
            filename = format_filename(filename)
            str1="livestreamer --twitch-oauth-token ### twitch.tv/"+user+" "+quality+" -o "+directory+filename
            subprocess.call(str1)
            print("Stream is done. Going back to checking..")
            time.sleep(15)

def main():
    global refresh
    global user
    global quality
    global directory

    print("Usage: check.py [refresh] [user] [quality]")
    print("Usage: TwitchChecker.py [refresh] [user] [quality]")

    refresh = 15.0

    str1=""
    refresh = 15.0
    user = "###"
    quality = "best"
    directory = "C:\Users\###\Videos\###\\"


    if(refresh<15):
        print("Check interval should not be lower than 15 seconds")
        refresh=15

    print("Checking for",user,"every",refresh,"seconds. Record with",quality,"quality.")
    loopcheck()


if __name__ == "__main__":
    # execute only if run as a script
    main()

set /p DUMMY=Hit ENTER to continue...

对于批处理文件我有:

@echo
C:\Python\python C:\Users\xxx\Videos\xxx\xxx.py 
@echo off

当我点击两个文件中的任何一个时发生的一切都是命令提示窗口快速闪烁。

感谢所有人的帮助!

0 个答案:

没有答案