moviepy音视频剪辑:TextClip不支持中文字符以及OSError: magick.exe: unable to read font 仿宋_GB2312.ttf的解决办法
☞ ░ 前往老猿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的解决办法的更多相关文章
- PyQt+moviepy音视频剪辑实战文章目录
☞ ░ 前往老猿Python博文目录 ░ 本专栏为moviepy音视频剪辑合成相关内容介绍的免费专栏,对应的收费专栏为<moviepy音视频开发专栏>. 一.moviepy基础能力系统介绍 ...
- moviepy音视频剪辑:TextClip.list(font)和search搜索字体报错UnicodeDecodeError:utf-8 codec cannott decode byte 问题
☞ ░ 前往老猿Python博文目录 ░ 在moviepy2.0.0.Dev版本中,执行如下语句: from moviepy.editor import * TextClip.search('gb', ...
- moviepy音视频剪辑:视频基类VideoClip子类DataVideoClip、UpdatedVideoClip、ImageClip、ColorClip、TextClip类详解
☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑相关类及关系>介绍了剪辑相关类及关系,其中VideoClip有多个直接子类和间接子类 ...
- moviepy音视频剪辑:headblur的参数r_blur卷积核以及fx、fy、r_zone的功能作用及用途
☞ ░ 前往老猿Python博文目录 ░ 在moviepy1.03版本中,headblur的调用语法为:headblurbak(clip,fx,fy,r_zone,r_blur=None) 其中参数f ...
- moviepy音视频剪辑:视频基类VideoClip子类VideoFileClip、CompositeVideoClip、ImageSequenceClip介绍
☞ ░ 前往老猿Python博文目录 ░ 一.引言 在<moviepy音视频剪辑:moviepy中的剪辑相关类及关系>介绍了VideoClip主要有六个直接子类(VideoFileClip ...
- moviepy音视频剪辑:视频剪辑基类VideoClip的属性及方法详解
☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>和<moviepy音视频剪辑:moviepy中的剪辑基类Cl ...
- moviepy音视频剪辑:moviepy中的剪辑基类Clip的属性和方法详解
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...
- 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 ...
- moviepy音视频剪辑:视频变换处理与内容相关的变换函数headblur、mask_and/or、mirror_x/y、rotate、painting、scroll介绍
一.引言 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>介绍了剪辑基类的fl.fl_time.fx方法,在<moviepy音视频剪辑:视频剪辑基类VideoC ...
随机推荐
- SAP S/4HANA 2020安装实录
欢迎关注微信公众号:sap_gui (ERP咨询顾问之家) 今天开始试着安装SAP S/4HANA 2020版本,也是目前SAP ERP最高的版本,总安装文件大小大概50GB,数据库版本必须是HANA ...
- 9、Django之模型层第四篇:进阶操作
一 QuerySet对象 1.1可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. Entry.objects.all()[:5] # ...
- Python列表排序方法汇总,超详细!
1. 修改原列表,不创建新列表的排序 1 a = [3, 2, 8, 4, 6] 2 print(id(a)) # 2180873605704 3 a.sort() # 默认升序 4 print(a) ...
- OpenCV计算机视觉学习(11)——图像空间几何变换(图像缩放,图像旋转,图像翻转,图像平移,仿射变换,镜像变换)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 图像 ...
- Redis系列文章-数据结构篇
Redis系列文章 前言: 工作原因,在学习mybatis知识后,2个月没有补充新的知识了,最近拿起书本开始学习.打算写下这个Redis系列的文章. 目录结构如下: Redis内置数据结构 Redis ...
- linux 内存泄露 valgrind
内泄漏工具 valgrind: https://linux.die.net/man/1/valgrind www.valgrind.org/docs/manual/index.html www.val ...
- select实现超时(套接字IO超时设置)
实现超时的三种方式: 1.SIGALARM信号 void handler(int sig) { return 0; } signal(SIGALRM,handler); alarm(5); int ...
- MyBatis 使用手册
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作.MyBatis 可以通过简单的 XM ...
- 每天一个linux命令之top
每天一个linux命令之top 转 https://www.linuxprobe.com/chapter-02.html 在图2-6中,top命令执行结果的前5行为系统整体的统计信息,其所代表的含义 ...
- kali 系列学习07-攻击之密码生成
比较理想的字典是拖库字典,比如CSDN字典,如果要生成字典,可以使用Crunch 和 rtgen 两个工具, 一.密码生成 1.Crunch (1)启动crunch命令.执行命令如下所示. #crun ...

