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

☞ ░ 前往老猿Python博文目录

在moviepy2.0.0.Dev版本中,执行如下语句:

from  moviepy.editor import *
TextClip.search('gb', 'font')

报错:

Traceback (most recent call last):
File "F:/study/python/project/moviepyTest/moviepyTest.py", line 45, in
TextClip.search('gb', 'font')
File "C:\Program Files\Python37\lib\site-packages\moviepy\video\VideoClip.py", line 1307, in search
names_list = TextClip.list(arg)
File "C:\Program Files\Python37\lib\site-packages\moviepy\video\VideoClip.py", line 1283, in list
result = process.communicate()[0]
File "C:\Program Files\Python37\lib\subprocess.py", line 926, in communicate
stdout = self.stdout.read()
File "C:\Program Files\Python37\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb7 in position 66845: invalid start byte

这是中文解码的支持问题,解决办法将list方法的解码部分的’UTF-8’换成’ASN’或’CP936都可以,修改后的代码如下:

    def list(arg):
        """Returns a list of all valid entries for the ``font`` or ``color`` argument of
        ``TextClip``"""

        popen_params = {"stdout": sp.PIPE, "stderr": sp.DEVNULL, "stdin": sp.DEVNULL}

        if os.name == "nt":
            popen_params["creationflags"] = 0x08000000
        
        process = sp.Popen(
            [IMAGEMAGICK_BINARY, "-list", arg], encoding='cp936', **popen_params
        )
        result = process.communicate()[0]
        lines = result.splitlines()

        if arg == "font":
            # Slice removes first 8 characters: "  Font: "
            return [l[8:] for l in lines if l.startswith("  Font:")]
        elif arg == "color":
            # Each line is of the format "aqua  srgb(0,255,255)  SVG" so split on space and take
            # the first item to get the color name.
            # The first 5 lines are header information, not colors, so ignore
            return [l.split(" ")[0] for l in lines[5:]]
        else:
            raise Exception("Moviepy Error: Argument must equal 'font' or 'color'")

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

关于收费专栏

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

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

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

跟老猿学Python、学5G!

☞ ░ 前往老猿Python博文目录

posted on 2020-06-29 13:01  老猿Python  阅读(309)  评论(0编辑  收藏  举报