Skip to content

Commit

Permalink
json format subtitles support
Browse files Browse the repository at this point in the history
  • Loading branch information
CheshireCC committed Mar 14, 2024
1 parent b5c0805 commit 5ad74fa
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 21 deletions.
8 changes: 4 additions & 4 deletions fasterWhisperGUIConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"preciese": 0,
"thread_num": "4",
"num_worker": "1",
"download_root": "G:/Program Files (x86)/FasterWhisperGUI/cache",
"download_root": "C:/Users/12059/.cache/huggingface/hub",
"local_files_only": false
},
"vad_param": {
Expand All @@ -39,7 +39,7 @@
},
"Transcription_param": {
"aggregate_contents": true,
"language": 1,
"language": 3,
"task": false,
"beam_size": "5",
"best_of": "5",
Expand Down Expand Up @@ -67,11 +67,11 @@
"tabMovable": false,
"tabScrollable": false,
"tabShadowEnabled": false,
"tabMaxWidth": 366,
"tabMaxWidth": 257,
"closeDisplayMode": 0,
"whisperXMinSpeaker": 2,
"whisperXMaxSpeaker": 3,
"outputFormat": 4,
"outputFormat": 0,
"outputEncoding": 1
}
}
2 changes: 1 addition & 1 deletion faster_whisper_GUI/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

STR_BOOL = {"False" : False, "True" : True}

SUBTITLE_FORMAT = ["SRT", "TXT", "VTT", "LRC", "SMI"]
SUBTITLE_FORMAT = ["SRT", "TXT", "VTT", "LRC", "SMI", "JSON"]

CAPTURE_PARA = [
{"rate": 44100
Expand Down
46 changes: 32 additions & 14 deletions faster_whisper_GUI/mainWindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
from .whisper_x import WhisperXWorker
from .de_mucs import DemucsWorker
# from .style_sheet import StyleSheet
from .subtitleFileRead import readSRTFileToSegments
from .subtitleFileRead import readSRTFileToSegments, readJSONFileToSegments
from .config import ENCODING_DICT
from .util import outputWithDateTime
from .split_audio import SplitAudioFileWithSpeakersWorker
Expand Down Expand Up @@ -1133,36 +1133,52 @@ def openExcitedFiles(self):
dataDir,_ = os.path.split(file)
self.page_process.fileNameListView.avDataRootDir = dataDir
# filesList = os.listdir(dataDir)

# json 格式字幕文件为首选
file_subtitle_fileName = ".".join(file.split(".")[:-1]+["json"])
ext_ = "json"
# 检测 json 格式字幕文件存在性
if not os.path.exists(file_subtitle_fileName):
# 没有 json 格式字幕文件的时候将会尝试获取 srt 格式字幕
file_subtitle_fileName = ".".join(file.split(".")[:-1]+["srt"])
ext_ = "srt"

file_subtitle_fileName = ".".join(file.split(".")[:-1]+["srt"])
fileName_subtitle_without_Ext = '.'.join(os.path.split(file_subtitle_fileName)[-1].split('.')[:-1])
# fileName_subtitle_without_Ext = '.'.join(os.path.split(file_subtitle_fileName)[-1].split('.')[:-1])

# 当字幕文件目录所指向的文件存在时
if os.path.exists(file_subtitle_fileName):
print(f"find existed srt file: {file_subtitle_fileName}")
# 获取文件的后缀名
# ext_ = file_subtitle_fileName.split(".")[-1]

else:
file_subtitle_fileName,_ = QFileDialog.getOpenFileName(
file_subtitle_fileName,ext_ = QFileDialog.getOpenFileName(
self,
self.tr("选择字幕文件"),
file_subtitle_fileName,
# self.page_process.fileNameListView.avDataRootDir,
"SRT file(*.srt)",

"JSON file(*.json);;SRT file(*.srt)",
)
# print(ext_)

if file_subtitle_fileName and os.path.isfile(file_subtitle_fileName):
print(f"get srt file: {file_subtitle_fileName}")
else:
messageBoxDia_ = MessageBox(self.tr("无效文件"),self.tr("必须要有有效的字幕文件"),self)
messageBoxDia_ = MessageBox(self.tr("没有字幕文件"),self.tr("必须要有有效的字幕文件"),self)
messageBoxDia_.show()
return

code_ = self.page_output.combox_output_code.currentText()

try:
segments = readSRTFileToSegments(file_subtitle_fileName, file_code=ENCODING_DICT[code_])
except UnicodeDecodeError as e:
print("read subtitle file failed")
print(str(e))
self.raiseErrorInfoBar(self.tr("读取字幕文件失败"), self.tr("读取失败 文件可能使用了不同的编码\n检查 fasterwhispergui.log 文件可能会获取更多信息"))
if ext_ in ["JSON file(*.json)" ,"json"]:
segments = readJSONFileToSegments(file_subtitle_fileName, file_code=ENCODING_DICT[code_])
else:
segments = readSRTFileToSegments(file_subtitle_fileName, file_code=ENCODING_DICT[code_])
except Exception as e:
print("read subtitle file failed:")
print(f" {str(e)}")
self.raiseErrorInfoBar(self.tr("读取失败"), self.tr("读取字幕文件失败 \n检查日志文件可能会获取更多信息"))
return
# 输出字幕文件内容
# for segment in segments:
Expand Down Expand Up @@ -1509,9 +1525,11 @@ def deleteResultTableEvent(self, routeKey:str):
self.outputWithDateTime("deleteTable")

print(f"len_DataModel:{len(self.tableModel_list)}")
# print(routeKey)
for tb in self.tableModel_list.items():
print(f" {tb[0]}")
print(f"data to delete: {routeKey}")
file_key ="_".join(routeKey.split("_")[1:])
print(file_key)
print(f"key: {file_key}")
self.tableModel_list.pop(file_key)
print(f"len_DataModel_after_pop:{len(self.tableModel_list)}")

Expand Down
18 changes: 18 additions & 0 deletions faster_whisper_GUI/subtitleFileRead.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
# coding:utf-8

import os
from typing import List
from .seg_ment import segment_Transcribe
import json
from faster_whisper import Word

def readJSONFileToSegments(file:str, file_code = "utf8") -> List[segment_Transcribe]:

with open(os.path.abspath(file),"r", encoding= file_code) as fp:
subtitles_str = json.load(fp=fp)["body"]
segments = [ segment_Transcribe(
start=subtitle["from"],
end=subtitle["to"],
text=subtitle["content"],
words=[ Word(word["start"],word["end"],word["word"],word["probability"]) for word in subtitle["words"]],
speaker=subtitle["speaker"] or None
) for subtitle in subtitles_str
]
return segments


def readSRTFileToSegments(file:str, file_code = "utf8") -> List[segment_Transcribe]:

Expand Down
6 changes: 4 additions & 2 deletions faster_whisper_GUI/tableViewInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,15 @@ def onCurrentIndexChanged(self, index):
def removeTab(self, index):

item = self.tabBar.tabItem(index)
print(f"removeTab: {item.routeKey()}")

self.signal_delete_table.emit(item.routeKey())

widget = self.findChild(TableView, item.routeKey())

# print(f"table:{widget}")
self.stackedWidget.removeWidget(widget)
self.tabBar.removeTab(index)
widget.deleteLater()
# widget.deleteLater()


class CustomTableView(TableView):
Expand Down
31 changes: 31 additions & 0 deletions faster_whisper_GUI/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import torch
import numpy as np
import av
import json

from faster_whisper import (
WhisperModel
Expand Down Expand Up @@ -410,8 +411,38 @@ def writeSubtitles(outputFileName:str,
wirteLRC(outputFileName, segments,language=language, file_code=file_code)
elif format == "SMI":
writeSMI(outputFileName, segments, language=language, avFile=fileName, file_code=file_code)
elif format == "JSON":
writeJson(outputFileName, segments, language=language, avFile=fileName, file_code=file_code)

print(f"write over | {outputFileName}")

def writeJson(fileName:str,segments:List[segment_Transcribe],language:str,avFile="",file_code="utf8"):
result = {}
result["language"] = language
result["file"]=avFile

result["body"] = []

for segment in segments:
result["body"].append(
{
"from":segment.start,
"to":segment.end,
"location":2,
"content":segment.text,
"speaker":segment.speaker or "",
"words":[{"start":word.start,"end":word.end,"word":word.word,"probability":word.probability }for word in segment.words]
}
)
with open(os.path.abspath(fileName),'w',encoding=file_code) as fp:

json.dump(
result,
fp,
ensure_ascii=False,
indent=4
)


def writeSMI(fileName:str, segments:List[segment_Transcribe], language:str, avFile = "",file_code="utf8"):

Expand Down

0 comments on commit 5ad74fa

Please sign in to comment.