I receveid a question: how do I add audio to video using tkvideo. I also receveid the code that actually played the video but it had no audio.
In fact the page of https://pypi.org/project/tkVideo/ says that Python module for playing videos (without sound) inside tkinter Label widget using Pillow and imagei.
I forked tkvideo to add the audio.
It works this way. If you run the following script the first time, it will use ffmpeg to create the mp3 file with the audio and then it will reproduce it together with the audio. The next time you will run it, it will not be necessary to produce the mp3 again. The name of the mp3, that you will find in the same directory of the example.py and of the video, will be the same of the video with the extention mp3, of course.
I called my version tkvideoaudio. Let’s see how to install it. You can do it only from github (you can always copy the file in the repository and put it in your folder, but installing it you can simply import it).
How to install tkvideoaudio (tkvideo + sync audio)
Go in the cmd and write this:
pip install tkvideoaudio@git+https://github.com/formazione/tkvideoaudio
Once you installed it you can use the example without changing anything from the version of tkvideo.
''' to install tkvideoaudio pip install tkvideoaudio@git+https://github.com/formazione/tkvideoaudio ''' import tkinter as tk from tkvideoaudio import tkvideo import pygame root = tk.Tk() root.title("video play") label_video=tk.Label(root) label_video.pack() video_file=tkvideo("001.mp4",label_video,loop=1,size=(800,600)) video_file.play() root.mainloop() pygame.quit()
Apart from the pygame.quit() at the end (maybe I could put that in the tkvideoaudio.py file to avoid having to do it.
The repository (the fork)
https://github.com/formazione/tkvideoaudio
The original repository
https://github.com/huskeee/tkvideo
Final consideration
So, I was thinking to do a lot of more explanations, but, since I put everything in github and you can install it as I showed you above, there’s no need to explain. Maybe I will go into details in a next post.
See ya.
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame