简体   繁体   English

在 python 中的视频文件 (.mp4) 后面添加图像

[英]Add an image behind a video file (.mp4) in python

I am attempting to add an image behind a video using moviepy and cannot come up with any way to do it.我正在尝试使用moviepy在视频后面添加图像,但无法想出任何方法来做到这一点。 My image dimension is (1080 x 720) and my video dimension is (200 x 200).我的图像尺寸是(1080 x 720),我的视频尺寸是(200 x 200)。 I am trying to put the video file on top of the image file, place it in the midlle and export the final video with the dimensions of the image.我正在尝试将视频文件放在图像文件的顶部,将其放在 midlle 并导出具有图像尺寸的最终视频。 This is a visual representation of what I am trying to achieve:这是我要实现的目标的视觉表示:

View image看图

I tried doing this with the following code, but could not manage to get it to work.我尝试使用以下代码执行此操作,但无法使其正常工作。 Any help is seriously appreciated:)非常感谢任何帮助:)

import moviepy
import moviepy.editor as mp

video = mp.VideoFileClip("/media/pi/vid.mp4")

logo = (mp.ImageClip("/media/pi/pic.png"))

# Preferably I would want the image to be the same duration as the video file. 
# The following was just for testing.
final = mp.CompositeVideoClip([video, logo.set_duration(3)])
final.write_videofile("test.mp4")

Hello!你好!

I can't test it now, but here it said that我现在无法测试它,但这里它说

Note that by default the composition has the size of its first clip (as it is generally a background)请注意,默认情况下,合成具有其第一个剪辑的大小(因为它通常是背景)

So maybe you should try this (if you don't know, what is "center", you should see docs ):所以也许你应该试试这个(如果你不知道什么是“中心”,你应该看看文档):

import moviepy
import moviepy.editor as mp

video = mp.VideoFileClip("/media/pi/vid.mp4")
logo = (mp.ImageClip("/media/pi/pic.png"))

final = mp.CompositeVideoClip([logo, video.set_position("center")])
final.write_videofile("test.mp4")

SIDE NOTE: you should downgrade from moviepy 1.0.1 to moviepy 1.0.0, otherwise you will get an "object has no attribute stdout" error or something like that.旁注:您应该从moviepy 1.0.1 降级到moviepy 1.0.0,否则您将收到“对象没有属性stdout”错误或类似的错误。 To downgrade:降级:

pip install moviepy==1.0.0 

or或者

 pip3 install moviepy = 1.0.0

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

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