moviepy icon indicating copy to clipboard operation
moviepy copied to clipboard

how to create clipvideo from bytes

Open elrondfeng opened this issue 2 years ago • 1 comments

I am writing an Azure function to convert a video file from storage. when the video passes in to the function, it is in the format of bytes. How can I create the clipvideo from it? I red some info online, the answer is use the make_frame(t) function and pass the function to clipvideo function, however, I am still very confused how to write the make_frame function. Can anyone provide a sample code of the function here?

elrondfeng avatar Sep 13 '22 14:09 elrondfeng

a following question to this is how to convert a videochip object to binary bytes?

elrondfeng avatar Sep 14 '22 17:09 elrondfeng

This question will depend in the format on your data. Don't confuse between the format and the encoding. Your encoding is binary but the format will depend on the video media type.

In summary, you need to convert from your binary data to a numpy array. Can be understood with the following example:

from moviepy.video.VideoClip import ColorClip


clip = (ColorClip(color=(255, 0, 0), size=(2, 2))
        .with_fps(1)
        .with_duration(5))

previous_make_frame = clip.make_frame

def make_frame(t):
    result = previous_make_frame(t)
    print(t, result)
    return result

clip.make_frame = make_frame

clip.write_videofile("test.mp4", fps=1, codec="libx264", audio=False)

If you run it, the output will be:

Moviepy - Building video test.mp4.
Moviepy - Writing video test.mp4

frame_index:   0%|                              | 0/5 [00:00<?, ?it/s, now=None]
0.0 [[[255   0   0]
  [255   0   0]]

 [[255   0   0]
  [255   0   0]]]
1.0 [[[255   0   0]
  [255   0   0]]

 [[255   0   0]
  [255   0   0]]]
2.0 [[[255   0   0]
  [255   0   0]]

 [[255   0   0]
  [255   0   0]]]
3.0 [[[255   0   0]
  [255   0   0]]

 [[255   0   0]
  [255   0   0]]]
4.0 [[[255   0   0]
  [255   0   0]]

 [[255   0   0]
  [255   0   0]]]
Moviepy - Done !                                                                
Moviepy - video ready test.mp4

So make_frame is a function which takes a t parameter with the interpolated time for the current frame and returns a numpy array with the content of the frame for each pixel. In this case are 8-bits RGBs with the color red (255, 0, 0).

a following question to this is how to convert a videochip object to binary bytes?

Again, that would depend on the format of your data, but you can convert them to numpy arrays as bytes with the tobytes method. You can iter the frames of a clip with iter_frames:

import numpy as np

np.array([frame for frame in clip.iter_frames()]).tobytes()

mondeja avatar Oct 10 '22 13:10 mondeja

@mondeja Can you provide the same example for a video that is converted to bytes?

pooya-mohammadi avatar Feb 09 '23 10:02 pooya-mohammadi