使用python在视频上渲染文本

In this post, I will show you how to add text to your videos using Python. It may sound very difficult at first glance, but trust me it’s much easier than you think, and also it’s cool. This will be a simple exercise where we will be able to see how machine learning can be used in real life. If you check my other articles, you will see that I like to work on hands-on projects. I think this is the best way to practice our coding skills and improve ourselves. You will find a handful of project ideas online when you do a little research. But I highly recommend doing your own brainstorming and writing down your personal project ideas, then start working on how you can make them happen. Let’s get back to our project.

在这篇文章中,我将向您展示如何使用Python向视频中添加文本。 乍一看听起来很困难,但请相信我,它比您想象的要容易得多,而且很酷。 这将是一个简单的练习,我们将能够看到如何在现实生活中使用机器学习。 如果查看其他文章,您会发现我喜欢动手做项目。 我认为这是练习我们的编码技能和提高自我的最好方法。 进行一些研究后,您会在网上找到一些项目构想。 但是,我强烈建议您进行头脑风暴,写下您的个人项目构想,然后着手研究如何实现它们。 让我们回到我们的项目。

目录 (Table of Contents)

  • Getting Started

    入门

  • Step 1: Import Library

    步骤1:导入库

  • Step 2: Define your video

    步骤2:定义影片

  • Step 3: Create your text

    步骤3:建立文字

  • Final Step: Rendering Text on Video

    最后一步:在视频上渲染文本

入门 (Getting Started)

As you can understand from the title, for this project we will need a video recording file. It can even be a recording of yourself speaking to the camera. Using a library called MoviePy, we will add text to our video recording.

从标题可以理解,对于这个项目,我们将需要一个视频记录文件。 它甚至可以记录您自己对着摄像机讲话。 使用名为MoviePy的库我们将文本添加到视频记录中。

First, we will import the libraries, I will show you how to install them. In the second step, we will define the video we will be using for the project, it can be a short video. In the third step, we will define a text variable, it will be the text that will be added to the video. And finally, we will render the text on the video recording. If you are ready, let’s get started by installing the libraries!

首先,我们将导入库,我将向您展示如何安装它们。 在第二步中,我们将定义将用于项目的视频,它可以是简短的视频。 在第三步中,我们将定义一个文本变量,它将是要添加到视频中的文本。 最后,我们将在录像中渲染文本。 如果您准备好了,就让我们开始安装这些库!

图书馆 (Libraries)

We are going to use one library for this project, but to make the text rendering we will also need to install another module. The library is called MoviePy, and here how to install it:

我们将为该项目使用一个库,但是要进行文本渲染,我们还需要安装另一个模块。 该库称为MoviePy,以及如何安装它:

pip install moviepy

You can learn more about moviepy from its official documentation page.

您可以从其官方文档页面了解有关moviepy的更多信息。

And the second module that we need to install is called ImageMagick. Without this module, you may get an error while rendering text on to the video recording. To install ImageMagick, I used Homebrew which is a package manager for macOs and Linux devices. You can search online “how to install ImageMagick” if you are using a different operating system.

我们需要安装的第二个模块称为ImageMagick。 如果没有此模块,则在将文本呈现到视频记录时可能会出错。 要安装ImageMagick,我使用Homebrew,它是macO和Linux设备的软件包管理器。 如果使用其他操作系统,则可以在线搜索“如何安装ImageMagick”。

brew install imagemagick

You can learn more about imagemagick from its official documentation page.

您可以从其官方文档页面了解有关imagemagick的更多信息。

MoviePy is a library that can read and write all the most common audio and video formats, including GIF. If you are having issues when installing MoviePy library, try by installing ffmpeg. Ffmpeg is a leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter, and play pretty much anything that humans and machines have created.

MoviePy是一个库,可以读取和写入所有最常见的音频和视频格式,包括GIF。 如果在安装MoviePy库时遇到问题,请尝试安装ffmpeg。 Ffmpeg是领先的多媒体框架,能够解码,编码,转码,mux,demux,流,过滤和播放人类和机器创造的几乎所有东西。

Now, we should get to writing code in our code editor. We will start by importing the libraries.

现在,我们应该在代码编辑器中编写代码。 我们将从导入库开始。

第1步-导入库 (Step 1 — Import Library)

import moviepy.editor as mp

Yes, that’s all we need to get the task done. Without losing any time let’s move to the next step.

是的,这就是我们完成任务所需要的。 在不浪费时间的情况下,让我们继续下一步。

第2步-定义您的视频 (Step 2 — Define your video)

In this step, we will define a variable called “my_video”. Then using a special method by moviepy library, we will define our video file. Also, I want to keep the audio of the video, that’s why the value for the video is True.

在此步骤中,我们将定义一个名为“ my_video”的变量。 然后,通过moviepy库使用特殊方法,我们将定义视频文件。 另外,我想保留视频的音频,这就是为什么视频的值为True。

my_video = mp.VideoFileClip(“data/video_test.mov”, audio=True)

By the way, there are many video formats, some of them can be listed as:

顺便说一下,视频格式很多,其中一些可以列出为:

  • MP4 (mp4, m4a, m4v, f4v, f4a, m4b, m4r, f4b, mov)

    MP4(mp4,m4a,m4v,f4v,f4a,m4b,m4r,f4b,mov)
  • 3GP (3gp, 3gp2, 3g2, 3gpp, 3gpp2)

    3GP(3gp,3gp2、3g2、3gpp,3gpp2)
  • OGG (ogg, oga, ogv, ogx)

    OGG(ogg,oga,ogv,ogx)
  • WMV (wmv, wma, asf*)

    WMV(WMV,WMA,ASF *)

Make sure your path is correct, and your video format is working properly. If you are having issues in this step, try converting your video to different formats.

确保您的路径正确,并且视频格式工作正常。 如果您在此步骤中遇到问题,请尝试将视频转换为其他格式。

Before we move to the next step, let’s define the width and the height of our video. We will need these when rendering the text into the video. Don’t worry, this will be very easy.

在进行下一步之前,让我们定义视频的宽度和高度。 在将文本渲染到视频中时,我们将需要这些。 不用担心,这将非常容易。

w,h = moviesize = my_video.size

步骤3 —创建您的文本 (Step 3 — Create your text)

In this step, we will define a new variable for our text. I called it “my_text”. And using the TextClip method we will be able to create the text.

在这一步中,我们将为文本定义一个新变量。 我称它为“ my_text”。 使用TextClip方法,我们将能够创建文本。

my_text = mp.TextClip(“The Art of Adding Text on Video”, font=’Amiri-regular’, color=’white’, fontsize=34)

As you can see, we can even play with the parameters. It’s really cool that we can customize our text. Text font, text color and font size can be changed to your preferences.

如您所见,我们甚至可以使用参数。 我们可以自定义文本,这真的很酷。 文本字体,文本颜色和字体大小可以更改为您的首选项。

Now, we will locate the text on our video. And also we can design a background for our text. You can play with the parameters and see how it works. Background color, background opacity, and position of the text are some of the parameters.

现在,我们将在视频中找到文字。 我们还可以为文本设计背景。 您可以使用参数并查看其工作原理。 背景颜色,背景不透明度和文本位置是其中的一些参数。

txt_col = my_text.on_color(size=(my_video.w + my_text.w, my_text.h+5), color=(0,0,0), pos=(6,’center’), col_opacity=0.6)

最后一步-在视频上渲染文本 (Final Step — Rendering text on video)

Great! You made it until the final step. This step is where the magic happens.

大! 您做到了直到最后一步。 这是魔术发生的地方。

动画 (Animation)

First, we will define a new variable called “text_mov” where we can add some animation. The movement of the text over the video. You can find your favorite result by playing with the parameters. Here is the line of code where we do the animation:

首先,我们将定义一个名为“ text_mov”的新变量,在其中可以添加一些动画。 文字在视频上的移动。 您可以通过使用参数找到自己喜欢的结果。 这是我们制作动画的代码行:

txt_mov = txt_col.set_pos( lambda t: (max(w/30,int(w-0.5*w*t)),max(5*h/6,int(100*t))) )

渲染图 (Rendering)

Finally, it’s time to render the text on our video. Thanks to the moviepy library, we will use a method called “CompositeVideoClip” which will do the rendering for us. As mentioned earlier, if you are getting errors in this line I highly recommend installing imageMagick and ffmpeg modules. Here is the line to make the magic happen:

最后,是时候在我们的视频上渲染文本了。 多亏了moviepy库,我们将使用一种名为“ CompositeVideoClip”的方法来为我们进行渲染。 如前所述,如果您在此行中遇到错误,我强烈建议您安装imageMagickffmpeg模块。 这是使魔术发生的线:

final = mp.CompositeVideoClip([my_video,txt_mov])

导出结果 (Export your result)

Almost there, to see how the final result looks, let’s export the video. We will save it as a new video file. What’s really cool about exporting is you can adjust the video format and even the frames per second. Here is the code to export the first 5 seconds of the rendered video:

快到那里了,看看最终结果如何,让我们导出视频。 我们将其另存为新的视频文件。 导出真正酷的是,您可以调整视频格式,甚至每秒调整帧数。 这是导出渲染的视频的前5秒的代码:

final.subclip(0,17).write_videofile(“data/text_add_result.mov”,fps=24,codec=’libx264')

分享结果 (Sharing the result)

Congrats! You have created a program that renders a custom text on a video without using any video editing software. It is really cool to see how machine learning can be used in our daily life. Hoping that you enjoyed reading this post and working on the project. I would be glad if you learned something new today. Working on hands-on programming projects like this one is the best way to sharpen your coding skills.

恭喜! 您已经创建了一个程序,可以在不使用任何视频编辑软件的情况下在视频上呈现自定义文本。 看看如何在我们的日常生活中使用机器学习真的很棒。 希望您喜欢阅读这篇文章并从事该项目。 如果您今天学到一些新知识,我将很高兴。 从事这样的动手编程项目是提高您的编码技能的最佳方法。

Feel free to contact me if you have any questions while implementing the code.

实施代码时如有任何疑问,请随时与我联系

Follow my blog and Towards Data Science to stay inspired. Thank you,

关注我的博客迈向数据科学 ,保持灵感。 谢谢,

相关内容 (Related Content)

翻译自: https://towardsdatascience.com/rendering-text-on-video-using-python-1c006519c0aa

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值