Reputation: 169
I was just wondering if making an interactive video in Python 3.6 would be possible? I looked at options for actually inputting videos into Tkinter using python-gstreamer but I couldn't get it to work.
So could anyone suggest a way and explain how to get a video to play in Python? So perhaps there could be a Tkinter window and this video could be displayed in this frame?
If Python isn't possible any other ideas are welcome.
Thank you
Upvotes: 0
Views: 2208
Reputation: 1423
You can try this with the Video widget in a jupyter notebook:
from ipywidgets import Video, Image
from IPython.display import display
from ipywidgets import Checkbox
fileA= 'videoA.mp4'
fileB= 'videoB.mp4'
video = Video.from_file(fileB)
top_toggle = Checkbox(description='Change Video')
def video_loader(filename):
with open(filename, 'rb') as f:
video.value = f.read()
def video_change(button):
if button['new']:
video_loader(fileA)
else:
video_loader(fileB)
top_toggle.observe(video_change, names='value')
display(top_toggle)
display(video)
See also here: https://github.com/QuantStack/quantstack-talks/blob/master/2018-11-14-PyParis-widgets/notebooks/1.ipywidgets.ipynb
Upvotes: 1
Reputation: 26647
The following code makes a Tkinter window and displays a video in it. You will need to install some dependencies if you don't already have the required libraries.
import tkinter as tk, threading
import imageio
from PIL import Image, ImageTk
name = "video.mp4" # video file path
video = imageio.get_reader(name)
def stream(label):
for data in video.iter_data():
frame_image = ImageTk.PhotoImage(Image.fromarray(data))
label.config(image=frame_image)
label.image = frame_image
if __name__ == "__main__":
root = tk.Tk()
video_label = tk.Label(root)
video_label.pack()
thread = threading.Thread(target=stream, args=(video_label,))
thread.daemon = 1
thread.start()
root.mainloop()
Upvotes: 0