Add support for creating VideoFrame
directly from bytes (from_bytes
)
#1513
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.
This pull request introduces a new feature to the
VideoFrame
class, allowing the creation of aVideoFrame
object directly from byte data. With the newfrom_bytes
method, it's possible to build a frame without the need for an intermediate converter likePIL
orNumPy
.Key changes:
from_bytes
method: Allows creating aVideoFrame
from bytes, specifying width, height, image format, and options for horizontal and vertical flipping.copy_array_to_plane
function: Updated to call a new helper function,copy_bytes_to_plane
, which handles byte copying with support for flipping.These changes remove the need for intermediate converters, such as
PIL
orNumPy
, streamlining the process of creating video frames directly from bytes. The implementation supports the"rgba"
format and raises an exception for unsupported formats.Performance Comparison:
Here are the results of the performance tests for different implementations (using a
1280x720px
buffer in rgba format, unsigned byte):Implementation 1: Using PIL to create an image and then converting it to a VideoFrame:
Implementation 1 - Took ~ 0.0301 seconds.
Implementation 2: Directly creating a VideoFrame from bytes, with support for flipping:
Implementation 2 - Took ~ 0.0023 seconds.
That is a performance improvement of around 13x!