Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load(self):
self.unload()
ff_opts = {'vn': True, 'sn': True} # only audio
self._ffplayer = MediaPlayer(self.source,
callback=self._callback_ref,
loglevel='info', ff_opts=ff_opts)
player = self._ffplayer
player.set_volume(self.volume)
player.toggle_pause()
self._state = 'paused'
# wait until loaded or failed, shouldn't take long, but just to make
# sure metadata is available.
s = time.clock()
while ((not player.get_metadata()['duration'])
and not self.quitted and time.clock() - s < 10.):
time.sleep(0.005)
def open_video(self):
self.player = MediaPlayer(self.source, ff_opts={'paused': True, 'ss': 1.0, 'an': True})
frame = None
while not frame:
frame, value = self.player.get_frame(force_refresh=True)
data = self.player.get_metadata()
self.length = data['duration']
self.framerate = data['frame_rate']
self.pixel_format = data['src_pix_fmt']
extension = os.path.splitext(fullpath)[1].lower()
try:
if extension in imagetypes:
#This is an image file, use PIL to generate a thumnail
image = Image.open(full_filename)
image.thumbnail((self.thumbsize, self.thumbsize), Image.ANTIALIAS)
if image.mode != 'RGB':
image = image.convert('RGB')
output = BytesIO()
image.save(output, 'jpeg')
thumbnail = output.getvalue()
elif extension in movietypes:
#This is a video file, use ffpyplayer to generate a thumbnail
player = MediaPlayer(full_filename, ff_opts={'paused': True, 'ss': 1.0, 'an': True})
frame = None
while not frame:
frame, value = player.get_frame(force_refresh=True)
player.close_player()
player = None
frame = frame[0]
frame_size = frame.get_size()
frame_converter = SWScale(frame_size[0], frame_size[1], frame.get_pixel_format(), ofmt='rgb24')
new_frame = frame_converter.scale(frame)
image_data = bytes(new_frame.to_bytearray()[0])
image = Image.frombuffer(mode='RGB', size=(frame_size[0], frame_size[1]), data=image_data, decoder_name='raw')
image = image.transpose(1)
image.thumbnail((self.thumbsize, self.thumbsize), Image.ANTIALIAS)
output = BytesIO()
def load(self):
self.unload()
ff_opts = {'vn': True, 'sn': True} # only audio
self._ffplayer = MediaPlayer(self.source,
callback=self._player_callback,
loglevel='info', ff_opts=ff_opts)
player = self._ffplayer
player.set_volume(self.volume)
player.toggle_pause()
self._state = 'paused'
# wait until loaded or failed, shouldn't take long, but just to make
# sure metadata is available.
s = time.perf_counter()
while ((not player.get_metadata()['duration']) and
not self.quitted and time.perf_counter() - s < 10.):
time.sleep(0.005)
def play(self):
if self._ffplayer and self._state == 'paused':
self._ffplayer.toggle_pause()
self._state = 'playing'
return
self.load()
self._out_fmt = 'rgba'
ff_opts = {
'paused': True,
'out_fmt': self._out_fmt,
'sn': True,
'volume': self._volume,
}
self._ffplayer = MediaPlayer(
self._filename, callback=self._player_callback,
thread_lib='SDL',
loglevel='info', ff_opts=ff_opts)
# Disabled as an attempt to fix kivy issue #6210
# self._ffplayer.set_volume(self._volume)
self._thread = Thread(target=self._next_frame_run, name='Next frame')
self._thread.daemon = True
self._thread.start()
image = image.convert('RGB')
output = BytesIO()
image.save(output, 'jpeg')
thumbnail = output.getvalue()
elif extension in movietypes:
#This is a video file, use ffpyplayer to generate a thumbnail
player = MediaPlayer(full_filename, ff_opts={'paused': True, 'ss': 1.0, 'an': True})
frame = None
while not frame:
frame, value = player.get_frame(force_refresh=True)
player.close_player()
player = None
frame = frame[0]
frame_size = frame.get_size()
frame_converter = SWScale(frame_size[0], frame_size[1], frame.get_pixel_format(), ofmt='rgb24')
new_frame = frame_converter.scale(frame)
image_data = bytes(new_frame.to_bytearray()[0])
image = Image.frombuffer(mode='RGB', size=(frame_size[0], frame_size[1]), data=image_data, decoder_name='raw')
image = image.transpose(1)
image.thumbnail((self.thumbsize, self.thumbsize), Image.ANTIALIAS)
output = BytesIO()
image.save(output, 'jpeg')
thumbnail = output.getvalue()
return thumbnail
except:
return None
self.filename = filename
images = []
while True:
frame, t = loader.next_frame()
if frame is None:
break
images.append(frame)
if not len(images):
raise Exception('No image found in {}'.format(filename))
w, h = images[0].get_size()
ifmt = images[0].get_pixel_format()
if ifmt != 'rgba' and ifmt != 'rgb24':
fmt = 'rgba'
sws = SWScale(w, h, ifmt, ofmt=fmt)
for i, image in enumerate(images):
images[i] = sws.scale(image)
else:
fmt = ifmt if ifmt == 'rgba' else 'rgb'
return [ImageData(w, h, fmt, img.to_memoryview()[0], source_image=img)
for img in images]
self.filename = filename
images = []
while True:
frame, t = loader.next_frame()
if frame is None:
break
images.append(frame)
if not len(images):
raise Exception('No image found in {}'.format(filename))
w, h = images[0].get_size()
ifmt = images[0].get_pixel_format()
if ifmt != 'rgba' and ifmt != 'rgb24':
fmt = 'rgba'
sws = SWScale(w, h, ifmt, ofmt=fmt)
for i, image in enumerate(images):
images[i] = sws.scale(image)
else:
fmt = ifmt if ifmt == 'rgba' else 'rgb'
return [ImageData(w, h, fmt, img.to_memoryview()[0], source_image=img)
for img in images]
self.first_frame = None
else:
self.player.set_pause(False)
frame = None
while not frame:
frame, value = self.player.get_frame(force_refresh=False)
if value == 'eof':
return None
self.player.set_pause(True)
self.frame_number = self.frame_number + 1
if self.max_frames:
if self.frame_number > self.max_frames:
return None
frame_image = frame[0]
frame_size = frame_image.get_size()
frame_converter = SWScale(frame_size[0], frame_size[1], frame_image.get_pixel_format(), ofmt='rgb24')
new_frame = frame_converter.scale(frame_image)
image_data = bytes(new_frame.to_bytearray()[0])
image = Image.frombuffer(mode='RGB', size=(frame_size[0], frame_size[1]), data=image_data, decoder_name='raw')
#for some reason, video frames are read upside-down? fix it here...
image = image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
if image.mode != 'RGB':
image = image.convert('RGB')
image = self.adjust_image(image, preview=False)
return [image, frame[1]]
def reload_edit_image(self, *_):
"""Regenerate the edit preview image."""
if self.video:
if not self.player:
return
location = self.length * self.position
frame = self.seek_player(location)
frame = frame[0]
frame_size = frame.get_size()
pixel_format = frame.get_pixel_format()
frame_converter = SWScale(frame_size[0], frame_size[1], pixel_format, ofmt='rgb24')
new_frame = frame_converter.scale(frame)
image_data = bytes(new_frame.to_bytearray()[0])
original_image = Image.frombuffer(mode='RGB', size=(frame_size[0], frame_size[1]), data=image_data, decoder_name='raw')
#for some reason, video frames are read upside-down? fix it here...
original_image = original_image.transpose(PIL.Image.FLIP_TOP_BOTTOM)
self.original_width = original_image.size[0]
self.original_height = original_image.size[1]
self.original_image = original_image
image = original_image.copy()
else:
original_image = Image.open(self.source)
try:
self.exif = original_image.info.get('exif', b'')
except: