FFmpeg and ffplay
You will need the ffmpeg free software to use the app we will make with Python and tkinter. Here is a link to the site.
Ffmpeg is free, as I said, and it has a lot of support and it is very powerful.
It has a lot of option and it has a command line interface that could be a little overwhelming at first (and also further) sight. There comes Python in help to make a little GUI, with tkinter, that allows us to use it easily and suitable for out needs- This time we will create a GUI to open and start mp4 files with ffplay, icluded in ffmpeg.
How to install ffmpeg amd ffplay
Go to ffmpeg.org install the windows version:
- choose the windows version
- download the zip file
- unzip it
- rename the unzipped file like “ffmpeg”
- cut the file
- copy it in the c: directory (for example)
- go in to the bin subfolder (where there are ffplay.exe enad ffmpeg.exe)
- copy the path to the bin folder
- choose system
- choose advaced option
- choose environment variables
- choose path
- choose edit
- choose new
- paste the path
- press ok
- go into the cmd
- type ffmpeg -version
- if appears the number of the version (and other stuff) it’s ok
- go on to the next paragraph


App to start video
Let’s create a window with the list of the mp4 in a listbox.
# use ffmplay to play the videos with tkinter
import tkinter as tk
import tkinter.ttk as ttk
import os
root = tk.Tk()
lb = tk.Listbox(root)
lb.pack()
for file in os.listdir():
if file.endswith(".mp4"):
lb.insert(0, file)
root.mainloop()
Let’s start the movie
Now we add a button and an action that activate a function when the mouse button is pressed to make the selected movie start. To stop the movie press q or esc.
# use ffmplay to play the videos with tkinter
import tkinter as tk
import tkinter.ttk as ttk
import os
root = tk.Tk()
lb = tk.Listbox(root)
lb.pack()
def ffplay(event):
if lb.curselection():
file = lb.curselection()[0]
os.system("ffplay " + lb.get(file))
for file in os.listdir():
if file.endswith(".mp4"):
lb.insert(0, file)
bstart = ttk.Button(root, text="Start movie")
bstart.pack()
bstart.bind("<ButtonPress-1>", ffplay)
root.mainloop()
Start an mp4 file with your default software for videos (without ffplay)
# use ffmplay to play the videos with tkinter
import tkinter as tk
import tkinter.ttk as ttk
import os
root = tk.Tk()
lb = tk.Listbox(root)
lb.pack()
def ffplay(event):
if lb.curselection():
file = lb.curselection()[0]
os.startfile(lb.get(file))
for file in os.listdir():
if file.endswith(".mp4"):
lb.insert(0, file)
bstart = ttk.Button(root, text="Start movie")
bstart.pack()
bstart.bind("<ButtonPress-1>", ffplay)
root.mainloop()
Starting a video with tkvideo
import tkinter as tk
from tkvideo import tkvideo
import pygame
import os
def get_audio(file):
from moviepy.editor import VideoFileClip
# Load the MP4 file
video = VideoFileClip(file + ".mp4")
# Extract the audio
audio = video.audio
# Save the audio as an MP3 file
audio.write_audiofile(file + ".mp3")
def start(file):
if not file + ".mp3" in os.listdir("."):
get_audio(file)
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file + ".mp3")
root = tk.Tk()
# root.geometry("640x480")
videoPlayer = tk.Label(root)
videoPlayer.pack()
video = tkvideo(file + ".mp4", videoPlayer, loop=1, size=(640,480))
video.play()
pygame.mixer.music.play()
root.mainloop()
start("froggo")
Tkinter test for students
Tkinter articles
