You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
918 B
44 lines
918 B
3 years ago
|
import time
|
||
|
from threading import Thread
|
||
|
|
||
|
import av
|
||
|
from PIL import Image
|
||
|
|
||
|
|
||
|
class VideoFile(Thread):
|
||
|
file: str = ""
|
||
|
frame: Image = None
|
||
|
running: bool = False
|
||
|
|
||
|
def __init__(self, file: str):
|
||
|
self.file = file
|
||
|
super().__init__()
|
||
|
|
||
|
def get_frame(self):
|
||
|
return self.frame
|
||
|
|
||
|
def stop(self):
|
||
|
self.running = False
|
||
|
|
||
|
def open(self):
|
||
|
self.running = True
|
||
|
self.start()
|
||
|
|
||
|
def run(self) -> None:
|
||
|
container = av.open(self.file)
|
||
|
video = container.streams.video[0]
|
||
|
print(video.time_base)
|
||
|
|
||
|
while self.running:
|
||
|
try:
|
||
|
for frame in container.decode(video):
|
||
|
|
||
|
if self.running:
|
||
|
self.frame = frame.to_image()
|
||
|
else:
|
||
|
break
|
||
|
|
||
|
time.sleep(1.0 / 60)
|
||
|
except:
|
||
|
self.running = False
|