Python Forum
[Tkinter] mpv window freeze before video end
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] mpv window freeze before video end
#1
The code below works to play a video file (did not include the video file here), but problem is: If I pause the video by right clicking, then 'x' out the window (or right click title bar and select 'close') the window freezes and will not close until I shut down the python program. I have tried dozens including threading etc, but always get stuck at the same freeze point.
#!/usr/bin/python3

import os
import shutil 
import tkinter as tk 
from tkinter import *
from tkinter import  filedialog

import mpv
player = mpv.MPV(input_default_bindings=True,input_vo_keyboard=True,osc=True)

window=tk.Tk()
currdir=os.getcwd()
path=filedialog.askopenfilename(parent=window,initialdir=currdir, title="Select file")
# above code works to select file, I entered a filename below to test if a direct filename causes same problem, and it does
try:
    player.play('/home/rainer/Videos/Garage62-2021-02-06__09-16-51.mkv')
    
finally:
    window.destroy()

window.mainloop()
Reply
#2
You should embed mpv

import tkinter as tk
import mpv

root=tk.Tk()
root.geometry("500x280")
player = mpv.MPV(wid=str(int(root.winfo_id())), input_default_bindings=True,input_vo_keyboard=True,osc=True)
player.play('video.mp4')

tk.mainloop()
rfeyer likes this post
Reply
#3
Axel,
you can not believe how much you helped! I have been dealing with this window freeze for 2+ weeks now, tons of trial and error and tons of posts (including StackOF) without any advancement until your embedding tip!
TY TONS!

Now, i do have an interesting point:
Tried the script on my main computer (Mint 20.1, python3.8+) as:
import tkinter as tk
import mpv
 
root=tk.Tk()
root.geometry("500x280")
player = mpv.MPV(wid=str(int(root.winfo_id())), input_default_bindings=True,input_vo_keyboard=True,osc=True)
player.play('/home/rainer/Videos/Garage62-2021-02-06__09-16-51.mkv')
 
tk.mainloop()


I truly copied the whole code from IDLE.

The result error I got was:
Traceback (most recent call last):
File "/home/rainer/PyProjects/tkinter/MPV/tryembed.py", line 2, in <module>
import mpv
File "/home/rainer/PyProjects/tkinter/MPV/mpv.py", line 20, in <module>
import ctypes.util
File "/usr/lib/python3.8/ctypes/util.py", line 3, in <module>
import subprocess
File "/home/rainer/PyProjects/tkinter/MPV/subprocess.py", line 10, in <module>
subprocess.call(["fmpeg", "-i", sourcedir+"/"+name+".avi", sourcedir+"/"+name+".webm"])
AttributeError: partially initialized module 'subprocess' has no attribute 'call' (most likely due to a circular import)

Amazing?
Couldn't figure it out why (I am not trying to import subprocess or use a call).
Then I remembered one of the many trials this morning: It did involve subprocess and call. Totally different .py file! Not overwritten with your code.
Then I rebooted the PC, ran the code from terminal (no IDLE). Same response.
Then took the script to another PC (also Mint 20.1 Py 3.8+) and your code ran beautifully!
That is just strange!

Again, TY for your help!
Reply
#4
Maybe the python bindings for ffmpeg are interesting for you. That makes a lot of things easier.

https://github.com/kkroening/ffmpeg-python

pip3 install ffmpeg-python
Reply
#5
Not at all, I am amazed at how much you helped with MPV!
I did some testing with ffmpeg last 2 days, but getting spead up and down was not possible for me, so I gave up.
No, this will work as you got me over this huge brick wall!
Reply
#6
Can I ask an add-on question (let me know if I need to start a separate forum thread):

I added frames for main, play, and widgets. changed code to have MPV play in the PlayFrame. Can the process of playing be stopped (before end of file) and window cleared? With a Button in the WidgeFrame?
I will eventually add extended Listbox for selecting multiple clips to play successively.
this is what I now have:
root=tk.Tk()
root.geometry("1300x780")

MainFrame = ttk.Frame(root)
MainFrame.grid(column=0, row=0, sticky="N,S,E,W")

PlayFrame = ttk.Frame(MainFrame,height=700,width=900)
PlayFrame.grid(column=0,row=0,columnspan = 10,rowspan=10, sticky="W,N,S")

WidgetFrame = ttk.Frame(MainFrame)
WidgetFrame.grid(column=11,row=11)

#player = mpv.MPV(wid=str(int(root.winfo_id())), input_default_bindings=True,input_vo_keyboard=True,osc=True)
player = mpv.MPV(wid=str(int(PlayFrame.winfo_id())), input_default_bindings=True,input_vo_keyboard=True,osc=True)

player.play('/home/rainer/Videos/Garage62-2021-02-06__09-16-51.mkv')

tk.mainloop()
Reply
#7
A little test seems to work

import tkinter as tk
from tkinter import ttk
import mpv

def stop_player():
   player.stop() 
   
root=tk.Tk()
root.geometry("1300x780")
 
MainFrame = ttk.Frame(root)
MainFrame.grid(column=0, row=0, sticky="N,S,E,W")
 
PlayFrame = ttk.Frame(MainFrame,height=700,width=900)
PlayFrame.grid(column=0,row=0,columnspan = 10,rowspan=10, sticky="W,N,S")
 
WidgetFrame = ttk.Frame(MainFrame)
WidgetFrame.grid(column=11,row=11)

stop_button = ttk.Button(WidgetFrame, text="Stop", command = stop_player)
stop_button.grid(column=0,row=0,columnspan = 10,rowspan=10)

player = mpv.MPV(wid=str(int(PlayFrame.winfo_id())), input_default_bindings=True,input_vo_keyboard=True,osc=True)
 
player.play('/home/rainer/Videos/Garage62-2021-02-06__09-16-51.mkv')


tk.mainloop()
rfeyer likes this post
Reply
#8
TOTALLY appreciated!

It certainly worked, and, again, has helped immensely!

I am sure I will be able to build the rest of buttons/ lists/ checkboxes I need, but am also sure there will be more hurdles.
TY
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 348 Mar-17-2024, 09:37 AM
Last Post: deanhystad
Question [Tkinter] Sections of the code causes the GUI to freeze. What should I do? Wilkk 13 3,451 Jan-11-2023, 07:16 PM
Last Post: Wilkk
  tkinter window and turtle window error 1885 3 6,625 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] tkinter freeze DeanAseraf1 0 2,091 Jul-20-2019, 07:46 AM
Last Post: DeanAseraf1
  Tkinter GUI freeze petterg 4 10,833 Jul-01-2019, 03:54 PM
Last Post: petterg
  [Tkinter] GUI Freeze/tkinter SamGer 2 4,062 Jun-24-2019, 07:25 PM
Last Post: noisefloor
  [PyQt] Embedding a Video in Video Player WhatsupSmiley 0 5,828 Jan-28-2019, 06:24 PM
Last Post: WhatsupSmiley
  update a variable in parent window after closing its toplevel window gray 5 8,979 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020