【问题标题】:Why is this Popen with threading not working?为什么这个带有线程的 Popen 不起作用?
【发布时间】:2019-04-09 17:38:25
【问题描述】:

我写了一个小 tkinter GUI 来处理 ffmpeg 的 4 个输入。由于子流程需要一些时间,我想对流程进行状态。因此我使用线程,因此 tkinter 在执行子进程时不会冻结。

我的问题是,通过线程化,ffmpeg 命令以 0kb 输出目标文件,并且不再向该文件写入任何内容。如果我在没有线程的情况下使用我的函数,一切正常,但 GUI 冻结。

这是代码的主要部分:

def ffmpeg(v0,v1,v2,v3):
    cmd = [ path+'ffmpeg.exe',"-y","-i",v0,"-i",v1,"-i",v2,'-i',v3,'-filter_complex',"[0:v][1:v]hstack[top];[2:v][3:v]hstack[bottom];[top][bottom]vstack,format=yuv420p[v]",'-map',"[v]","out.mp4"]
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    while True:
        output = process.stdout.readline()
        inpu = process.stderr.readline()
        if output == b'' and process.poll() is not None:
            break
        if output:
            print(output.strip()) # HERE i will insert into tkinter textfield
    rc = process.poll()

def buttonClick(v0,v1,v2,v3):

    #ffmpeg(v0,v1,v2,v3) # This line works
    t = threading.Thread(target=ffmpeg,args=(v0,v1,v2,v3,)) #This doesn't work
    t.start()
    #t.join()

#tkvar list elements are absolute paths to the videofiles
submitButton = Button(mainframe, text="Process Video", command=lambda: buttonClick(tkvar[0].get(),tkvar[1].get(),tkvar[2].get(),tkvar[3].get()))
submitButton.grid(row = 7, column =3)

为什么我的线程不起作用?

【问题讨论】:

  • 将这行inpu = process.stderr.readline()移到while之外。它会阻塞直到进程完成。

标签: python multithreading tkinter ffmpeg subprocess


【解决方案1】:

问题是 process.stdout.readline() 始终为空,因为 ffmpeg 始终将所有内容写入标准错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2013-01-26
    • 2016-04-20
    • 2017-06-19
    • 2021-07-19
    • 2020-06-14
    相关资源
    最近更新 更多