Skip to content

Commit

Permalink
fix: change of codec in Video class
Browse files Browse the repository at this point in the history
  • Loading branch information
blankjul committed Sep 15, 2021
1 parent 7b5fd55 commit 564abaf
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ docs/source/*.html

*.gif
*.mp4
*.webm

**/.DS_Store

Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Configuration file for the Sphinx documentation builder.
# Config file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
Expand Down
52 changes: 30 additions & 22 deletions docs/source/index.ipynb

Large diffs are not rendered by default.

36 changes: 23 additions & 13 deletions pyrecorder/scratch.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import matplotlib.pyplot as plt
import numpy as np

from pyrecorder.converters.matplotlib import Matplotlib
from pyrecorder.recorder import Recorder
from pyrecorder.writers.gif import GIF
from pyrecorder.writers.video import Video

# create a writer object (here, mp4)
writer = Video("video.mp4")
# initialize the converter which is creates an image when `record()` is called
converter = Matplotlib()

writer = GIF("github.gif")
writer = GIF("medium2.gif", duration=0.5)

# use the with statement to close the recorder when done
with Recorder(writer) as rec:
rec = Recorder(writer, converter=converter)

# record 10 different snapshots
for t in range(10):
for t in range(50):

# create the plot (here, using matplotlib)
X = np.random.random((50, 2))
plt.scatter(X[:, 0], X[:, 1], facecolor="none", edgecolor="red")
# let us create a local figure object with two sub figures
fig, (ax1, ax2) = plt.subplots(2, figsize=(3, 4))

# use the record to store the current plot
rec.record()
X = np.random.random((100, 2))
ax1.scatter(X[:, 0], X[:, 1], color="green")

X = np.random.random(5)
ax2.pie(X)
# ax2.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.

# fix the size of figure and legends
fig.tight_layout()

# take a snapshot the specific figure object with the recorder
rec.record(fig=fig)


rec.close()
13 changes: 10 additions & 3 deletions pyrecorder/writers/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Video(Writer):

def __init__(self,
fname,
codec=cv2.VideoWriter_fourcc('m', 'p', '4', 'v'),
codec='mp4v',
fps=1,
) -> None:
super().__init__()
Expand All @@ -18,10 +18,17 @@ def __init__(self,
self.file = None

def write(self, frame, n_frames=1):

if self.file is None:
height, width, layers = frame.shape
self.file = cv2.VideoWriter(self.fname, self.codec, self.fps, frameSize=(width, height))

fourcc = self.codec
if isinstance(fourcc, str):
fourcc = cv2.VideoWriter_fourcc(*self.codec)

self.file = cv2.VideoWriter(self.fname,
fourcc,
self.fps,
frameSize=(width, height))

for k in range(n_frames):
self.file.write(frame)
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Scientific/Engineering :: Visualization',
'Topic :: Multimedia :: Recorder'
]
Expand Down

0 comments on commit 564abaf

Please sign in to comment.