主要是有几个坑,要规避
-----其实按txt来读取的话,一个也不用安装,主要是我对python不熟悉,以为要装个什么东西。其实如果按txt来读取的,如果记事本能打开,也不用其他处理的话,不用特殊模块
1.被baidu人工智能坑了一把
我的问题是:在baidu里边搜 python 读取ass
回答:要使用Python来读取ASS文件(Advanced SubStation Alpha)可以通过第三方库pyssa
来完成。首先需要安装pyssa
库,可以在命令行中运行以下命令进行安装:
然而这个是错误的,而且pyssa这个库,第一没有轮子WHL,第二pip安装的时候报错
提示Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 这个错误
然后网上找了一圈,一看要安装这么大的包,赶紧找轮子,发现也没有轮子。最后想看看其他有没有库,后来发现百度坑我了。
2. 把问题改为:python 读取ass文件
答案就正确了。下面是答案
要使用Python来读取ASS(Advanced SubStation Alpha)字幕文件,可以使用第三方库pydub
。
首先,确保已经安装了pydub
库。如果没有安装,可以通过运行以下命令进行安装:
ip install pydub
然后,在Python中导入所需的模块并编写代码来读取ASS文件内容:
from pydub import AudioSegment
import os
# ASS文件路径
file_path = "your_subtitle.ass"
# 判断文件类型是否为ASS
if file_path[-4:] == ".ass":
# 打开ASS文件
with open(file_path, 'r', encoding='utf-8') as f:
ass_content = f.read()
print("ASS文件内容:\n", ass_content)
else:
print("不支持的文件格式")
上面的代码会将ASS文件的内容存储到变量ass_content
中,并输出该内容。注意,这里默认使用UTF-8编码来读取文件内容。根据实际情况,你也可以选择其他编码方式。
3、TCL电视的默认播放器的字幕不支持的坑(给儿子看电视,不想让他再去选个播放器)
不知道支持哪些字幕格式,反正有几个肯定不支持。ass和srt都是支持的,但是都是有限支持,就是能读,显示出来可能不是你想要的结果(和电脑上看视频不一样)
比如ass里边如下的,会重叠,但是\N 回车是支持的。而且不支持修改字体的大小和位置
至少这段,在电视上都是默认字体(而且会重叠)
Dialogue: 2,0:00:02.45,0:00:04.79,中,,0,0,0,,{\fad(200,200)}水善
Dialogue: 1,0:00:02.45,0:00:04.79,英,,0,0,0,,{\fad(200,200)}Water
另外
如下面这个srt格式的,改变字体位置和大小的,也是不支持的。
如果要自己修改,下面这2个格式解读 也是必要的 srt字幕解析(上)_srt{\pos}-CSDN博客
1
00:00:01,000 --> 00:00:04,000
{\an7} Top-left: an7
2
00:00:05,000 --> 00:00:08,000
{\an8}Top-center: an8
3
00:00:09,000 --> 00:00:12,000
{\an9}Top-right: an9
4
00:00:13,000 --> 00:00:16,000
{\an4}Middle-left: an4
5
00:00:17,000 --> 00:00:20,000
{\an5}Middle-center: an5
6
00:00:21,000 --> 00:00:24,000
{\an6}Middle-right: an6
7
00:00:25,000 --> 00:00:28,000
{\an1}Bottom-left: an1
8
00:00:29,000 --> 00:00:32,000
{\an2}Bottom-Center an2
9
00:00:33,000 --> 00:00:36,000
{\an3}Bottom-right: an3
11
00:00:37,000 --> 00:00:45,000
Back to default
{\pos(200,230)} # 和改变字体颜色等命令一样, 在爱奇艺万能播放器可用, 但ffmpeg合并时不支持该命令.
下面是Python 3.10写的
主要是把下面这个形式
Dialogue: 2,0:00:02.45,0:00:04.79,中,,0,0,0,,{\fad(200,200)}水善
Dialogue: 1,0:00:02.45,0:00:04.79,英,,0,0,0,,{\fad(200,200)}Water
Dialogue: 2,0:00:05.33,0:00:07.00,中,,0,0,0,,{\fad(200,200)}土强
Dialogue: 1,0:00:05.33,0:00:07.00,英,,0,0,0,,{\fad(200,200)}Earth
改为这个形式
Dialogue: 0,0:00:02.45,0:00:04.79,CC,,51,51,0,,水善\N{\fn微软雅黑\fs45}Water
Dialogue: 0,0:00:05.33,0:00:07.00,CC,,51,51,0,,土强\N{\fn微软雅黑\fs45}Earth
import os
# 指定要读取的目录
directory = "/"
# 获取目录下的所有文件和文件夹
files = os.listdir(os.getcwd())
lizi = r"""
用来自己看看,主要是把下面的变成上面的形式
Dialogue: 0,0:00:02.50,0:00:03.83,CC,,51,51,5,,水善\N{\fn微软雅黑\fs45}Water...
Dialogue: 0,0:00:04.87,0:00:06.29,CC,,51,51,5,,土强\N{\fn微软雅黑\fs45}Earth...
一开始是把读Dialogue: 2和Dialogue: 1,后来发现还有3,所有不行了,只能默认中文在上,英文在下。
Dialogue: 2,0:00:02.45,0:00:04.79,中,,0,0,0,,{\fad(200,200)}水善
Dialogue: 1,0:00:02.45,0:00:04.79,英,,0,0,0,,{\fad(200,200)}Water
Dialogue: 2,0:00:05.33,0:00:07.00,中,,0,0,0,,{\fad(200,200)}土强
Dialogue: 1,0:00:05.33,0:00:07.00,英,,0,0,0,,{\fad(200,200)}Earth
"""
# from pydub import AudioSegment 没必要 下面都没用到
for file in files:
print(file)
# ASS文件路径
#file_path = "S01E17.ass"
strar1 = """[Script Info]
; Script generated by Aegisub 3.2.2
; http://www.aegisub.org/
Title: 【BluRay 1080p】降世神通 Avatar: The Last Airbender 第二季 S02 (Book 2: Earth) 中英双字幕 中文(中国)字幕
Original Script: Generated by Xmader/bilitwin based on https://www.bilibili.com/video/av48304410?from=search&seid=10565814691118317516
ScriptType: v4.00+
PlayResX: 1440
PlayResY: 1080
Timer: 100.0000
YCbCr Matrix: TV.601
[Aegisub Project Garbage]
Audio File: ../修改完成/Avatar The Last Airbender (2005) S02E01 The Avatar State.mkv
Video File: ../修改完成/Avatar The Last Airbender (2005) S02E01 The Avatar State.mkv
Video AR Mode: 4
Video AR Value: 1.333333
Video Zoom Percent: 0.500000
Scroll Position: 313
Active Line: 317
Video Position: 33404
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: CC,方正黑体简体,60,&H00FFFFFF,&H00FFFFFF,&H00000000,&H7F000000,0,0,0,0,100,100,0,0,1,2,3,2,51,51,5,0
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
"""
d1 = 'Dialogue: 0'
m1 = ',CC,,51,51,5,,'
m2=r"""\N{\fn微软雅黑\fs45}"""
# 判断文件类型是否为ASS and file[0:5]=='S03E01'
if file[-4:] == ".ass" and file[0:1]!='N' :
# 打开ASS文件
with open(file, 'r', encoding='utf-8') as f:
ass_content = f.read()
#print("ASS文件内容:\n", ass_content)
t1=ass_content.find("Dialogue:")
str=ass_content[t1:]
lines = str.split("\n")
outstr=''
lmark='1'
laststart=''
for line in lines:
l2=line.split(",")
if len(l2)>2:
if l2[1] == laststart:
#不去做排序和判断,默认为上面是中文,下面是英文。中文在上,英文在下,另外考虑到有时候只有中文或者只有英文,那么回车放在开头比较好
n1=m2+line[line.find('}')+1:]
laststart=l2[1]
outstr=outstr+n1
else:
n1="\n"+d1+','+l2[1]+','+l2[2]+m1+line[line.find('}')+1:]
laststart=l2[1]
outstr=outstr+n1
# print("ASS文件内容:\n", ass_content[t1:])
print(n1)
with open("N"+file, 'w', encoding='utf-8') as nfile:
# 写入一些文本内容
nfile.write(strar1+outstr)
else:
print("不支持的文件格式")
# print(outstr)
下面这个代码用来读取srt文件,直接按txt来,然后来个文件另存为.srt或者.txt。
自己可以测试一下,如果可以用txt打开的,打开来没有乱码。如果用python把这个文件就看成文本,按文本自己去处理的话,那么真的不用装什么其他库了。装了某个库其实是有结构的处理这些信息,比如srt能读取起始时间了。要不然没啥用。
import os
# 指定要读取的目录
directory = "/"
# 获取目录下的所有文件和文件夹
files = os.listdir(os.getcwd())
for file in files:
# 打印文件清单 print(file)
if file[-4:] == ".srt":
with open(file, 'r', encoding='utf-8') as f:
ass_content = f.read()
print("ASS文件内容:\n", ass_content)
with open("N"+file, 'w', encoding='utf-8') as nfile:
# 写入一些文本内容
nfile.write('abc123')