0

正如标题所述,我正在尝试使用 PySimpleGUI 创建一个基本的流链接 GUI。

我有 [gui.Text('Stream link:'), gui.InputText()],一堆其他代码(尚未实现的选项),它们将流链接作为输入,并runCommand('streamlink ' + values[0] + ' best -o output.ts')使用流链接以最佳质量将流下载到 output.ts。

运行命令是:

def runCommand(cmd, timeout=None, window=None):
    os.path.dirname(os.path.realpath(__file__)) 
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = ''
    for line in p.stdout:
        line = line.decode(errors='replace' if (sys.version_info) < (3, 5) else 'backslashreplace').rstrip()
        output += line
        print(line)
        window.Refresh() if window else None
    retval = p.wait(timeout)
    return (retval, output)

if __name__ == '__main__':
    main()

(我在这里找到的)

这是这样工作的:

[cli][info] Found matching plugin youtube for URL
[cli][info] Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p, 720p, 720p60, 1080p60 (best)
[cli][info] Opening stream: 1080p60 (muxed-stream)
error: Failed to open output: output.ts ([Errno 13] Permission denied: 'output.ts')
[cli][info] Closing currently open stream...

如您所见,当我通过 runCommand 运行 streamlink 时,它会收到权限被拒绝错误。我试图将输出文件放在与运行 .py 脚本相同的目录中,但我认为 runCommand 可能指定了一个我无权写入的目录。

我不知道如何为这个命令指定一个目录,有人可以帮忙吗?

4

1 回答 1

0

我在与我的 scripy 文件相同的目录下得到了一个 output.ts。将链接地址输入到输入中,然后单击“开始”。

https://www.youtube.com/watch?v=yHURXBLNs_8
import os
import sys
import subprocess
import PySimpleGUI as sg

def runCommand(cmd, timeout=None, window=None):
    os.path.dirname(os.path.realpath(__file__))
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = ''
    for line in p.stdout:
        line = line.decode(errors='replace' if (sys.version_info) < (3, 5) else 'backslashreplace').rstrip()
        output += line
        print(line)
        window.Refresh() if window else None
    retval = p.wait(timeout)
    return (retval, output)

layout = [[sg.Text('Stream link:'), sg.InputText(key='-INPUT-'), sg.Button('Go')]]
window = sg.Window('Title', layout)

while True:

    event, values = window.read()

    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Go':
        runCommand('streamlink ' + values['-INPUT-'] + ' best -o output.ts')

window.close()

[cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=yHURXBLNs_8
[cli][info] Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p, 720p (best)
[cli][info] Opening stream: 720p (http)
[cli][info] Stream ended
[cli][info] Closing currently open stream...

如果文件区域存在,

[cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=yHURXBLNs_8
[cli][info] Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p, 720p (best)
[cli][info] Opening stream: 720p (http)
[cli][error] File output.ts already exists, use --force to overwrite it.
[cli][info] Closing currently open stream...

在这里,runCommand需要很长时间才能工作,所以 GUI 可能没有响应。建议使用多线程。

于 2020-12-22T09:22:32.780 回答