-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3_to_json.py
More file actions
43 lines (34 loc) · 1.22 KB
/
mp3_to_json.py
File metadata and controls
43 lines (34 loc) · 1.22 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
import whisper
import json
import os
import torch
# model = whisper.load_model("large-v2")
device = "cuda" if torch.cuda.is_available() else "cpu"
model = whisper.load_model("medium", device = device)
print(f"Model loaded successfully on", device)
audios = os.listdir("audios")
for audio in audios:
if ("_" in audio):
number = audio.split("_")[0]
title = audio.split("_")[1][:-4]
json_name = audio[:-4]
result = model.transcribe(
audio = f"audios/{audio}",
# audio = "audios/sample.mp3",
language="hi",
task="translate",
word_timestamps = False
)
chunks = []
for segment in result["segments"]:
chunks.append({
"number": number,
"title": title,
"start": segment["start"],
"end": segment["end"],
"text": segment["text"]
})
chunks_with_metadata = {"chunks": chunks, "text": result["text"]}
with open(f"jsons/{json_name}.json", "w") as f:
json.dump(chunks_with_metadata, f)
print(f"Audio Number {number} Converted in json Successfully!!!")