尝试通过 PySimpleGUI 运行带有参数的流链接,但权限被拒绝

问题描述

正如标题所述,我正在尝试使用 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()

(我找到了 here

这是这样工作的:

[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 可能指定了一个我无权写入的目录。

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

解决方法

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

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,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 可能没有响应。建议使用多线程。