☞ ░ 前往老猿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:videoWinBasedWorkHard_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:videoWinBasedWorkHard_scroll.mp4", threads=8)
2.2、输出剪辑播放截图
可以看到中文部分一个字符也没有,只有几个标点符号,下面的英文全部正常显示。
2.3、解决办法
找到对应中文字库,将其字库文件拷贝到代码所在目录,将TextClip的参数font不用字体名,而是用字体文件名。
2.3.1、找到字库文件名
通过资源管理器,打开C:WindowsFonts
目录,可以看到所有字体名和对应文件,截图如下:
可以看到字体’仿宋_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 FilesPython37libsite-packagesmoviepyvideoVideoClip.py", line 1248, in __init__
subprocess_call(cmd, logger=None)
File "C:Program FilesPython37libsite-packagesmoviepy ools.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 FilesPython37libsite-packagesmoviepydecorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:Program FilesPython37libsite-packagesmoviepyvideoVideoClip.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:videoWinBasedWorkHard_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:videoWinBasedWorkHard_scroll.mp4", threads=8)
此时再播放文件可以看到中英文都正常显示:
更多moviepy的介绍请参考《PyQt+moviepy音视频剪辑实战文章目录》或《moviepy音视频开发专栏》。
关于收费专栏
老猿的付费专栏《使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,付费专栏《moviepy音视频开发专栏》详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,两个专栏加起来只需要19.9元,都适合有一定Python基础但无相关专利知识的小白读者学习。这2个收费专栏都有对应免费专栏,只是收费专栏的文章介绍更具体、内容更深入、案例更多。
对于缺乏Python基础的同仁,可以通过老猿的免费专栏《专栏:Python基础教程目录》从零开始学习Python。
如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。