File size: 1,968 Bytes
c43f92d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from faster_whisper import WhisperModel

class TranscriptionMaker():
    #書き起こしファイル(ファイル名_transcription.txt)を吐き出すディレクトリを指定
    def __init__(self,output_dir=os.path.abspath("/tmp/data/transcriptions")):
        self.model = WhisperModel("base", device="cpu")
        self.output_dir = output_dir
        try:
            if not os.path.exists(self.output_dir):
                os.makedirs(self.output_dir)
        except OSError as e:
            print(f"Error creating directory {self.output_dir}: {e}")
            raise

    #音声ファイルのパスを受け取り、書き起こしファイルを作成する
    def create_transcription(self,audio_path):
        try:
            if not os.path.isfile(audio_path):
                raise FileNotFoundError(f"The specified audio file does not exist: {audio_path}")
            
            segments, info = self.model.transcribe(audio_path)
            results = []

            for segment in segments:
                results.append({
                    "start": segment.start,
                    "end": segment.end,
                    "text": segment.text
                })

            #ファイルの書き込み
            output_file=os.path.join(self.output_dir,os.path.basename(audio_path)+"_transcription.txt")
            try:
                with open(output_file,"w",encoding="utf-8") as f:
                    for result in results:
                        f.write(f"[{result['start']:.2f}s - {result['end']:.2f}s] {result['text']}\n")
            except OSError as e:
                print(f"Error writing transcription file: {e}")
                raise
            return output_file
        except FileNotFoundError as e:
            print(f"Error: {e}")
            raise
        except Exception as e:
            print(f"An unexpected error occurred: {e}")
            raise