-
Notifications
You must be signed in to change notification settings - Fork 0
/
videostream.py
166 lines (146 loc) · 5.72 KB
/
videostream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from ffpyplayer.player import MediaPlayer
from ffpyplayer import pic
try:
from PIL import Image
except ImportError:
from pil import Image
print('Error to load PIL lib, try: pip install pillow')
import time
import os
__author__ = 'Hernani Aleman Ferraz'
__email__ = 'afhernani@gmail.com'
__apply__ = 'Flash - player'
__version__ = '1.2'
class VideoStream:
def __init__(self, video_source=None):
ff_opts = {'paused': True, 'autoexit': False} # Audio options
self.video_surce = video_source
# Open the video source
self.player = MediaPlayer(video_source, ff_opts=ff_opts)
# TODO: colocar pausa de tiempo para cargas mediaplayer y obtener los datos
# conseguir el frame rate para la sincronizacion self.dalay
while self.player.get_metadata()['src_vid_size'] == (0, 0):
time.sleep(0.01)
data = self.player.get_metadata()
print('data -->', data)
self.f_rate = data['frame_rate']
print('delay -> ', self.f_rate)
self.w, self.h = data['src_vid_size']
print('WxH -> ', self.w, self.h)
self.pts = self.player.get_pts() # Returns the elapsed play time. float
print('pts ->', self.pts)
self.duration = data['duration']
print('duration', self.duration)
self.pause = self.player.get_pause() # Returns whether the player is paused.
print('pause ->', self.pause)
self.volume = self.player.get_volume() # Returns the volume of the audio. loat: A value between 0.0 - 1.0
print('volume ->', self.volume)
self.player.toggle_pause() # Toggles -alterna- the player’s pause state
# self.player.set_pause(False) # auses or un-pauses the file. state: bool
cond = True
while cond:
self.l_frame, self.val = self.player.get_frame()
if self.val == 'eof':
print('can not open source: ', video_source)
break
elif self.l_frame is None:
time.sleep(0.01)
else:
self._imagen, self.pts = self.l_frame
print('pts ->', self.pts)
# arr = self._imagen.to_memoryview()[0] # array image
# self.imagen = Image.frombytes("RGB", self.original_size, arr.memview)
# self.imagen.show()
cond = False
# propierties.
@property
def f_rate(self):
return self.__f_rate
@f_rate.setter
def f_rate(self, val):
import math
vn = val[0]
vd = val[1]
if vd <= 1:
self.__f_rate = vn
elif vd > 1 :
self.__f_rate = int(round(vn/vd))
else:
self.__f_rate = 30
# end properties.
def get_frame(self):
'''
Return valores:
val : 'eof' or 'pause'
pts : time location aduio imagen.
imagen : frame image
Return (val, t, imagen)
'''
self.l_frame, self.val = self.player.get_frame()
if self.val == 'eof':
# condicion final fichero, salimos if and while
# self.player.toggle_pause() # ponemos en pause
return self.val, None, None
elif self.l_frame is None:
time.sleep(0.01)
return self.val, None, None
else:
# import math
self._imagen, self.pts = self.l_frame
return self.val, self.pts, self._imagen
# w, h = self._imagen.get_size()
# linesize = [int(math.ceil(w * 3 / 32.) * 32)]
# self._imagen = pic.Image(plane_buffers=[bytes(b' ') * (h * linesize[0])],
# pix_fmt=self._imagen.get_pixel_format(), size=(w, h), linesize=linesize)
# self._imagen.get_linesizes(keep_align=True)
# if self.new_size is not None:
# sws = None
# n_w , n_h = self.new_size
# if n_w > n_h:
# sws = pic.SWScale(w, h, self._imagen.get_pixel_format(), oh=n_h)
# else:
# sws = pic.SWScale(w, h, self._imagen.get_pixel_format(), ow=n_w)
# self._imagen = sws.scale(self._imagen)
# size = self._imagen.get_size()
# arr = self._imagen.to_memoryview()[0] # array image
# self.imagen = Image.frombytes("RGB", size, arr.memview)
# print('>>> videostream::get_frame()::self.pts ->', self.pts)
def toggle_pause(self):
'''
Function: toggle_pause
'''
try: # Stopping audio
self.player.toggle_pause()
# self.player = None
except:
pass
def seek(self, pts=None, relative=False, accurate=False):
if not pts:
return
self.player.seek(pts, relative=False, accurate=False)
def snapshot(self, road=None):
'''
get current frame
'''
img = self.l_frame[0]
if img is not None:
size = img.get_size()
arr = img.to_memoryview()[0] # array image
img = Image.frombytes("RGB", size, arr.memview)
# vamos a guardar esto.
time_str = time.strftime("%d-%m-%Y-%H-%M-%S")
frame_name = f"frame-{time_str}.jpg"
if not road:
ruta = os.path.dirname(self.video_surce)
name_out = os.path.join(ruta, frame_name)
else:
name_out = os.path.join(road, frame_name)
img.save(name_out)
# Release the video source when the object is destroyed
def __del__(self):
self.player.close_player()
print('__del__')
if __name__ == '__main__':
video = VideoStream('_Work/tem.mp4')