简体   繁体   English

MoviePy:如何通过多个步骤在视频中移动图像?

[英]MoviePy: How to move an image across a video with multiple steps?

I want to move an image in multiple steps, where the background is a video.我想分多个步骤移动图像,其中背景是视频。 Currently this is my code:目前这是我的代码:

waterClip = ImageClip("watermark.png").set_position(lambda t: (np.exp(10*t - 10*titleTime) * -1, 75)).

Its a watermark that displays at the top left of the video, which quickly slides away to the left in a smooth fashion.它是显示在视频左上角的水印,它以流畅的方式快速向左滑动。

However, I want it to "pop" out towards the right, before sliding all the way to the left.但是,我希望它向右“弹出”,然后一直向左滑动。 So I think I would need 2 steps, the first one moving the image towards the right, and the second moving it all the way to the left (which the code above does)所以我想我需要 2 个步骤,第一个将图像向右移动,第二个将它一直向左移动(上面的代码就是这样做的)

How would I do this?我该怎么做?

Thanks!谢谢!

EDIT:编辑:

Here is all my code:这是我的所有代码:

import numpy as np
from moviepy.editor import *
from moviepy.video.fx.all import crop
import random

finalList = []
cum = 10
first = 4
backName = random.choice(os.listdir("backgroundVid/"))
backTemp = VideoFileClip("backgroundVid/" + backName).without_audio() 
# chooses a random video from a folder, all 59s long and 1280x720 60fps

if backTemp.duration >= cum:
    backTemp = backTemp.subclip(0, cum)
elif backTemp.duration < cum:
    backTemp = backTemp.loop(duration = cum)

(w, h) = backTemp.size
if (w == 720) & (h == 1280):
    back = backTemp
else:
    backCropped = crop(backTemp, width= h/(w/h), height=h, x_center=w/2, y_center=h/2)
    back = backCropped.resize((720,1280))
# resizes video to 720x1280 

finalList.append(back)


image = ImageClip('watermark.png', duration=10)
image = image.set_position(lambda t: (np.exp(10*t - 10*first) * -1, 75))
image.fps = 60
finalList.append(image)

videoTemp = CompositeVideoClip(finalList)
videoTemp.write_videofile("TEST_FinalVideoTT" + ".mp4")

And here is what it currently looks like:这是它目前的样子:

I am not sure that I got it exactly as you indented...我不确定我是否完全按照你缩进的方式得到它......

The main concept is dividing the movement to two stages:主要概念是将运动分为两个阶段:

  • Moving to the left:向左移动:

     ((np.exp(10*(t - first))*(-1)
  • Moving to the right ( most_left_col = -image.size[0] ):向右移动( most_left_col = -image.size[0] ):

     most_left_col - np.exp(10*(t-end_t))*(-1))

The lambda may be as follows: lambda可能如下:

image = image.set_position(lambda t: ((np.exp(10*(t - first))*(-1) if t < end_t else most_left_col - np.exp(10*(t-end_t))*(-1)), 100))

end_t is computed in such way that np.exp(10*(end_t - first))*(-1) = most_left_col . end_t的计算方式为np.exp(10*(end_t - first))*(-1) = most_left_col

The result is that end_t = np.log(-most_left_col)/10 + first结果是end_t = np.log(-most_left_col)/10 + first

That way when the entire watermark is out of the image, it starts moving to the other direction.这样当整个水印都在图像之外时,它就会开始向另一个方向移动。


Cone sample:锥样:

import numpy as np
from moviepy.editor import *

#ffmpeg -y -f lavfi -i color=black:size=720x1280:rate=10:duration=10 -vcodec libx264 original_video.mp4
#ffmpeg -y -f lavfi -i testsrc=size=720x1280:rate=1:duration=1 -frames 1 -update 1 watermark.png

finalList = []
first = 4

backTemp = VideoFileClip("original_video.mp4").without_audio()
(w, h) = backTemp.size

back = backTemp  # The example assumes that backTemp resolution is 720x1280, and duration is 10 seconds
finalList.append(back)

image = ImageClip('watermark.png', duration=10)
most_left_col = -image.size[0]
end_t = np.log(-most_left_col)/10 + first

image = image.set_position(lambda t: ((np.exp(10*(t - first))*(-1) if t < end_t else most_left_col - np.exp(10*(t-end_t))*(-1)), 100))
image.fps = 10  # Set to 10 fps for testing\
finalList.append(image)

videoTemp = CompositeVideoClip(finalList)
videoTemp.write_videofile("TEST_FinalVideoTT" + ".mp4")

For testing we may create original_video.mp4 and watermark.png using FFmpeg CLI:为了进行测试,我们可以使用 FFmpeg CLI 创建original_video.mp4watermark.png

ffmpeg -y -f lavfi -i color=black:size=720x1280:rate=10:duration=10 -vcodec libx264 original_video.mp4
ffmpeg -y -f lavfi -i testsrc=size=720x1280:rate=1:duration=1 -frames 1 -update 1 watermark.png

Sample output:样本 output:
在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM