zoukankan      html  css  js  c++  java
  • 字幕文件srt处理之pysrt

    什么是字幕文件?

    字幕文件就是在播放视频的时候加载的用来记录显示字幕的文件。文本格式字幕的扩展名通常是 ass、srt、smi、ssa 或 sub,因为是文本格式,所以尺寸很小,通常不过百十来 KB。其中 srt 文本字幕是最流行的,因为其制作和修改非常简单:一句时间代码 + 一句字幕。

    pysrt

    用来进行操作字幕文件的python第三方库,把字幕文件读取为一个对象,很容易进行读取修改保存等操作。

    安装以及示例

    安装的方式很简单直接用pip就可以

    pip install pysrt
    

    示例

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-#
    
    # -------------------------------------------------------------------------------
    # Name:         字幕文件读取
    # Author:       yunhgu
    # Date:         2021/6/29 9:53
    # Description: 
    # -------------------------------------------------------------------------------
    import pysrt
    
    srt = pysrt.open("srt_file.srt")
    content = srt.data[0]
    
    characters_per_second = content.characters_per_second
    duration = content.duration
    end = content.end
    from_lines = content.from_lines
    from_string = content.from_string
    index = content.index
    position = content.position
    shift = content.shift
    split_timestamps = content.split_timestamps
    start = content.start
    text = content.text
    text_without_tags = content.text_without_tags
    
    print(f"characters_per_second: {characters_per_second}")
    print(f"duration: {duration}")
    print(f"start: {start}")
    print(f"end: {end}")
    print(f"text: {text}")
    print(f"text_without_tags: {text_without_tags}")
    print(f"from_lines: {from_lines}")
    print(f"from_string:{from_string}")
    print(f"index: {index}")
    print(f"position: {position}")
    print(f"shift: {shift}")
    print(f"split_timestamps: {split_timestamps}")
    

    image

    还有一些其他的方法,感兴趣的可以自行测试。

    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    让CEF支持FLASH(非安装插件的形式)
    解决SQLServer 2008 日志无法收缩,收缩后大小不改变
    HTML Socket实现 .NET
    JS基础之BOM对象
    JavaScript对象
    JS函数
    JavaScript概述
    CSS块级元素和行内元素
    返回顶部示例
    CSS属性操作二
  • 原文地址:https://www.cnblogs.com/yunhgu/p/14948777.html
Copyright © 2011-2022 走看看