fix glueAudio
This commit is contained in:
10
app.py
10
app.py
@@ -115,7 +115,9 @@ def main():
|
|||||||
|
|
||||||
recognizeBtn.click(
|
recognizeBtn.click(
|
||||||
gh.handleRecognizeBtn,
|
gh.handleRecognizeBtn,
|
||||||
inputs=[audioFiles, fastWhisperModel, device, compute_type, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText, gr.State(GLUED_AUDIO_FILENAME), gr.State(OUTPUT_PATH)],
|
inputs=[audioFiles, fastWhisperModel, device, compute_type, beamSize,
|
||||||
|
vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2,
|
||||||
|
wordTimestamps, noSpeechThreshold, conditionOnPreviousText, gr.State(GLUED_AUDIO_FILENAME), gr.State(OUTPUT_PATH)],
|
||||||
outputs=[recognizedText],
|
outputs=[recognizedText],
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -123,7 +125,8 @@ def main():
|
|||||||
# автоматический пайплайн
|
# автоматический пайплайн
|
||||||
recognizedText.change(
|
recognizedText.change(
|
||||||
gh.generateByCondition,
|
gh.generateByCondition,
|
||||||
inputs=[apiKey, llmProvider, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("change"), saveFileCheckbox, filename, filenamePdf, gr.State(OUTPUT_PATH)],
|
inputs=[apiKey, llmProvider, llmModel, systemPrompt, recognizedText, llmTemperature,
|
||||||
|
isPipelineEnabledCheckbox, gr.State("change"), saveFileCheckbox, filename, filenamePdf, gr.State(OUTPUT_PATH)],
|
||||||
outputs=[refinedText, refinedTextMD]
|
outputs=[refinedText, refinedTextMD]
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -137,7 +140,8 @@ def main():
|
|||||||
)
|
)
|
||||||
refineTextBtn.click(
|
refineTextBtn.click(
|
||||||
gh.generateByCondition,
|
gh.generateByCondition,
|
||||||
inputs=[apiKey, llmProvider, llmModel, systemPrompt, recognizedText, llmTemperature, isPipelineEnabledCheckbox, gr.State("click"), saveFileCheckbox, filename, filenamePdf, gr.State(OUTPUT_PATH)],
|
inputs=[apiKey, llmProvider, llmModel, systemPrompt, recognizedText, llmTemperature,
|
||||||
|
isPipelineEnabledCheckbox, gr.State("click"), saveFileCheckbox, filename, filenamePdf, gr.State(OUTPUT_PATH)],
|
||||||
outputs=[refinedText, refinedTextMD]
|
outputs=[refinedText, refinedTextMD]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,61 @@
|
|||||||
from pydub import AudioSegment
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
class GlueAudio():
|
class GlueAudio():
|
||||||
def glue(self, audioFiles):
|
def glue(self, audio_files: list, output_path: str, output_filename: str) -> Path:
|
||||||
glued = AudioSegment.empty()
|
"""
|
||||||
|
Склеивает аудиофайлы РАЗНЫХ форматов с помощью FFmpeg и filter_complex.
|
||||||
|
Это универсальный и эффективный по памяти метод.
|
||||||
|
|
||||||
for audioFile in audioFiles:
|
Args:
|
||||||
audio = AudioSegment.from_file(audioFile)
|
audio_files (list): Список путей к исходным аудиофайлам.
|
||||||
glued += audio
|
output_path (str): Директория для сохранения итогового файла.
|
||||||
|
output_filename (str): Имя итогового склеенного файла.
|
||||||
|
|
||||||
return glued
|
Returns:
|
||||||
|
Path: Путь к созданному склеенному файлу.
|
||||||
|
"""
|
||||||
|
output_dir = Path(output_path)
|
||||||
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
final_audio_path = output_dir / output_filename
|
||||||
|
|
||||||
|
if not audio_files:
|
||||||
|
raise ValueError("Список аудиофайлов для склейки пуст.")
|
||||||
|
|
||||||
|
# 1. Формируем часть команды с входными файлами (-i file1 -i file2 ...)
|
||||||
|
input_args = []
|
||||||
|
for file_path in audio_files:
|
||||||
|
input_args.extend(['-i', str(Path(file_path).resolve())])
|
||||||
|
|
||||||
|
# 2. Формируем строку для filter_complex
|
||||||
|
num_files = len(audio_files)
|
||||||
|
stream_specifiers = "".join([f"[{i}:a]" for i in range(num_files)])
|
||||||
|
filter_complex_str = f"{stream_specifiers}concat=n={num_files}:v=0:a=1[outa]"
|
||||||
|
|
||||||
|
# 3. Собираем полную команду
|
||||||
|
command = [
|
||||||
|
'ffmpeg',
|
||||||
|
*input_args, # Распаковываем список входных файлов
|
||||||
|
'-filter_complex', filter_complex_str,
|
||||||
|
'-map', '[outa]',
|
||||||
|
'-c:a', 'libmp3lame',
|
||||||
|
'-q:a', '2',
|
||||||
|
str(final_audio_path),
|
||||||
|
'-y'
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 4. Выполняем команду
|
||||||
|
print(f"Выполнение команды FFmpeg: {' '.join(command)}")
|
||||||
|
subprocess.run(command, check=True, capture_output=True, text=True)
|
||||||
|
print("FFmpeg успешно завершил склейку.")
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise FileNotFoundError("FFmpeg не найден. Убедитесь, что он установлен и доступен в системной переменной PATH.")
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print("Ошибка при выполнении FFmpeg!")
|
||||||
|
print("Stderr:", e.stderr)
|
||||||
|
raise RuntimeError(f"Ошибка FFmpeg при склейке файлов: {e.stderr}")
|
||||||
|
|
||||||
|
return final_audio_path
|
||||||
@@ -12,15 +12,23 @@ class GradioHandlers:
|
|||||||
self.llm_factory = llm_factory # Сохраняем фабрику
|
self.llm_factory = llm_factory # Сохраняем фабрику
|
||||||
|
|
||||||
def handleRecognizeBtn(
|
def handleRecognizeBtn(
|
||||||
self, audioFiles, model, device,
|
self, audioFiles, model, device, compute_type, beamSize, vadFilter,
|
||||||
compute_type, beamSize, vadFilter,
|
minSilenceDurationMs, speechPadMs, temp0, temp1, temp2,
|
||||||
minSilenceDurationMs, speechPadMs,
|
wordTimestamps, noSpeechThreshold, conditionOnPreviousText, filename, outPath
|
||||||
temp0, temp1, temp2,
|
):
|
||||||
wordTimestamps, noSpeechThreshold, conditionOnPreviousText,
|
try:
|
||||||
filename, outPath):
|
glued_audio_path = self.ga.glue(
|
||||||
audioFile = self.ga.glue(audioFiles)
|
audio_files=[f.name for f in audioFiles], # Передаем список путей
|
||||||
file = self.fh.saveFile(filename, audioFile, outPath)
|
output_path=outPath,
|
||||||
return self.FasterWhisper.recognize(model, device, compute_type, file, beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText)
|
output_filename=filename
|
||||||
|
)
|
||||||
|
except (FileNotFoundError, RuntimeError) as e:
|
||||||
|
# Если FFmpeg не найден или произошла ошибка, сообщаем пользователю
|
||||||
|
gr.Warning(str(e))
|
||||||
|
return "" # Возвращаем пустую строку в текстовое поле
|
||||||
|
|
||||||
|
# Передаем путь к склеенному файлу в FasterWhisper
|
||||||
|
return self.FasterWhisper.recognize(model, device, compute_type, str(glued_audio_path), beamSize, vadFilter, minSilenceDurationMs, speechPadMs, temp0, temp1, temp2, wordTimestamps, noSpeechThreshold, conditionOnPreviousText)
|
||||||
|
|
||||||
# Функция улучшения текста
|
# Функция улучшения текста
|
||||||
def generateByCondition(self, api_key, llm_provider,
|
def generateByCondition(self, api_key, llm_provider,
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class FasterWhisper:
|
|||||||
|
|
||||||
for seg in segments:
|
for seg in segments:
|
||||||
text += f"[{self.format_timestamp(seg.start)} -> {self.format_timestamp(seg.end)}] {seg.text}" + '\n'
|
text += f"[{self.format_timestamp(seg.start)} -> {self.format_timestamp(seg.end)}] {seg.text}" + '\n'
|
||||||
|
print(f"[{self.format_timestamp(seg.start)} -> {self.format_timestamp(seg.end)}] {seg.text}")
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user