Files
audio2motion/notebook/test_movie.ipynb
T
2022-03-14 18:13:13 +08:00

11 KiB

In [4]:
import numpy as np

from moviepy.editor import *
from moviepy.video.tools.segmenting import findObjects

# WE CREATE THE TEXT THAT IS GOING TO MOVE, WE CENTER IT.

screensize = (720,460)
txtClip = TextClip('Cool effect',color='white', font="Amiri-Bold",
                   kerning = 5, fontsize=100)
cvc = CompositeVideoClip( [txtClip.set_pos('center')],
                        size=screensize)

# THE NEXT FOUR FUNCTIONS DEFINE FOUR WAYS OF MOVING THE LETTERS


# helper function
rotMatrix = lambda a: np.array( [[np.cos(a),np.sin(a)], 
                                 [-np.sin(a),np.cos(a)]] )

def vortex(screenpos,i,nletters):
    d = lambda t : 1.0/(0.3+t**8) #damping
    a = i*np.pi/ nletters # angle of the movement
    v = rotMatrix(a).dot([-1,0])
    if i%2 : v[1] = -v[1]
    return lambda t: screenpos+400*d(t)*rotMatrix(0.5*d(t)*a).dot(v)
    
def cascade(screenpos,i,nletters):
    v = np.array([0,-1])
    d = lambda t : 1 if t<0 else abs(np.sinc(t)/(1+t**4))
    return lambda t: screenpos+v*400*d(t-0.15*i)

def arrive(screenpos,i,nletters):
    v = np.array([-1,0])
    d = lambda t : max(0, 3-3*t)
    return lambda t: screenpos-400*v*d(t-0.2*i)
    
def vortexout(screenpos,i,nletters):
    d = lambda t : max(0,t) #damping
    a = i*np.pi/ nletters # angle of the movement
    v = rotMatrix(a).dot([-1,0])
    if i%2 : v[1] = -v[1]
    return lambda t: screenpos+400*d(t-0.1*i)*rotMatrix(-0.2*d(t)*a).dot(v)



# WE USE THE PLUGIN findObjects TO LOCATE AND SEPARATE EACH LETTER

letters = findObjects(cvc) # a list of ImageClips


# WE ANIMATE THE LETTERS

def moveLetters(letters, funcpos):
    return [ letter.set_pos(funcpos(letter.screenpos,i,len(letters)))
              for i,letter in enumerate(letters)]

clips = [ CompositeVideoClip( moveLetters(letters,funcpos),
                              size = screensize).subclip(0,5)
          for funcpos in [vortex, cascade, arrive, vortexout] ]

# WE CONCATENATE EVERYTHING AND WRITE TO A FILE

final_clip = concatenate_videoclips(clips)
final_clip.write_videofile('../../coolTextEffects.avi',fps=25,codec='mpeg4')
Moviepy - Building video ../../coolTextEffects.avi.
Moviepy - Writing video ../../coolTextEffects.avi

                                                               
Moviepy - Done !
Moviepy - video ready ../../coolTextEffects.avi
In [27]:

import resampy
import random
import numpy as np
import numpy as np
from scipy import interpolate


def resample_by_nsteps(feat, nsteps_tgt):
    """ 
       feat,  (nframes, ... ) (with source_fps)
    """

    nsteps_src = feat.shape[0]
    data_idx = np.arange(0, nsteps_src,  1)
    samp_idx = np.arange(0, nsteps_src-1, (nsteps_src - 1) / nsteps_tgt)
    # print(f"data_idx = {list(data_idx)}")
    # print(f"smap_idx = {list(samp_idx)}")
    # sample
    func = interpolate.interp1d(data_idx, y=feat, axis=0)
    data_interpolate = func(samp_idx)

    return data_interpolate


# def convert_fps(feat, source_fps = 30, target_fps=25):
#     """ 
#        feat,  (nframes, ... ) (with source_fps)
#     """

#     nsize = feat.shape[0]
#     data_idx = np.arange(0, nsize, 1)
#     samp_idx = np.arange(0, nsize-1, 1.0*source_fps/target_fps)


#     print(f"1.0*source_fps/target_fps = {1.0*source_fps/target_fps}")

#     # get sample idx
#     # if source_fps > target_fps:

#     print(f"data_idx = {list(data_idx)}")
#     print(f"smap_idx = {list(samp_idx)}")

#     # sample
#     func = interpolate.interp1d(data_idx, y=feat, axis=0)
#     data_interpolate = func(samp_idx)

#     del func

#     return data_interpolate



def interpolate_feat(feat, input_rate, output_rate, nsteps_tgt=None):
    
    feat_shape = feat.shape
    feat = feat.reshape(feat_shape[0], -1)

    dim_feat = feat.shape[1]

    nsteps_src = feat.shape[0]


    seq_len = nsteps_src / float(input_rate)
    if nsteps_tgt is None:
        nsteps_tgt = int(seq_len * output_rate)

    input_timestamps = np.arange(nsteps_src) / float(input_rate)
    output_timestamps = np.arange(nsteps_tgt) / float(output_rate)
    output_feat = np.zeros((nsteps_tgt, dim_feat))

    for feat in range(dim_feat):
        output_feat[:, feat] = np.interp(output_timestamps,
                                             input_timestamps,
                                             feat[:, feat])
    return output_feat
In [2]:

data.shape
Out [2]:
(150, 1024)
In [35]:
data = np.random.random(size=(150, 1024))
nsteps = data.shape[0]

speed = random.sample({0.5, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3, 1.4, 1.5},1)[0]
nsteps_speed = int(nsteps * speed)

data = np.random.random(size=(nsteps_speed, 1024))

print(f"nsteps = {nsteps}, random speed = {speed}, nsteps_speed = {nsteps_speed}")

data_convert = resample_by_nsteps(data, nsteps_tgt=nsteps)

print(f"data shape = {data.shape}, data_convert = {data_convert.shape}, nstep_speed={nsteps_speed}")

nsteps = 150, random speed = 0.9, nsteps_speed = 135
data_idx = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134]
smap_idx = [0.0, 0.8933333333333333, 1.7866666666666666, 2.6799999999999997, 3.5733333333333333, 4.466666666666667, 5.359999999999999, 6.253333333333333, 7.1466666666666665, 8.04, 8.933333333333334, 9.826666666666666, 10.719999999999999, 11.613333333333333, 12.506666666666666, 13.4, 14.293333333333333, 15.186666666666666, 16.08, 16.973333333333333, 17.866666666666667, 18.759999999999998, 19.653333333333332, 20.546666666666667, 21.439999999999998, 22.333333333333332, 23.226666666666667, 24.12, 25.013333333333332, 25.906666666666666, 26.8, 27.69333333333333, 28.586666666666666, 29.48, 30.37333333333333, 31.266666666666666, 32.16, 33.053333333333335, 33.946666666666665, 34.839999999999996, 35.733333333333334, 36.626666666666665, 37.519999999999996, 38.413333333333334, 39.306666666666665, 40.199999999999996, 41.093333333333334, 41.986666666666665, 42.879999999999995, 43.77333333333333, 44.666666666666664, 45.56, 46.45333333333333, 47.346666666666664, 48.24, 49.13333333333333, 50.026666666666664, 50.92, 51.81333333333333, 52.70666666666666, 53.6, 54.49333333333333, 55.38666666666666, 56.28, 57.17333333333333, 58.06666666666666, 58.96, 59.85333333333333, 60.74666666666666, 61.64, 62.53333333333333, 63.42666666666666, 64.32, 65.21333333333334, 66.10666666666667, 67.0, 67.89333333333333, 68.78666666666666, 69.67999999999999, 70.57333333333334, 71.46666666666667, 72.36, 73.25333333333333, 74.14666666666666, 75.03999999999999, 75.93333333333334, 76.82666666666667, 77.72, 78.61333333333333, 79.50666666666666, 80.39999999999999, 81.29333333333334, 82.18666666666667, 83.08, 83.97333333333333, 84.86666666666666, 85.75999999999999, 86.65333333333334, 87.54666666666667, 88.44, 89.33333333333333, 90.22666666666666, 91.12, 92.01333333333334, 92.90666666666667, 93.8, 94.69333333333333, 95.58666666666666, 96.48, 97.37333333333333, 98.26666666666667, 99.16, 100.05333333333333, 100.94666666666666, 101.84, 102.73333333333333, 103.62666666666667, 104.52, 105.41333333333333, 106.30666666666666, 107.2, 108.09333333333333, 108.98666666666666, 109.88, 110.77333333333333, 111.66666666666666, 112.56, 113.45333333333333, 114.34666666666666, 115.24, 116.13333333333333, 117.02666666666667, 117.92, 118.81333333333333, 119.70666666666666, 120.6, 121.49333333333333, 122.38666666666667, 123.28, 124.17333333333333, 125.06666666666666, 125.96, 126.85333333333332, 127.74666666666667, 128.64, 129.53333333333333, 130.42666666666668, 131.32, 132.21333333333334, 133.10666666666665]
data shape = (135, 1024), data_convert = (150, 1024), nstep_speed=135
In [ ]: