0

我试图在 Python 的 moviepy 模块的帮助下加速和/或减速影片剪辑,但我似乎无法正常工作。该脚本运行非常顺利,没有任何错误,但我没有看到任何效果。可能是我的脚本错误,我无法检测到问题。寻求您的帮助/提示。我不需要完整的解决方案,任何提示都会有很大帮助。我一直在研究这个解决方案一段时间,我想我应该在这里发布我的问题。任何帮助、提示、指导将不胜感激。谢谢你。

from moviepy.editor import *
from moviepy.video.tools.drawing import color_split
import os

dir = os.path.split(os.path.realpath(__file__))[0]
img = os.path.join('tmp', 'stock.jpg')
folder = 'tmp'


def f_accel_decel(t, old_d, new_d, abruptness=1, soonness=1.0):
    """
    abruptness
      negative abruptness (>-1): speed up down up
      zero abruptness : no effect
      positive abruptness: speed down up down

    soonness
      for positive abruptness, determines how soon the
      speedup occurs (0<soonness < inf)
    """

    a = 1.0+abruptness
    def _f(t):
        f1 = lambda t: (0.5)**(1-a)*(t**a)
        f2 = lambda t: (1-f1(1-t))
        return (t<.5)*f1(t) + (t>=.5)*f2(t) 

    return old_d*_f((t/new_d)**soonness)

def accel_decel(clip, new_duration=None, abruptness=1.0, soonness=1.0):
    """
    new_duration
      If None, will be that of the current clip.
    abruptness
      negative abruptness (>-1): speed up down up
      zero abruptness : no effect
      positive abruptness: speed down up down

    soonness
      for positive abruptness, determines how soon the
      speedup occurs (0<soonness < inf)
    """

    if new_duration is None:
        new_duration = clip.duration

    fl = lambda t : f_accel_decel(t, clip.duration, new_duration,
                                   abruptness, soonness)

    return clip.fl_time(fl).set_duration(new_duration)



duration = 30

main_clip = ImageClip(img, duration=30)
W,H = main_clip.size
print W,H



clip1 = (main_clip
             .subclip(0,duration)
             .set_pos(lambda t:(max((0), (int(W-0.5*W*t))), "center"))
             )


modifiedClip1 = accel_decel(clip1, abruptness=5, soonness=1.3)


cc = CompositeVideoClip([modifiedClip1], size=(1920,1080), bg_color=(232,54,18)).resize(0.5)
cc.preview(fps=24)
#cc.write_videofile("mask.avi", fps=25, codec="libx264", bitrate="1000K", threads=3)
4

1 回答 1

3

我认为加速和减速剪辑对象的最佳方法是使用缓动函数

一些参考网站:

这是我在尝试理解这些函数时编写的脚本的一部分。也许您可以使用其中一些概念来解决您的问题。

from __future__ import division
from moviepy.editor import TextClip, CompositeVideoClip
import math


def efunc(x0, x1, dur, func='linear', **kwargs):
    # Return an easing function.
    # It will control a single dimention of the clip movement.
    # http://www.gizma.com/easing/

    def linear(t):
        return c*t/d + b

    def out_quad(t):
        t = t/d
        return -c * t*(t-2) + b

    def in_out_sine(t):
        return -c/2 * (math.cos(math.pi*t/d) - 1) + b

    def in_quint(t):
        t = t/d
        return c*t*t*t*t*t + b

    def in_out_circ(t):
        t /= d/2;
        if t < 1:
            return -c/2 * (math.sqrt(1 - t*t) - 1) + b
        t -= 2;
        return c/2 * (math.sqrt(1 - t*t) + 1) + b;

    def out_bounce(t):
        # http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js
        t = t/d
        if t < 1/2.75:
            return c*(7.5625*t*t) + b
        elif t < 2/2.75:
            t -= 1.5/2.75
            return c*(7.5625*t*t + .75) + b
        elif t < 2.5/2.75:
            t -= 2.25/2.75
            return c*(7.5625*t*t + .9375) + b
        else:
            t -= 2.625/2.75
            return c*(7.5625*t*t + .984375) + b

    # Kept the (t, b, c, d) notation found everywhere.
    b = x0
    c = x1 - x0
    d = dur
    return locals()[func]


def particle(x0, x1, y0, y1, d, func='linear', color='black', **kwargs):
    # Dummy clip for testing.

    def pos(t):
        return efunc(x0, x1, d, func=func)(t), efunc(y0, y1, d, func=func)(t)

    return (
        TextClip('*', fontsize=80, color=color)
        .set_position(pos)
        .set_duration(d)
        )

# Make a gif to visualize the behaviour of the functions:

easing_functions = [
    ('linear', 'red'),
    ('in_out_sine', 'green'),
    ('in_out_circ', 'violet'),
    ('out_quad', 'blue'),
    ('out_bounce', 'brown'),
    ('in_quint', 'black'),
    ]

d = 4
x0, x1 = 0, 370
clips = []
for i, (func, c) in enumerate(easing_functions):
    y = 40*i
    clips.append(particle(x0, x1, y, y, d=d, func=func, color=c))
    clips.append(particle(x1, x0, y, y, d=d, func=func, color=c).set_start(d))

clip = CompositeVideoClip(clips, size=(400,250), bg_color=(255,255,255))
clip.write_gif('easing.gif', fps=12)


脚本的输出:

缓动函数演示

于 2016-10-19T16:29:18.063 回答