字幕文件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

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

posted @   不能说的秘密  阅读(3364)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示