|
3 | 3 | import whisper |
4 | 4 |
|
5 | 5 |
|
6 | | -def transcribe_audio(path): |
7 | | - model = whisper.load_model("base") # Change this to your desired model |
8 | | - print("Whisper model loaded.") |
9 | | - transcribe = model.transcribe(audio=path) |
10 | | - segments = transcribe["segments"] |
| 6 | +def transcribe_audio(path, output_filetype="srt", whisper_model="base"): |
| 7 | + # extract the filename from the path without extension |
| 8 | + filename = os.path.splitext(os.path.basename(path))[0] |
| 9 | + output_filename = os.path.join("SrtFiles", f"{filename}.{output_filetype}") |
| 10 | + # chcek if the output file exists, if it does, append a number to the filename |
| 11 | + if os.path.exists(output_filename): |
| 12 | + i = 1 |
| 13 | + while os.path.exists(output_filename): |
| 14 | + output_filename = os.path.join("SrtFiles", f"{filename}({i}).{output_filetype}") |
| 15 | + i += 1 |
| 16 | + if output_filetype == "srt": |
| 17 | + # open the output file in write mode |
| 18 | + with open(output_filename, "w", encoding="utf-8") as srtFile: |
| 19 | + srtFile.write("") |
| 20 | + model = whisper.load_model(whisper_model) |
| 21 | + print("Whisper model loaded.") |
| 22 | + transcribe = model.transcribe(audio=path) |
| 23 | + segments = transcribe["segments"] |
11 | 24 |
|
12 | | - for segment in segments: |
13 | | - startTime = str(0) + str(timedelta(seconds=int(segment["start"]))) + ",000" |
14 | | - endTime = str(0) + str(timedelta(seconds=int(segment["end"]))) + ",000" |
15 | | - text = segment["text"] |
16 | | - segmentId = segment["id"] + 1 |
17 | | - segment = f"{segmentId}\n{startTime} --> {endTime}\n{text[1:] if text[0] is ' ' else text}\n\n" |
| 25 | + for segment in segments: |
| 26 | + startTime = str(0) + str(timedelta(seconds=int(segment["start"]))) + ",000" |
| 27 | + endTime = str(0) + str(timedelta(seconds=int(segment["end"]))) + ",000" |
| 28 | + text = segment["text"] |
| 29 | + segmentId = segment["id"] + 1 |
| 30 | + segment = f"{segmentId}\n{startTime} --> {endTime}\n{text[1:] if text[0] == ' ' else text}\n\n" |
| 31 | + with open(output_filename, "a", encoding="utf-8") as srtFile: |
| 32 | + srtFile.write(segment) |
| 33 | + return srtFile |
| 34 | + |
| 35 | + elif output_filetype == "json": |
| 36 | + with open(output_filename, "w", encoding="utf-8") as jsonFile: |
| 37 | + jsonFile.write("{\n \"captions\": [\n") |
| 38 | + model = whisper.load_model(whisper_model) |
| 39 | + print("Whisper model loaded.") |
| 40 | + transcribe = model.transcribe(audio=path) |
| 41 | + segments = transcribe["segments"] |
| 42 | + for segment in segments: |
| 43 | + startTime = timedelta(seconds=int(segment["start"])) |
| 44 | + endTime = timedelta(seconds=int(segment["end"])) |
| 45 | + duration = endTime - startTime # Calculate the duration |
| 46 | + startTime_str = str(0) + str(startTime) + ",000" |
| 47 | + endTime_str = str(0) + str(endTime) + ",000" |
| 48 | + duration_str = str(0) + str(duration) + ",000" |
| 49 | + text = segment["text"] |
| 50 | + segmentId = segment["id"] + 1 |
| 51 | + segment = f"{{\t\n\"id\": {segmentId},\n\"start\": \"{startTime_str}\",\n\"end\": \"{endTime_str}\",\n\"duration\": \"{duration_str}\",\n\"text\": \"{text[1:] if text[0] == ' ' else text}\"\n}},\n" |
| 52 | + with open(output_filename, "a", encoding="utf-8") as jsonFile: |
| 53 | + jsonFile.write(segment) |
| 54 | + # remove the last comma |
| 55 | + with open(output_filename, "rb+") as jsonFile: |
| 56 | + jsonFile.seek(-2, os.SEEK_END) |
| 57 | + jsonFile.truncate() |
| 58 | + with open(output_filename, "a", encoding="utf-8") as jsonFile: |
| 59 | + jsonFile.write("\n]\n}") |
| 60 | + return jsonFile |
| 61 | + |
| 62 | + elif output_filetype == "txt": |
| 63 | + with open(output_filename, "w", encoding="utf-8") as txtFile: |
| 64 | + txtFile.write("") |
| 65 | + model = whisper.load_model(whisper_model) |
| 66 | + print("Whisper model loaded.") |
| 67 | + transcribe = model.transcribe(audio=path) |
| 68 | + segments = transcribe["segments"] |
| 69 | + for segment in segments: |
| 70 | + startTime = str(0) + str(timedelta(seconds=int(segment["start"]))) + ",000" |
| 71 | + endTime = str(0) + str(timedelta(seconds=int(segment["end"]))) + ",000" |
| 72 | + text = segment["text"] |
| 73 | + segmentId = segment["id"] + 1 |
| 74 | + segment = f"{segmentId}\n{startTime} --> {endTime}\n{text[1:] if text[0] == ' ' else text}\n\n" |
| 75 | + with open(output_filename, "a", encoding="utf-8") as txtFile: |
| 76 | + txtFile.write(segment) |
| 77 | + return txtFile |
18 | 78 |
|
19 | | - srtFilename = os.path.join("SrtFiles", f"VIDEO_FILENAME.srt") |
20 | | - with open(srtFilename, "a", encoding="utf-8") as srtFile: |
21 | | - srtFile.write(segment) |
22 | 79 |
|
23 | | - return srtFilename |
| 80 | +output_dir = "SrtFiles" |
| 81 | +# check if the output directory exists, if it does not, create it |
| 82 | +if not os.path.exists(output_dir): |
| 83 | + os.mkdir("SrtFiles") |
| 84 | +path = input("Please enter the path of the audio file:") |
| 85 | +output_filetype = int(input("Please enter the output file type (SRT is selected by default):\n1.SRT\n2.JSON\n3.TXT\n")) |
| 86 | +if output_filetype == 1: |
| 87 | + output_filetype = "srt" |
| 88 | +elif output_filetype == 2: |
| 89 | + output_filetype = "json" |
| 90 | +elif output_filetype == 3: |
| 91 | + output_filetype = "txt" |
24 | 92 |
|
| 93 | +whisper_model = int(input("Please enter the name of the whisper model you want to use (base is selected by default):\n1.Tiny\n2.Base\n3.Small\n4.Medium\n5.Large\n")) |
| 94 | +if whisper_model == 1: |
| 95 | + whisper_model = "tiny" |
| 96 | +elif whisper_model == 2: |
| 97 | + whisper_model = "base" |
| 98 | +elif whisper_model == 3: |
| 99 | + whisper_model = "small" |
| 100 | +elif whisper_model == 4: |
| 101 | + whisper_model = "medium" |
| 102 | +elif whisper_model == 5: |
| 103 | + whisper_model = "large" |
25 | 104 |
|
26 | | -os.mkdir("SrtFiles") |
27 | | -path = input("Please enter the path of the audio file:") |
28 | | -srtFilename = transcribe_audio(path) |
| 105 | +srtFilename = transcribe_audio(path, output_filetype, whisper_model) |
| 106 | +# extract srt file name from srtFilename |
| 107 | +srtFilename = os.path.basename(srtFilename.name) |
29 | 108 | print(f"Your subtitles are ready. You can find them in {srtFilename}") |
0 commit comments