使用 SetParent win32gui 函数将 ffplay window 嵌入到 tkinter 框架中

Embedding ffplay window into tkinter frame by using SetParent win32gui function

我正在使用将 ffplay window 嵌入到我的 tkinter 框架中。

这是我的代码:

file = "path/to/the/file.mp4"
command = subprocess.Popen(["ffplay","-i","%s"%file],stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,)
pid = gw.getWindowsWithTitle('%s'%file) #getting the hwnd of the ffplay window
hwnd = re.findall(r'\d+',str(pid))
x = int(videoWidget.winfo_x()) #video widget is the target tkinter frame
y =int(videoWidget.winfo_y())
width =  int(videoWidget.winfo_width())
height =  int(videoWidget.winfo_height())
win32gui.MoveWindow(int(hwnd[-1]), int(x), int(y),int(width) , int(height), 1) # trying to move the ffplay window to the videowidget geometry 
win32gui.SetParent(int(hwnd[-1]),int(stream_id)) #trying to set the parent of the ffplay window to the videowidget, that should show the ffplay window in the tkinter frame

代码 运行 没有任何错误,但是 ffplay window 和 tkinter window 都停止响应没有任何错误

如果有人能帮助我,我将不胜感激

停止响应的根本原因: stdout=PIPE and/or stderr=PIPE。 它会阻塞 ffmpeg 的输出并导致死锁。

您可以为 ffmpeg 的输出创建一个新的控制台。

我的测试样本:

import subprocess
from subprocess import Popen, CREATE_NEW_CONSOLE
import pygetwindow as gw
import win32gui
import regex as re
import time
file = "path/to/the/file.mp4"
proc = subprocess.Popen(["ffplay","-i","%s"%file], creationflags = CREATE_NEW_CONSOLE)

time.sleep(2) #make sure the window has been started and available to find.
pid = gw.getWindowsWithTitle('%s'%file) #getting the hwnd of the ffplay window
hwnd = re.findall(r'\d+',str(pid))
print(hwnd)
x = 0 #video widget is the target tkinter frame
y = 0
width =  800
height =  500
win32gui.MoveWindow(int(hwnd[-1]), int(x), int(y),int(width) , int(height), 1) # trying to move the ffplay window to the videowidget geometry 

此外,在调用getWindowsWithTitle之前,您应该确保已经启动了window。

由于我无法发表评论,我想指出您可以将标准输出通过管道传输到 subprocess.DEVNULL、stdout=subprocess.DEVNULL 以防止死锁。