Posts: 18
Threads: 3
Joined: Feb 2021
Feb-19-2021, 11:30 PM
(This post was last modified: Feb-19-2021, 11:30 PM by rfeyer.)
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()
Posts: 1,034
Threads: 16
Joined: Dec 2016
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()
Posts: 18
Threads: 3
Joined: Feb 2021
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!
Posts: 1,034
Threads: 16
Joined: Dec 2016
Feb-20-2021, 08:23 PM
(This post was last modified: Feb-20-2021, 08:25 PM by Axel_Erfurt.)
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
Posts: 18
Threads: 3
Joined: Feb 2021
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!
Posts: 18
Threads: 3
Joined: Feb 2021
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()
Posts: 1,034
Threads: 16
Joined: Dec 2016
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()
Posts: 18
Threads: 3
Joined: Feb 2021
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
|