-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.py
More file actions
145 lines (120 loc) · 5.63 KB
/
extractor.py
File metadata and controls
145 lines (120 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys
import os
from pathlib import Path
from PyQt6.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout,
QWidget, QFileDialog, QLabel, QProgressBar, QMessageBox, QComboBox)
from PyQt6.QtCore import QThread, pyqtSignal
# Решаем проблему с разными версиями moviepy (v1.0.3 vs v2.0+)
try:
try:
from moviepy.editor import VideoFileClip
except ImportError:
from moviepy import VideoFileClip
except ImportError:
print("Ошибка: Библиотека moviepy не найдена. Установите её: pip install moviepy")
class AudioExtractorThread(QThread):
finished = pyqtSignal(str)
error = pyqtSignal(str)
def __init__(self, video_path, format_ext):
super().__init__()
self.video_path = video_path
self.format_ext = format_ext
def run(self):
video = None
try:
# Загружаем видео
video = VideoFileClip(self.video_path)
video_path_obj = Path(self.video_path)
audio_path = str(video_path_obj.with_suffix(self.format_ext))
if not video.audio:
self.error.emit("В выбранном видео отсутствует аудиодорожка.")
return
# Настройки в зависимости от формата
if self.format_ext == ".wav":
# WAV 16kHz Mono — идеал для распознавания текста (STT)
video.audio.write_audiofile(
audio_path,
fps=16000,
nbytes=2,
codec='pcm_s16le',
ffmpeg_params=["-ac", "1"],
logger=None
)
else:
# Обычный MP3 для экономии места
video.audio.write_audiofile(audio_path, logger=None)
self.finished.emit(audio_path)
except Exception as e:
self.error.emit(str(e))
finally:
if video:
video.close()
# В новых версиях moviepy может потребоваться явное удаление объекта
if hasattr(video, 'reader') and video.reader:
video.reader.close()
if hasattr(video, 'audio') and video.audio and hasattr(video.audio, 'reader') and video.audio.reader:
video.audio.reader.close_all()
class VideoToAudioApp(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle("Extractor by Semyon v1.0")
self.setFixedSize(450, 250)
layout = QVBoxLayout()
# Выбор формата
self.label_fmt = QLabel("1. Выберите формат для будущего текста (STT):")
layout.addWidget(self.label_fmt)
self.format_chooser = QComboBox()
self.format_chooser.addItems([".wav (Рекомендуется для распознавания)", ".mp3 (Сжатый файл)"])
layout.addWidget(self.format_chooser)
# Кнопка действия
self.info_label = QLabel("2. Выберите видеофайл:")
layout.addWidget(self.info_label)
self.btn_open = QPushButton("ОТКРЫТЬ ВИДЕО И ИЗВЛЕЧЬ ЗВУК")
self.btn_open.setStyleSheet("height: 50px; font-weight: bold; background-color: #e1e1e1;")
self.btn_open.clicked.connect(self.select_file)
layout.addWidget(self.btn_open)
# Прогресс и статус
self.progress = QProgressBar()
self.progress.setRange(0, 0)
self.progress.hide()
layout.addWidget(self.progress)
self.status_label = QLabel("Статус: Ожидание")
layout.addWidget(self.status_label)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def select_file(self):
file_filter = "Video files (*.mp4 *.mkv *.avi *.mov *.wmv)"
file_path, _ = QFileDialog.getOpenFileName(self, "Выберите видео", "", file_filter)
if file_path:
selected_text = self.format_chooser.currentText()
ext = ".wav" if ".wav" in selected_text else ".mp3"
self.start_processing(file_path, ext)
def start_processing(self, path, ext):
self.btn_open.setEnabled(False)
self.format_chooser.setEnabled(False)
self.status_label.setText(f"Статус: Обработка {os.path.basename(path)}...")
self.progress.show()
self.thread = AudioExtractorThread(path, ext)
self.thread.finished.connect(self.on_success)
self.thread.error.connect(self.on_error)
self.thread.start()
def on_success(self, audio_path):
self.reset_ui()
self.status_label.setText("Статус: Успешно завершено")
QMessageBox.information(self, "Готово", f"Аудиофайл создан рядом с видео:\n{audio_path}")
def on_error(self, message):
self.reset_ui()
self.status_label.setText("Статус: Ошибка")
QMessageBox.critical(self, "Ошибка", f"Произошла ошибка:\n{message}")
def reset_ui(self):
self.btn_open.setEnabled(True)
self.format_chooser.setEnabled(True)
self.progress.hide()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = VideoToAudioApp()
window.show()
sys.exit(app.exec())