python3 (The operation on python2 is unconfirmed. Added on 2020/5/21) linux(debian 64bit) cpu: intel core i7
5/20 postscript:
pip3 install tkmedia
And ffmpeg can be installed by installing (5/30 postscript).
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]".)
ffmpeg
(Also, a PC with moderate performance (Company's Tetsuwan OOO and Sele OO starting with i are not recommended)
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.
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()
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