Skip to content

Instantly share code, notes, and snippets.

@theptrk
Last active June 14, 2023 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/a2c408596ad8e9858bf01fd8d3e61e40 to your computer and use it in GitHub Desktop.
Save theptrk/a2c408596ad8e9858bf01fd8d3e61e40 to your computer and use it in GitHub Desktop.
import json
import time
import whisper
MODEL_TYPE = "large"
model = whisper.load_model(MODEL_TYPE)
start = time.time()
file_name = "video"
extension = "mp4"
file_path = f"data/{file_name}.{extension}"
print(f"transcribing: {file_path}")
base_result = model.transcribe(file_path)
wc = len(base_result["text"].split(" "))
end = time.time()
total_seconds = end - start
print(f"total words: {wc}")
print(f"total seconds: {total_seconds}")
print(f"words/seconds: {wc}/{total_seconds}={wc/total_seconds}")
# save ['text']
with open(f'{file_name}-transcript-{MODEL_TYPE}.txt', 'w') as myfile:
myfile.write(base_result['text'])
# save ['segments']
with open(f'{file_name}-segments-{MODEL_TYPE}.json', 'w') as myfile:
myfile.write(json.dumps(base_result['segments']))
def get_segments_formatted(segments):
def seconds_to_HH_MM_SS(duration_seconds):
minutes, seconds = divmod(duration_seconds, 60)
hours, minutes = divmod(minutes, 60)
formatted_duration = "{:02d}:{:02d}:{:02d}".format(int(hours), int(minutes), int(seconds))
return formatted_duration
segment_transcript = []
for segment in segments:
start = seconds_to_HH_MM_SS(segment['start'])
text = segment['text']
print(f"{start} {text}")
segment_transcript.append(f"{start} {text}")
return "\n".join(segment_transcript)
# save ['segments'] but format the time
with open(f'{file_name}-sentence-transcript-{MODEL_TYPE}.txt', 'w') as myfile:
myfile.write(get_segments_formatted(base_result['segments']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment