Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better video support for codecs and container types + Fix Loader.load #4

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions granular/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def decode_image(buffer, *args):
return np.asarray(Image.open(io.BytesIO(buffer)))


def encode_mp4(array, fps=20):
def encode_video(array, fps=20, format='mp4', codec='h264'):
import av
T, H, W = array.shape[:3]
fp = io.BytesIO()
output = av.open(fp, mode='w', format='mp4')
stream = output.add_stream('mpeg4', rate=float(fps))
output = av.open(fp, mode='w', format=format)
stream = output.add_stream(codec, rate=float(fps))
stream.width = W
stream.height = H
stream.pix_fmt = 'yuv420p'
Expand All @@ -85,14 +85,14 @@ def encode_mp4(array, fps=20):
return fp.getvalue()


def decode_mp4(buffer, *args):
def decode_video(buffer, *args):
import numpy as np
import av
container = av.open(io.BytesIO(buffer))
stream = container.streams.video[0]
T, H, W = stream.frames, stream.height, stream.width
array = np.empty((T, H, W, 3), dtype=np.uint8)
for t, frame in enumerate(container.decode(video=0)):
array[t] = frame.to_ndarray(format='rgb24')
array = []
for frame in container.decode(video=0):
array.append(frame.to_ndarray(format='rgb24'))
array = np.stack(array)
container.close()
return array

Expand All @@ -106,7 +106,8 @@ def decode_mp4(buffer, *args):
'tree': encode_tree,
'jpg': bind(encode_image, format='jpg'),
'png': bind(encode_image, format='png'),
'mp4': encode_mp4,
'mp4': bind(encode_video, format='mp4', codec='h264'),
'webm': bind(encode_video, format='webm', codec='vp9'),
}


Expand All @@ -119,5 +120,6 @@ def decode_mp4(buffer, *args):
'tree': decode_tree,
'jpg': decode_image,
'png': decode_image,
'mp4': decode_mp4,
'mp4': decode_video,
'webm': decode_video,
}
5 changes: 3 additions & 2 deletions granular/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ def load(self, d):
self._receive()
self.consumed = self.step = d['step']
self.seed = d['seed']
for _ in range(self.prefetch):
self._request()
if self.started:
for _ in range(self.prefetch):
self._request()

def close(self):
self.stop.set()
Expand Down
Loading