Skip to content

Commit

Permalink
feat: record screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdur-rahmaanJ committed May 4, 2022
1 parent 2f34a03 commit 73ee397
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 20 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,54 @@ For about the same number of lines for a simple snake game, you get one complete
- [Display most frequent words using PyGame](https://www.pythonkitchen.com/display-most-frequent-words-python-pygame/)
- [Realtime CPU monitor using PyGame](https://www.pythonkitchen.com/realtime-cpu-monitor-using-pygame/)

# (new) record video

```python
"""
Author: Abdur-Rahmaan Janhangeer
Github: https://github.com/Abdur-rahmaanJ
"""

from hooman import Hooman

import pygame

hapi = Hooman(500, 500)

counter = 1
def handle_events(event):
if event.type == pygame.QUIT:
hapi.is_running = False


hapi.handle_events = handle_events

while hapi.is_running:
hapi.background((255, 255, 255))

hapi.stroke_size(5)
hapi.stroke((0, 255, 0))

for i in range(0, hapi.WIDTH, 20):
hapi.line(i, 0, hapi.mouseX(), hapi.mouseY())


hapi.record()
hapi.flip_display()
hapi.event_loop()


pygame.quit()
hapi.save_record('mov.mp4')
```

# (new) screenshot


```
hapi.save(path)
```

# (new) save to svg

```python
Expand Down
2 changes: 1 addition & 1 deletion hooman/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .hooman import Hooman
from .formula import *

version_info = (0, 7, 1)
version_info = (0, 8, 0)
__version__ = ".".join([str(v) for v in version_info])
Binary file added hooman/demos/mov.mp4
Binary file not shown.
37 changes: 37 additions & 0 deletions hooman/demos/record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Author: Abdur-Rahmaan Janhangeer
Github: https://github.com/Abdur-rahmaanJ
"""

from hooman import Hooman

import pygame

hapi = Hooman(500, 500)

counter = 1
def handle_events(event):
if event.type == pygame.QUIT:
hapi.is_running = False


hapi.handle_events = handle_events

while hapi.is_running:
hapi.background((255, 255, 255))

hapi.stroke_size(5)
hapi.stroke((0, 255, 0))

for i in range(0, hapi.WIDTH, 20):
hapi.line(i, 0, hapi.mouseX(), hapi.mouseY())


hapi.record()
hapi.flip_display()
hapi.event_loop()


pygame.quit()

hapi.save_record('mov.mp4')
36 changes: 18 additions & 18 deletions hooman/demos/test.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
"""
Author: Abdur-Rahmaan Janhangeer
Github: https://github.com/Abdur-rahmaanJ
"""

from hooman import Hooman

import pygame

window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

hapi = Hooman(500, 500)

counter = 1
def handle_events(event):
if event.type == pygame.QUIT:
hapi.is_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
hapi.is_running = False


hapi.handle_events = handle_events

clock = pygame.time.Clock()

hapi.set_background(hapi.color["white"])

while hapi.is_running:
hapi.background((255, 255, 255))

hapi.fill(100)
hapi.stroke(0)
hapi.stroke_size(4)
hapi.regular_polygon(100, 200, 50, 50, hapi.mouseX() // 50)
hapi.rotate(hapi.mouseY() // 2)
hapi.stroke_size(5)
hapi.stroke((0, 255, 0))

hapi.event_loop()
for i in range(0, hapi.WIDTH, 20):
hapi.line(i, 0, hapi.mouseX(), hapi.mouseY())

hapi.flip_display()

clock.tick(60)
hapi.record()
hapi.flip_display()
hapi.event_loop()


pygame.quit()

hapi.save_record('mov.mp4')
62 changes: 62 additions & 0 deletions hooman/hooman.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import sys
import os
import shutil
import uuid

import pygame
import pygame.gfxdraw
Expand Down Expand Up @@ -129,6 +132,9 @@ def __init__(self, WIDTH, HEIGHT, svg=False):
self._svg = svg
self._svg_commands = []

self.session_id = uuid.uuid1()
self._session_vars = {}

#
# colors
#
Expand Down Expand Up @@ -604,3 +610,59 @@ def scatterchart(self, x, y, width, height, params={}, **kwargs):
def save_svg(self, path):
# print(self._svg_commands)
SVG.save(self._svg_commands, path, self.WIDTH, self.HEIGHT)


#
# img
#


def save(self, path):
self.pygame.image.save(self.screen, path)


#
# video
#

def record(self):
if 'record_counter' not in self._session_vars:
self._session_vars['record_counter'] = 1

if self._session_vars['record_counter'] == 1:
try:
shutil.rmtree( 'hoomanvid' )
except FileNotFoundError:
pass

try:
os.mkdir('hoomanvid')
except:
pass
self.save(f"hoomanvid/{self._session_vars['record_counter']}.png")
self._session_vars['record_counter'] += 1

def save_record(self, path):

try:
import ffmpeg
except:
try:
shutil.rmtree( 'hoomanvid' )
except FileNotFoundError:
pass
sys.exit("You need to pip install ffmpeg-python and ensure ffmpeg is installed to use this feature")

(
ffmpeg
.input('hoomanvid/*.png', pattern_type='glob', framerate=25)
.output(path)
.run()
)

try:
shutil.rmtree( 'hoomanvid' )
except FileNotFoundError:
pass


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
keywords="human pygame canvas api wrapper", # Optional
keywords="human pygame canvas api wrapper game", # Optional
# You can just specify package directories manually here if your project is
# simple. Or you can use find_packages().
#
Expand Down

0 comments on commit 73ee397

Please sign in to comment.