python3 解决 moviepy1.0.3 SubtitlesClip UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte

在使用moviepy处理视频时,加载srt字幕使用的SubtitlesClip这个类读取的文件。报错gbk的错误,

发现问题是moviepy安装的1.0.3版本,此版本采用open打开文件,而且没有指定编码

def file_to_subtitles(filename):
    """Converts a srt file into subtitles.

    The returned list is of the form ``[((ta,tb),'some text'),...]``
    and can be fed to SubtitlesClip.

    Only works for '.srt' format for the moment.
    """

    times_texts = []
    current_times = None
    current_text = ""
    with open(filename, "r") as f:
        for line in f:
            times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", line)
            if times:
                current_times = [cvsecs(t) for t in times]
            elif line.strip() == "":
                times_texts.append((current_times, current_text.strip("\n")))
                current_times, current_text = None, ""
            elif current_times:
                current_text += line
    return times_texts

网上发现很多方案,解决都不完美和彻底。

去官方发现有个  2.0.0.dev2 版本,决定尝试一下,修改requirements.txt文件

moviepy~=2.0.0.dev2
pip install -r requirements.txt

再去查看,有encoding 参数

sub = SubtitlesClip(subtitles=subtitle_path, encoding="utf-8"),问题解决。

猜你喜欢

转载自blog.csdn.net/2301_76814617/article/details/141332861