Spaces:
Running
Running
Update transcription.py
Browse files- transcription.py +48 -2
transcription.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
from faster_whisper import WhisperModel
|
|
|
3 |
|
4 |
class TranscriptionMaker():
|
5 |
#書き起こしファイルを吐き出すディレクトリを指定
|
@@ -14,8 +15,10 @@ class TranscriptionMaker():
|
|
14 |
raise
|
15 |
|
16 |
#音声ファイルのディレクトリを受け取り、書き起こしファイルを作成する
|
17 |
-
def create_transcription(self,
|
18 |
results = []
|
|
|
|
|
19 |
#ディレクトリ内のファイルを全て取得
|
20 |
if not os.path.isdir(audio_directory):
|
21 |
raise ValueError(f"The specified path is not a valid directory: {audio_directory}")
|
@@ -44,4 +47,47 @@ class TranscriptionMaker():
|
|
44 |
except OSError as e:
|
45 |
print(f"Error writing transcription file: {e}")
|
46 |
raise
|
47 |
-
return output_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from faster_whisper import WhisperModel
|
3 |
+
from pydub import AudioSegment
|
4 |
|
5 |
class TranscriptionMaker():
|
6 |
#書き起こしファイルを吐き出すディレクトリを指定
|
|
|
15 |
raise
|
16 |
|
17 |
#音声ファイルのディレクトリを受け取り、書き起こしファイルを作成する
|
18 |
+
def create_transcription(self,segments_directory):
|
19 |
results = []
|
20 |
+
#細切れ音声をくっつける
|
21 |
+
audio_directory = self.merge_segments(segments_directory)
|
22 |
#ディレクトリ内のファイルを全て取得
|
23 |
if not os.path.isdir(audio_directory):
|
24 |
raise ValueError(f"The specified path is not a valid directory: {audio_directory}")
|
|
|
47 |
except OSError as e:
|
48 |
print(f"Error writing transcription file: {e}")
|
49 |
raise
|
50 |
+
return output_file
|
51 |
+
|
52 |
+
#ファイル名が連続しているならくっつける
|
53 |
+
def merge_segments(self,segments_dir,output_dir = "/tmp/data/merged_audio"):
|
54 |
+
if not os.path.exists(output_dir):
|
55 |
+
os.makedirs(output_dir, exist_ok=True)
|
56 |
+
|
57 |
+
files = sorted([f for f in os.listdir(segments_dir) if f.endswith('.wav')])
|
58 |
+
|
59 |
+
merged_files = []
|
60 |
+
current_group = []
|
61 |
+
previous_index = None
|
62 |
+
|
63 |
+
for file in files:
|
64 |
+
# ファイル名から番号を抽出(例: "0.wav" -> 0)
|
65 |
+
file_index = int(file.split('.')[0])
|
66 |
+
|
67 |
+
# 番号が連続していない場合、新しいグループを作成
|
68 |
+
if previous_index is not None and file_index != previous_index + 1:
|
69 |
+
# 現在のグループを結合して保存
|
70 |
+
if current_group:
|
71 |
+
merged_files.append(current_group)
|
72 |
+
current_group = []
|
73 |
+
|
74 |
+
# 現在のファイルをグループに追加
|
75 |
+
current_group.append(file)
|
76 |
+
previous_index = file_index
|
77 |
+
|
78 |
+
# 最後のグループを追加
|
79 |
+
if current_group:
|
80 |
+
merged_files.append(current_group)
|
81 |
+
|
82 |
+
# グループごとに結合して保存
|
83 |
+
for i, group in enumerate(merged_files):
|
84 |
+
combined_audio = AudioSegment.empty()
|
85 |
+
for file in group:
|
86 |
+
file_path = os.path.join(segments_dir, file)
|
87 |
+
segment = AudioSegment.from_file(file_path)
|
88 |
+
combined_audio += segment
|
89 |
+
# 出力ファイル名を設定して保存
|
90 |
+
output_file = os.path.join(output_dir, f'merged_{i}.wav')
|
91 |
+
combined_audio.export(output_file, format='wav')
|
92 |
+
|
93 |
+
return output_dir
|