Tkinter: start mp4 with ffplay

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.

https://www.ffmpeg.org/
The link to download https://www.ffmpeg.org/

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.

https://ffmpeg.org/documentation.html

Documentazione su ffmpeg

How to install ffmpeg amd ffplay

Go to ffmpeg.org install the windows version:

  1. choose the windows version
  2. download the zip file
  3. unzip it
  4. rename the unzipped file like “ffmpeg”
  5. cut the file
  6. copy it in the c: directory (for example)
  7. go in to the bin subfolder (where there are ffplay.exe enad ffmpeg.exe)
  8. copy the path to the bin folder
  9. choose system
  10. choose advaced option
  11. choose environment variables
  12. choose path
  13. choose edit
  14. choose new
  15. paste the path
  16. press ok
  17. go into the cmd
  18. type ffmpeg -version
  19. if appears the number of the version (and other stuff) it’s ok
  20. go on to the next paragraph
ffmpeg download
ffmpeg download
Download the version you want (the first option should be good)

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

Advertisement