Skip to content

Commit

Permalink
finish function split Audio
Browse files Browse the repository at this point in the history
  • Loading branch information
CheshireCC committed Dec 29, 2023
1 parent 5d92b6c commit 8eac17c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
8 changes: 4 additions & 4 deletions fasterWhisperGUIConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"model_param": {
"localModel": true,
"onlineModel": false,
"model_path": "F:/WhisperModels/faster-whisper/large-v3-float32",
"model_path": "",
"modelName": 0,
"use_v3_model": true,
"device": 1,
"deviceIndex": "0",
"preciese": 0,
"thread_num": "4",
"num_worker": "1",
"download_root": "C:/Users/12059/.cache/huggingface/hub",
"download_root": "",
"local_files_only": false
},
"vad_param": {
Expand Down Expand Up @@ -67,8 +67,8 @@
"tabShadowEnabled": false,
"tabMaxWidth": 326,
"closeDisplayMode": 0,
"whisperXMinSpeaker": 0,
"whisperXMaxSpeaker": 0,
"whisperXMinSpeaker": 3,
"whisperXMaxSpeaker": 3,
"outputFormat": 0,
"outputEncoding": 1
}
Expand Down
25 changes: 12 additions & 13 deletions faster_whisper_GUI/mainWindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from .subtitleFileRead import readSRTFileToSegments
from .config import ENCODING_DICT
from .util import outputWithDateTime
from .split_audio import SplitAudioFileWithSpeakersWorker

# =======================================================================================
# SignalStore
Expand Down Expand Up @@ -1285,19 +1286,17 @@ def outputAudioPartWithSpeaker(self):
output audio part with speaker
"""
outputWithDateTime("SegmentAudioFileWithSpeaker")
for segments, file_path, info in self.current_result:
with av.open(file_path) as av_file:

stream_ = next(s for s in av_file.streams if s.codec_context.type == 'audio')
# stream_.
audio_channel_num = stream_.channels
if audio_channel_num < 2:
print("单声道音频")
split_setore = False
else:
print("双声道音频")



self.page_output.outputAudioPartWithSpeakerButton.setEnabled(False)
output_path = self.page_output.outputGroupWidget.LineEdit_output_dir.text()
self.splitAudioFileWithSpeakerWorker = SplitAudioFileWithSpeakersWorker(self.current_result,output_path, self)
self.splitAudioFileWithSpeakerWorker.result_signal.connect(self.splitAudioFileWithSpeakerWorkerFinished)
self.splitAudioFileWithSpeakerWorker.start()

def splitAudioFileWithSpeakerWorkerFinished(self):
mes = MessageBox("over","ok", self)
mes.show()
self.page_output.outputAudioPartWithSpeakerButton.setEnabled(True)

def singleAndSlotProcess(self):
"""
Expand Down
1 change: 1 addition & 0 deletions faster_whisper_GUI/seg_ment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, segment: Segment=None, start:float = 0, end: float = 0, text:
self.words = []
except Exception:
self.words = []
self.speaker = None

else:
self.start = float(start)
Expand Down
24 changes: 18 additions & 6 deletions faster_whisper_GUI/split_audio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from PySide6.QtCore import (QThread, Signal)
import subprocess
from .transcribe import secondsToHMS

class SplitAudioFileWithSpeakersWorker(QThread):
# 定义一个信号,用于在处理完成后发送结果
Expand All @@ -12,12 +13,13 @@ def __init__(self, segments_path_info_list:list, output_path, parent=None):
self.output_path = output_path

# 检查输出目录
if not os.path.exists(self.output_path):
if output_path and not os.path.exists(self.output_path):
os.makedirs(self.output_path)

def creatCommandLine(self, start_time, end_time, fileName, output_path, speaker):

output_fileName = self.getOutPutFileName(output_path, start_time, end_time, speaker)
# print(output_fileName)
commandLine = []
commandLine.append("ffmpeg")
commandLine.append("-i")
Expand All @@ -27,6 +29,7 @@ def creatCommandLine(self, start_time, end_time, fileName, output_path, speaker)
commandLine.append("-to")
commandLine.append(end_time)
commandLine.append(output_fileName)
return commandLine

def getOutPutFileName(self, output_path:str, start_time:str, end_time:str, speaker:str):
return os.path.join(output_path, f"{speaker}_{start_time.replace(':','_')}_{end_time.replace(':','_')}.wav")
Expand All @@ -36,28 +39,37 @@ def run(self):

for result in self.segments_path_info_list:
segments,path,info = result
_,file = os.path.split(path)
output_path = os.path.join(self.output_path, ".".join(file.split('.')[:-1]))
base_path,file = os.path.split(path)
print(f" current task: {file}")

if not self.output_path:
output_path = base_path
else:
output_path = self.output_path
output_path = os.path.join(output_path, ".".join(file.split('.')[:-1]))

# print(output_path)
# 检查输出路径
if not os.path.exists(output_path):
os.makedirs(output_path)

for segment in segments:
if not segment.speaker: continue
start_time = segment.start_time
end_time = segment.end_time
start_time = secondsToHMS(segment.start)
end_time = secondsToHMS(segment.end)
speaker = segment.speaker

commandLine = self.creatCommandLine(start_time.replace(',','.'),end_time.replace(',','.'),path,output_path,speaker)

# print(commandLine)
temp_process = subprocess.Popen(commandLine, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", text=True,
creationflags=subprocess.CREATE_NO_WINDOW)
temp_process.wait()

# 完成后发送结果信号
result = "结束"
result = "over"
self.result_signal.emit(result)
self.stop()

def stop(self):
self.is_running = False
Expand Down

0 comments on commit 8eac17c

Please sign in to comment.