☞ ░ 前往老猿Python博文目录

一、引言

moviepy对中文和多语言环境的支持做得并不好,包括中文文件名以及用于显示文字的TextClip就是典型的中文支持方面存在问题的。对于编解码的问题,可以通过修改解码语句中的编码类型为’asn’或‘cp936’等方式解决,对于文字显示的处理,则稍微麻烦点。

二、使用TextClip对中英文混合内容进行滚动显示

本案例为win7 64位中文操作系统+Python3.72+Moviepy2.0.0dev1环境实现,其他环境老猿没测试过。

2.1、案例代码

将指定混合中英文内容的文本转成TextClip后与一个视频合成后输出,具体代码如下:

if __name__=='__main__':
#用一位老同学写的短诗的中英文作为TextClip显示内容,由于内容过长需要滚动显示
inf = """致敬奋战在一线的巾帼女英雄 你也是孩子的妈妈, 你也是爸妈的孩子。 但你说只要穿上白大褂, 我就是一名医护人员, 这就是我的职责。 我看不清你的长相, 在层层的防护服里, 都是一颗颗金子般的心。 “青山一道同云雨,明月何曾是两乡” 引用自王昌龄的《送柴侍御》, 为在抗击疫情一线的女性“逆行者”致敬。 中国加油!武汉加油! 武汉人民感谢所有白衣天使! Pay tribute to the heroine fighting in the front line
Yao Junfeng, Wuhan University You are also the mother of the child,
You're also a parent's child.
But you said just put on the white coat,
I'm a healthcare worker,
This is my duty.
I can't see what you look like,
In layers of protective clothing,
Every heart is like gold.
"The green mountains are the same as the clouds and the rain. How could the bright moon be the two villages?"
It is quoted from Wang Changling's "See Mr. Chai Off", To pay tribute to the female "reverse" in the front line of fighting the epidemic. Go China! Come on, Wuhan! Wuhan people thank all angels in white! """
clip = VideoFileClip(r"F:\video\WinBasedWorkHard_src.mp4", audio=False).crop(0, 300, 540, 840).subclip(0, 0.05)
txtclip = TextClip(inf, font='仿宋_GB2312', fontsize=18, color='blue', bg_color='white', transparent=True).set_duration(30).resize((clip.size[0], clip.size[1] * 2)).set_fps(clip.fps).set_start(clip.end) w = None
h = clip.size[1]
x_speed = x_start = y_start 0
y_speed = 20
txtclip = txtclip.fx(vfx.scroll, w, h, x_speed, y_speed, x_start, y_start) # .set_start(clip.end) newclip = CompositeVideoClip([txtclip, clip], bg_color=(255, 255, 255), ismask=False)
newclip.write_videofile(r"F:\video\WinBasedWorkHard_scroll.mp4", threads=8)

2.2、输出剪辑播放截图



可以看到中文部分一个字符也没有,只有几个标点符号,下面的英文全部正常显示。

2.3、解决办法

找到对应中文字库,将其字库文件拷贝到代码所在目录,将TextClip的参数font不用字体名,而是用字体文件名。

2.3.1、找到字库文件名

通过资源管理器,打开C:\Windows\Fonts目录,可以看到所有字体名和对应文件,截图如下:

可以看到字体’仿宋_GB2312’ 对应字体文件为“仿宋_GB2312.ttf”。

2.3.3、将字库文件拷贝到代码工作目录
2.3.4、修改代码并执行
txtclip = TextClip(inf, font='仿宋_GB2312', fontsize=18, color='blue', bg_color='white', transparent=True).set_duration(30).resize((clip.size[0], clip.size[1] * 2)).set_fps(clip.fps)

这行代码的font参数内容替换为:

txtclip = TextClip(inf, font='仿宋_GB2312.ttf', fontsize=18, color='blue', bg_color='white', transparent=True).set_duration(30).resize((clip.size[0], clip.size[1] * 2)).set_fps(clip.fps)

执行后报错,报错信息信息如下:

Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\moviepy\video\VideoClip.py", line 1248, in __init__
subprocess_call(cmd, logger=None)
File "C:\Program Files\Python37\lib\site-packages\moviepy\tools.py", line 43, in subprocess_call
raise IOError(err.decode("utf8"))
OSError: magick.exe: unable to read font `仿宋_GB2312.ttf' @ error/annotate.c/RenderFreetype/1382. During handling of the above exception, another exception occurred: Traceback (most recent call last):
File "F:/study/python/project/moviepyTest/moviepyTest.py", line 64, in <module>
txtclip = TextClip(inf, font='仿宋_GB2312.ttf', fontsize=18, color='blue', bg_color='white', transparent=True).set_duration(30).resize((clip.size[0], clip.size[1] * 2)).set_fps(clip.fps)
File "<decorator-gen-85>", line 2, in __init__
File "C:\Program Files\Python37\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:\Program Files\Python37\lib\site-packages\moviepy\video\VideoClip.py", line 1258, in __init__
raise IOError(error)
OSError: MoviePy Error: creation of None failed because of the following error: magick.exe: unable to read font `仿宋_GB2312.ttf' @ error/annotate.c/RenderFreetype/1382.
. .This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary. Check the documentation. Process finished with exit code 1

这是因为字体文件名为中文名导致的,很简单将拷贝过去的字体文件改一下名即可,老猿将其改为:fs_GB2312.ttf,同时代码中font参数也修改为对应文件名即可。最后完整代码如下:

from  moviepy.editor import *

if __name__=='__main__':
inf = """致敬奋战在一线的巾帼女英雄 你也是孩子的妈妈, 你也是爸妈的孩子。 但你说只要穿上白大褂, 我就是一名医护人员, 这就是我的职责。 我看不清你的长相, 在层层的防护服里, 都是一颗颗金子般的心。 “青山一道同云雨,明月何曾是两乡” 引用自王昌龄的《送柴侍御》, 为在抗击疫情一线的女性“逆行者”致敬。 中国加油!武汉加油! 武汉人民感谢所有白衣天使! Pay tribute to the heroine fighting in the front line You are also the mother of the child,
You're also a parent's child.
But you said just put on the white coat,
I'm a healthcare worker,
This is my duty.
I can't see what you look like,
In layers of protective clothing,
Every heart is like gold.
"The green mountains are the same as the clouds and the rain. How could the bright moon be the two villages?"
It is quoted from Wang Changling's "See Mr. Chai Off", To pay tribute to the female "reverse" in the front line of fighting the epidemic. Go China! Come on, Wuhan! Wuhan people thank all angels in white! """
ret = TextClip.search('gb','font')
print(ret) clip = VideoFileClip(r"F:\video\WinBasedWorkHard_src.mp4", audio=False).crop(0, 300, 540, 840).subclip(0, 0.5)
txtclip = TextClip(inf, font='fs_GB2312.ttf', fontsize=18, color='blue', bg_color='white', transparent=True).set_duration(30).resize((clip.size[0], clip.size[1] * 2)).set_fps(clip.fps) w = None
h = clip.size[1]
x_speed = x_start = y_start = 0
y_speed = 20
txtclip = txtclip.fx(vfx.scroll, w, h, x_speed, y_speed, x_start, y_start).set_start(clip.end) newclip = CompositeVideoClip([txtclip, clip], bg_color=(255, 255, 255), ismask=False)
newclip.write_videofile(r"F:\video\WinBasedWorkHard_scroll.mp4", threads=8)

此时再播放文件可以看到中英文都正常显示:

更多moviepy的介绍请参考《PyQt+moviepy音视频剪辑实战文章目录》或《moviepy音视频开发专栏》。

关于收费专栏

老猿的付费专栏《使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,付费专栏《moviepy音视频开发专栏》详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,两个专栏加起来只需要19.9元,都适合有一定Python基础但无相关专利知识的小白读者学习。这2个收费专栏都有对应免费专栏,只是收费专栏的文章介绍更具体、内容更深入、案例更多。

对于缺乏Python基础的同仁,可以通过老猿的免费专栏《专栏:Python基础教程目录》从零开始学习Python。

如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。

跟老猿学Python、学5G!

☞ ░ 前往老猿Python博文目录

moviepy音视频剪辑:TextClip不支持中文字符以及OSError: magick.exe: unable to read font 仿宋_GB2312.ttf的解决办法的更多相关文章

  1. PyQt+moviepy音视频剪辑实战文章目录

    ☞ ░ 前往老猿Python博文目录 ░ 本专栏为moviepy音视频剪辑合成相关内容介绍的免费专栏,对应的收费专栏为<moviepy音视频开发专栏>. 一.moviepy基础能力系统介绍 ...

  2. moviepy音视频剪辑:TextClip.list(font)和search搜索字体报错UnicodeDecodeError:utf-8 codec cannott decode byte 问题

    ☞ ░ 前往老猿Python博文目录 ░ 在moviepy2.0.0.Dev版本中,执行如下语句: from moviepy.editor import * TextClip.search('gb', ...

  3. moviepy音视频剪辑:视频基类VideoClip子类DataVideoClip、UpdatedVideoClip、ImageClip、ColorClip、TextClip类详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑相关类及关系>介绍了剪辑相关类及关系,其中VideoClip有多个直接子类和间接子类 ...

  4. moviepy音视频剪辑:headblur的参数r_blur卷积核以及fx、fy、r_zone的功能作用及用途

    ☞ ░ 前往老猿Python博文目录 ░ 在moviepy1.03版本中,headblur的调用语法为:headblurbak(clip,fx,fy,r_zone,r_blur=None) 其中参数f ...

  5. moviepy音视频剪辑:视频基类VideoClip子类VideoFileClip、CompositeVideoClip、ImageSequenceClip介绍

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 在<moviepy音视频剪辑:moviepy中的剪辑相关类及关系>介绍了VideoClip主要有六个直接子类(VideoFileClip ...

  6. moviepy音视频剪辑:视频剪辑基类VideoClip的属性及方法详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>和<moviepy音视频剪辑:moviepy中的剪辑基类Cl ...

  7. moviepy音视频剪辑:moviepy中的剪辑基类Clip的属性和方法详解

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  8. moviepy音视频剪辑:AudioClip帧处理时报TypeError: only size-1 arrays can be converted to Python scalar错

    ☞ ░ 前往老猿Python博文目录 ░ 一.环境 操作系统:win7 64位 moviepy:1.0.3 numpy:1.19.0 Python:3.7.2 二.应用代码及报错信息 程序代码 if ...

  9. moviepy音视频剪辑:视频变换处理与内容相关的变换函数headblur、mask_and/or、mirror_x/y、rotate、painting、scroll介绍

    一.引言 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>介绍了剪辑基类的fl.fl_time.fx方法,在<moviepy音视频剪辑:视频剪辑基类VideoC ...

随机推荐

  1. 多服务器使用Docker设置一主一从三哨兵redis(完整)

    本来应该续之前那篇博客Docker配置redis哨兵模式--多服务器·上写一个下篇的,但是忽然意识到应该将必要的环境打包为一个基础镜像,在此基础上建立与redis有关的镜像,这样既能够快速打包,又能够 ...

  2. 力扣 122 买卖股票的最佳时机II

    力扣 122 买卖股票的最佳时机II 思路: 动态规划,表面上是\(O(2^n)\)的搜索空间,实际上该天的选择只与前一天的状态(是否持有股票)有关.从收益的角度来看,确实每一天的不同选择都会产生不同 ...

  3. 庐山真面目之三微服务架构Consul版本实现

    庐山真面目之三微服务架构Consul版本实现 一.简介           在上一篇文章<庐山真面目之二微服务架构NGINX版本实现>中,我们已经探讨了如何搭建基于Nginx 网关的微服务 ...

  4. layui下拉框后台动态赋值

    前台页面: <select name="xm" id="xm" lay-verify="required" lay-filter=&q ...

  5. 经典c程序100例==41--50

    [程序41] 题目:学习static定义静态变量的用法 1.程序分析: 2.程序源代码: #include "stdio.h" varfunc() { int var=0; sta ...

  6. 【JVM第四篇--运行时数据区】堆

    写在前面的话:本文是在观看尚硅谷JVM教程后,整理的学习笔记.其观看地址如下:尚硅谷2020最新版宋红康JVM教程 一.堆的概述 JVM的运行时数据区如下: 一个Java程序运行起来对应着一个进程(操 ...

  7. @Autowired自动装配原理

    在类中为类名添加 @Auwowired注解,为该类在spring中注册成组件 1,先按照类型在容器中找对应的组件:找到一个, 直接赋值,一个都没找到, 抛异常 2,找到了多个:按变量名作为ID继续匹配 ...

  8. 数据结构实训——哈夫曼(Huffman)编/译码器

    题目4.哈夫曼(Huffman)编/译码器(限1人完成) [问题描述] 利用哈夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本.但是,这要求在发送端通过一个编码系统对待传数据预先 ...

  9. WireShark——ARP 协议包分析

     1. 什么是ARP ARP(Address Resolution Protocol)协议,即地址解析协议.该协议的功能就是将 IP 地 址解析成 MAC 地址. ARP(Address Resolu ...

  10. SSL加密原理

    对称加密算法 对称加密算法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密. 非对称加密算法 非对称加密算法(RSA)是内容加密的一类算法,它有两个秘钥:公钥与私钥 ...