-
Notifications
You must be signed in to change notification settings - Fork 234
feat(v2): add video support #972
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a452268
feat: add video url and tensors to proto
3ccb697
feat: add video url and video ndarray
dc957d1
feat: add video torch tensor and tests
fc86920
fix: mypy checks
8a55e0b
chore: add av to video extra
5cb098a
fix: allow dim 3
3ba1f78
test: wip video load and save
be63926
refactor: move to numpy to computational backend
395a495
fix: video load and save
406ec80
test: adjust tests
091e79a
fix: video load and save and add docstrings
dee1146
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
e4106a8
fix: fix some imports after merging
23ee930
docs: add doc strings and fix example urls
7ab8dbd
docs: small fixes in docs
ecf01d8
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
5295dd1
refactor: rename save to mp4 file to save
b3f2ccb
feat: add shape method to comp backend
20ecf2c
refactor: move validate shape to video tensor mixin
711d105
refactor: extract private load and make separate methods for frames
0c9c1fd
fix: use torch shape instead of size method
e3a465c
fix: add typehint to shape in comp backend
40eac93
docs: add supported strings for skip type
a700f30
fix: apply suggestions from code review
94572fd
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
07ceae8
fix: small change to trigger ci again
c2e129d
fix: extract shape var
d50ae67
fix: introduce compbackendinterface
2e365e6
fix: revert previous pr and fix for mypy
c44a035
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
95b0b81
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-v…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: video load and save
Signed-off-by: anna-charlotte <[email protected]>
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,65 @@ | ||
| from abc import ABC | ||
| from typing import BinaryIO, TypeVar, Union | ||
| from typing import BinaryIO, Optional, TypeVar, Union | ||
|
|
||
| import numpy as np | ||
|
|
||
| from docarray.typing.tensor.abstract_tensor import AbstractTensor | ||
| from docarray.typing.tensor.audio.audio_tensor import AudioTensor | ||
|
|
||
| T = TypeVar('T', bound='AbstractVideoTensor') | ||
|
|
||
|
|
||
| class AbstractVideoTensor(AbstractTensor, ABC): | ||
| def save_to_file( | ||
| def save_to_mp4_file( | ||
| self: 'T', | ||
| file_path: Union[str, BinaryIO], | ||
| frame_rate: int = 24, | ||
| codec: str = 'h264', | ||
| audio_tensor: Optional[AudioTensor] = None, | ||
| video_frame_rate: int = 30, | ||
| video_codec: str = 'h264', | ||
| audio_frame_rate: int = 48000, | ||
| audio_codec: str = 'aac', | ||
| audio_format: str = 'fltp', | ||
| ) -> None: | ||
| """ | ||
| Save video tensor to a .mp4 file. | ||
|
|
||
| :param file_path: path to a .mp4 file. If file is a string, open the file by | ||
| that name, otherwise treat it as a file-like object. | ||
| :param frame_rate: frames per second. | ||
| :param codec: the name of a decoder/encoder. | ||
| :param video_frame_rate: frames per second. | ||
| :param video_codec: the name of a decoder/encoder. | ||
| """ | ||
| import av | ||
|
|
||
| np_tensor = self.get_comp_backend().to_numpy(array=self) # type: ignore | ||
| video_tensor = np_tensor.astype('uint8') | ||
| import av | ||
|
|
||
| with av.open(file_path, mode='w') as container: | ||
| if video_tensor.ndim == 3: | ||
| video_tensor = np.expand_dims(video_tensor, axis=0) | ||
|
|
||
| stream = container.add_stream(codec, rate=frame_rate) | ||
| stream.height = video_tensor.shape[-3] | ||
| stream.width = video_tensor.shape[-2] | ||
| stream_video = container.add_stream(video_codec, rate=video_frame_rate) | ||
| stream_video.height = video_tensor.shape[-3] | ||
| stream_video.width = video_tensor.shape[-2] | ||
|
|
||
| if audio_tensor is not None: | ||
| stream_audio = container.add_stream(audio_codec) | ||
| audio_np = audio_tensor.get_comp_backend().to_numpy(array=audio_tensor) | ||
| audio_layout = 'stereo' if audio_np.shape[-2] == 2 else 'mono' | ||
|
|
||
| for i, audio in enumerate(audio_np): | ||
| frame = av.AudioFrame.from_ndarray( | ||
| array=audio, format=audio_format, layout=audio_layout | ||
| ) | ||
| frame.rate = audio_frame_rate | ||
| for packet in stream_audio.encode(frame): | ||
| container.mux(packet) | ||
|
|
||
| for vid in video_tensor: | ||
| frame = av.VideoFrame.from_ndarray(vid, format='rgb24') | ||
| for packet in stream.encode(frame): | ||
| for packet in stream_video.encode(frame): | ||
| container.mux(packet) | ||
|
|
||
| for packet in stream.encode(None): | ||
| for packet in stream_audio.encode(None): | ||
| container.mux(packet) | ||
| for packet in stream_video.encode(None): | ||
| container.mux(packet) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.