Play video with sound with python !! (tkinter / imageio)

1. Operation check environment

python3 (The operation on python2 is unconfirmed. Added on 2020/5/21) linux(debian 64bit) cpu: intel core i7

2. What you need to execute the code in this article

5/20 postscript:

pip3 install tkmedia

And ffmpeg can be installed by installing (5/30 postscript).

python module

tkinter (included with python, install python3-tk for linux imageio pillow pydub sounddevice numpy Also, some modules that come with python (All can be installed with "pip3 install [module name]".)

Other software

ffmpeg

(Also, a PC with moderate performance (Company's Tetsuwan OOO and Sele OO starting with i are not recommended)

3. How to use the code in this article

Place the files as follows:

play_video
|-__init__.py(Sky)
|-audio.py
|-video.py

Run the following in the directory where directory "play_video" is located:

import play_video.video
import play_video.audio
import tkinter

video = video.Video()
audio = audio.Audio()

root = tkinter.Tk()
root.frame = tkinter.Label(root)
root.frame.pack()

video.openfile("[videofile_path]")
audio.openfile("[videofile_path]",)

audio.play()
video.play()
root.mainloop()

Leave the license free. Feel free to edit, redistribute, use for commercial purposes, etc. (I don't know if anyone uses it) If possible, please let us know in the comments or email ([email protected]). Also, please check the license of the library and software you are using. However, the author does not take any responsibility, so thank you.

4. Audio playback

Use pydub and sounddevice (and numpy)

audio.py


from . import sounddevice
import pydub
import time
import numpy

class Audio():
    def __init__(self):
        pass
    def openfile(self, filepath):
        if ".mp3" in filepath:
            self.segment = pydub.AudioSegment.from_file(filepath,codec="mp3")
        elif ".wav" in filepath:
            self.segment = pydub.AudioSegment.from_file(filepath,codec="wav")
        elif ".mp4" in filepath:
            self.segment = pydub.AudioSegment.from_file(filepath)
        else:
            self.segment = pydub.AudioSegment.from_file(filepath)
    def play(self, place=0):
        if self.segment.channels != 1:
            self.samples = numpy.array(self.segment.get_array_of_samples().tolist(),dtype="int16").reshape(-1,self.segment.channels)
        else:
            self.samples = numpy.array(self.segment.get_array_of_samples().tolist(),dtype='int16')
        sounddevice.play(self.samples,self.segment.frame_rate)
    def stop(self):
        sounddevice.stop()

5. Video playback

Read it with imageio and display it in the frame of tkinter via PIL (that is multithreaded).

video.py


import tkinter
from tkinter import ttk
import imageio
from PIL import ImageTk, Image
import time
import threading
from imageio.plugins.ffmpeg import FfmpegFormat

class Video():
    def __init__(self):
        format = FfmpegFormat(
            "ffmpeg",
            "Many video formats and cameras (via ffmpeg)",
            ".mov .avi .mpg .mpeg .mp4 .mkv .wmv .webm",
            "I",
            )
        imageio.formats.add_format(format,True)#Make it compatible with webm.(Quite forcibly)
    def openfile(self, file_path,frame):
        self.frame = frame
        try:
            self.video = imageio.get_reader(file_path)
        except imageio.core.fetching.NeedDownloadError:
            imageio.plugins.avbin.download()
            self.video = imageio.get_reader(file_path)
    def play(self):
        self.video_thread = threading.Thread(target=self._stream)
        self.video_thread.start()
    def stop(self):
        self.video_thread.stop()
    def _stream(self):
        start_time=time.time()
        sleeptime = 1/self.video.get_meta_data()["fps"]
        frame_now = 0
        for image in self.video.iter_data():
            frame_now = frame_now + 1
            if frame_now*sleeptime >= time.time()-start_time:
                frame_image = ImageTk.PhotoImage(Image.fromarray(image))
                self.frame.config(image=frame_image)
                self.frame.image = frame_image
                time.sleep(sleeptime)
            else:
                pass

Recommended Posts

Play video with sound with python !! (tkinter / imageio)
Play with 2016-Python
Programming with Python and Tkinter
YouTube video management with Python 3
[Python] Play with Discord's Webhook.
Run Label with tkinter [Python]
Play RocketChat with API / Python
Let's play with Excel with Python [Beginner]
[Python] Creating multiple windows with Tkinter
Play audio files from Python with interrupts
Play handwritten numbers with python Part 2 (identify)
Save video frame by frame with Python OpenCV
Fractal to make and play with Python
Develop Windows apps with Python 3 + Tkinter (Preparation)
I want to play with aws with python
Play with Prophet
Create a frame with transparent background with tkinter [Python]
Python hand play (let's get started with AtCoder?)
FizzBuzz with Python3
Scraping with Python
[REAPER] How to play with Reascript in Python
Play with Google Spread Sheets in python (OAuth)
Statistics with python
Scraping with Python
Python with Go
Play with PyTorch
GUI image cropping tool made with Python + Tkinter
Play with Lambda layer (python) for about 5 minutes
Twilio with Python
Integrate with Python
Play Python async
[Python] I made a Youtube Downloader with Tkinter.
AES256 with python
Tested with Python
MVC with Tkinter
Develop Windows apps with Python 3 + Tkinter (exe file)
python starts with ()
with syntax (Python)
How to use tkinter with python in pyenv
Bingo with python
Zundokokiyoshi with python
Play with CentOS 8
Play with Pyramid
Excel with Python
Play with Fathom
Microcomputer with Python
[Python] Tkinter template
Cast with python
[Let's play with Python] Make a household account book
How to loop and play gif video with openCV
Display and shoot webcam video with Python Kivy [GUI]
[Python] Create a Tkinter program distribution file with cx_Freeze
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Display USB camera video with Python OpenCV with Raspberry Pi
[Python] Road to a snake charmer (5) Play with Matplotlib
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python