Skip to content
Advertisement

How run an mp4 in Tkinter?

I have been trying to run an mp4 in Tkinter and have been getting an error message about a couple line errors in –main–.py, and runpy.py. I’m a newbie and this is confusing me because I’ve been spending hours trying to fix this. I am also having a problem in the file path for my video, so help on that would also be greatly appreciated! Here is the code I am running!

import tkinter as tk, threading
import imageio
from PIL import Image, ImageTk
⠀
video_name = "C:UsersJacob KuhnVideosRotomdex.mp4" #This is my video file path
video = imageio.get_reader(video_name)
⠀
def stream(label):
⠀
    for image in video.iter_data():
        frame_image = ImageTk.PhotoImage(Image.fromarray(image))
        label.config(image=frame_image)
        label.image = frame_image
⠀
if __name__ == "__main__":
⠀
    root = tk.Tk()
    my_label = tk.Label(root)
    my_label.pack()
    thread = threading.Thread(target=stream, args=(my_label,))
    thread.daemon = 1
    thread.start()
    root.mainloop()

and the error sent.

Traceback (most recent call last):
 File "C:UsersJacob KuhnAppDataLocalProgramsPythonPython310librunpy.py", line 196, in _run_module_as_main 
return _run_code(code, main_globals, None,
 File "C:UsersJacob KuhnAppDataLocalProgramsPythonPython310librunpy.py", line 86, in _run_code
File "c:program filesmicrosoft visual studio2022communitycommon7ideextensionsmicrosoftpythoncoredebugpy__main__.py", line 45, in <module>
cli.main()
File "c:program filesmicrosoft visual studio2022communitycommon7ideextensionsmicrosoftpythoncoredebugpy/..debugpyservercli.py", line 444, in main
run()
File "c:program filesmicrosoft visual studio2022communitycommon7ideextensionsmicrosoftpythoncoredebugpy/..debugpyservercli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:UsersJacob KuhnAppDataLocalProgramsPythonPython310librunpy.py", line 268, in run_path
code, fname = _get_code_from_file(run_name, path_name)
File "C:UsersJacob KuhnAppDataLocalProgramsPythonPython310librunpy.py", line 242, in _get_code_from_file
code = compile(f.read(), fname, 'exec')
File "C:UsersJacob KuhnsourcereposRotomDexRotomDexRotomDex.py", line 5
video_name = "C:UsersJacob KuhnVideosRotomdex.mp4" #This is my video file path
                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
Press any key to continue . . .

Anybody got a clue?

Advertisement

Answer

Use ‘/’ forward slash instead of backward slash while specify the path. So you would write this instead:

C:/Users/Jacob Kuhn/Videos/Rotomdex.mp4
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement