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

resize video when time changed trigger a error #334

Closed
bluedazzle opened this issue Oct 14, 2016 · 3 comments
Closed

resize video when time changed trigger a error #334

bluedazzle opened this issue Oct 14, 2016 · 3 comments

Comments

@bluedazzle
Copy link
Contributor

I want to merge two videos and one has variable size and position when time changed.If the video change size in a very short period,it will trigger a error like this:

Traceback (most recent call last):
  File "/Users/RaPoSpectre/Desktop/cvtest/movi.py", line 59, in <module>
    final.write_videofile('nt.mp4', fps=24)
  File "<decorator-gen-51>", line 2, in write_videofile
  File "/usr/local/lib/python2.7/site-packages/moviepy/decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "<decorator-gen-50>", line 2, in write_videofile
  File "/usr/local/lib/python2.7/site-packages/moviepy/decorators.py", line 137, in use_clip_fps_by_default
    return f(clip, *new_a, **new_kw)
  File "<decorator-gen-49>", line 2, in write_videofile
  File "/usr/local/lib/python2.7/site-packages/moviepy/decorators.py", line 22, in convert_masks_to_RGB
    return f(clip, *a, **k)
  File "/usr/local/lib/python2.7/site-packages/moviepy/video/VideoClip.py", line 339, in write_videofile
    ffmpeg_params=ffmpeg_params)
  File "/usr/local/lib/python2.7/site-packages/moviepy/video/io/ffmpeg_writer.py", line 204, in ffmpeg_write_video
    fps=fps, dtype="uint8"):
  File "/usr/local/lib/python2.7/site-packages/tqdm/_tqdm.py", line 713, in __iter__
    for obj in iterable:
  File "/usr/local/lib/python2.7/site-packages/moviepy/Clip.py", line 473, in generator
    frame = self.get_frame(t)
  File "<decorator-gen-14>", line 2, in get_frame
  File "/usr/local/lib/python2.7/site-packages/moviepy/decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "/usr/local/lib/python2.7/site-packages/moviepy/Clip.py", line 95, in get_frame
    return self.make_frame(t)
  File "/usr/local/lib/python2.7/site-packages/moviepy/video/compositing/CompositeVideoClip.py", line 110, in make_frame
    f = c.blit_on(f, t)
  File "/usr/local/lib/python2.7/site-packages/moviepy/video/VideoClip.py", line 571, in blit_on
    return blit(img, picture, pos, mask=mask, ismask=self.ismask)
  File "/usr/local/lib/python2.7/site-packages/moviepy/video/tools/drawing.py", line 50, in blit
    1.0 * mask * blitted + (1.0 - mask) * blit_region)
ValueError: operands could not be broadcast together with shapes (149,112,3) (168,127,3) 

shape (168,127,3) is a shape create by a past time, shape(149,112,3) is a shape create by current time. Why this happened? It should be two shapes (149,112,3) broadcast together.

I find these two shapes created by function blit_on(self, picture, t):

def blit_on(self, picture, t):
        ...
        ct = t - self.start  # clip time

        # GET IMAGE AND MASK IF ANY

        img = self.get_frame(ct)    # shape(168, 127)
        mask = (None if (self.mask is None) else
                self.mask.get_frame(ct))   # shape(149, 112)
        ...
@bluedazzle
Copy link
Contributor Author

this is my code:

from moviepy.editor import *

last_move = None
last_move1 = None

lis = [(0.0, 113, 167, 47), (0.32, 138, 159, 47), (0.44, 152, 144, 47), (0.48, 193, 148, 47), (0.6, 193, 148, 47), (0.76, 205, 138, 55), (0.88, 204, 121, 63), (0.92, 190, 31, 127), (1.2, 183, 59, 127), (1.4, 137, 22, 127), (1.52, 137, 22, 127), (1.72, 129, 67, 127), (1.88, 123, 69, 127), (2.04, 131, 123, 63), (2.24, 130, 148, 63), (2.48, 130, 148, 63), (2.8, 138, 180, 63), (3.0, 138, 180, 63), (3.2, 146, 192, 63), (3.28, 105, 91, 151), (3.44, 105, 91, 151), (3.72, 11, 48, 151), (3.96, 5, 78, 151), (4.32, 4, 134, 1), (4.6, 149, 184, 48), (4.8, 145, 188, 48), (5.0, 154, 217, 48), (5.08, 163, 199, 48), (5.2, 163, 199, 48), (5.32, 164, 187, 48), (5.48, 163, 200, 48), (5.76, 163, 200, 48), (5.96, 173, 199, 48), (6.0, 133, 172, 48), (6.04, 128, 165, 48), (6.28, 128, 165, 48), (6.4, 129, 180, 48), (6.52, 133, 166, 48), (6.64, 133, 166, 48), (6.88, 144, 183, 48), (7.0, 153, 174, 48), (7.16, 153, 174, 48), (7.24, 153, 174, 48), (7.28, 253, 65, 104), (7.64, 253, 65, 104), (7.8, 279, 116, 80), (8.0, 290, 105, 80), (8.24, 288, 124, 80), (8.44, 243, 102, 80), (8.56, 243, 102, 80), (8.8, 202, 107, 80), (8.84, 164, 27, 104), (9.0, 164, 27, 104), (9.12, 121, 9, 104), (9.28, 77, 33, 104), (9.32, 52, 23, 104), (9.48, 52, 23, 104), (9.64, 33, 46, 104), (9.8, 93, 49, 104), (9.92, 93, 49, 104), (10.16, 173, 19, 104), (10.2, 226, 173, 48), (10.36, 226, 173, 48), (10.48, 211, 172, 48), (10.64, 208, 162, 48), (10.92, 220, 171, 48)]

lis1 = [(0.0, 113, 167, 47), (0.32, 138, 159, 47), (0.44, 152, 144, 47), (0.48, 193, 148, 47), (0.6, 193, 148, 47), (0.76, 205, 138, 55), (0.88, 204, 121, 63), (0.92, 190, 31, 127), (1.2, 183, 59, 127), (1.4, 137, 22, 127), (1.52, 137, 22, 127), (1.72, 129, 67, 127), (1.88, 123, 69, 127), (2.04, 131, 123, 63), (2.24, 130, 148, 63), (2.48, 130, 148, 63), (2.8, 138, 180, 63), (3.0, 138, 180, 63), (3.2, 146, 192, 63), (3.28, 105, 91, 151), (3.44, 105, 91, 151), (3.72, 11, 48, 151), (3.96, 5, 78, 151), (4.32, 4, 134, 1), (4.6, 149, 184, 48), (4.8, 145, 188, 48), (5.0, 154, 217, 48), (5.08, 163, 199, 48), (5.2, 163, 199, 48), (5.32, 164, 187, 48), (5.48, 163, 200, 48), (5.76, 163, 200, 48), (5.96, 173, 199, 48), (6.0, 133, 172, 48), (6.04, 128, 165, 48), (6.28, 128, 165, 48), (6.4, 129, 180, 48), (6.52, 133, 166, 48), (6.64, 133, 166, 48), (6.88, 144, 183, 48), (7.0, 153, 174, 48), (7.16, 153, 174, 48), (7.24, 153, 174, 48), (7.28, 253, 65, 104), (7.64, 253, 65, 104), (7.8, 279, 116, 80), (8.0, 290, 105, 80), (8.24, 288, 124, 80), (8.44, 243, 102, 80), (8.56, 243, 102, 80), (8.8, 202, 107, 80), (8.84, 164, 27, 104), (9.0, 164, 27, 104), (9.12, 121, 9, 104), (9.28, 77, 33, 104), (9.32, 52, 23, 104), (9.48, 52, 23, 104), (9.64, 33, 46, 104), (9.8, 93, 49, 104), (9.92, 93, 49, 104), (10.16, 173, 19, 104), (10.2, 226, 173, 48), (10.36, 226, 173, 48), (10.48, 211, 172, 48), (10.64, 208, 162, 48), (10.92, 220, 171, 48)]


def posi(t):
    global last_move
    if len(lis) == 0:
        return (last_move[1], last_move[2])
    if t >= lis[0][0]:
        last_move = item = lis.pop(0)
        return (item[1], item[2])
    else:
        if len(lis) > 0:
            dura = lis[0][0] - last_move[0]
            now = t - last_move[0]
            w = (lis[0][1] - last_move[1]) * (now / dura)
            h = (lis[0][2] - last_move[2]) * (now / dura)
            # print t, last_move[1] + w, last_move[2] + h
            return (last_move[1] + w, last_move[2] + h)
        return (last_move[1], last_move[2])

def size(t):
    global last_move1
    if len(lis1) == 0:
        return (last_move1[3], last_move1[3] * 1.33)
    if t >= lis1[0][0]:
        last_move1 = item = lis1.pop(0)
        return (item[3], item[3] * 1.33)
    else:
        if len(lis) > 0:
            dura = lis1[0][0] - last_move1[0]
            now = t - last_move1[0]
            s = (lis1[0][3] - last_move1[3]) * (now / dura)
            nsw = last_move1[3] + s
            nsh = nsw * 1.33
            # print t, nsw, nsh
            return (nsw, nsh)
        return (last_move1[3], last_move1[3] * 1.33)


avatar = VideoFileClip("new.mp4", has_mask=True)
maskclip = ImageClip("img.png", ismask=True, transparent=True)
tt = VideoFileClip("d.mp4")
final = CompositeVideoClip([tt, avatar.set_position(posi).set_mask(maskclip).resize(size)])
final.duration = tt.duration
final.write_videofile('nt.mp4', fps=24)

bluedazzle added a commit to bluedazzle/moviepy that referenced this issue Oct 15, 2016
fix resize video when time changed trigger a error
@bluedazzle
Copy link
Contributor Author

I write a function to check if img and mask have same shape, and will fill img until img have same shape with mask. this will solve the problem but not perfect.

add function fill_array(pre_array, shape=(0, 0)) in file moviepy/video/VideoClip.py:

    ....
    def fill_array(self, pre_array, shape=(0, 0)):
        pre_shape = pre_array.shape
        dx = shape[0] - pre_shape[0]
        dy = shape[1] - pre_shape[1]
        post_array = pre_array
        if dx < 0:
            post_array = pre_array[:shape[0]]
        elif dx > 0:
            x_1 = [[[1, 1, 1]] * pre_shape[1]] * dx
            post_array = np.vstack((pre_array, x_1))
        if dy < 0:
            post_array = post_array[:, :shape[1]]
        elif dy > 0:
            x_1 = [[[1, 1, 1]] * dy] * post_array.shape[0]
            post_array = np.hstack((post_array, x_1))
        return post_array


    def blit_on(self, picture, t):
        """
        Returns the result of the blit of the clip's frame at time `t`
        on the given `picture`, the position of the clip being given
        by the clip's ``pos`` attribute. Meant for compositing.
        """

        hf, wf = framesize = picture.shape[:2]

        if self.ismask and picture.max() != 0:
            return np.minimum(1, picture + self.blit_on(np.zeros(framesize), t))

        ct = t - self.start  # clip time

        # GET IMAGE AND MASK IF ANY

        img = self.get_frame(ct)
        mask = (None if (self.mask is None) else
                self.mask.get_frame(ct))
        if mask is not None:
            if (img.shape[0] != mask.shape[0]) or (img.shape[1] != mask.shape[1]):
                img = self.fill_array(img, mask.shape)
        hi, wi = img.shape[:2]
        ....

@ghost ghost mentioned this issue Apr 11, 2017
ghost pushed a commit that referenced this issue Apr 11, 2017
fix resize video when time changed trigger a error
ghost pushed a commit that referenced this issue Apr 11, 2017
@keikoro
Copy link
Collaborator

keikoro commented Nov 25, 2017

Looks like a fix for this was merged. Closing.

@keikoro keikoro closed this as completed Nov 25, 2017
ODtian added a commit to ODtian/moviepy that referenced this issue Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants