-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_subtitles.py
More file actions
108 lines (89 loc) · 2.99 KB
/
gen_subtitles.py
File metadata and controls
108 lines (89 loc) · 2.99 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from dotenv import load_dotenv
import os
from utils import download_youtube_audio, split_audio_with_sliding_window, process_chunks_and_collect_transcripts, create_srt_file
load_dotenv()
SARVAM_KEY = os.getenv('SARVAM_KEY')
# empty the audio_chunks folder
print('Emptying audio_chunks folder...')
try:
os.system('rm -rf audio_chunks/*')
os.system('rm -rf audio_files/*')
except Exception as e:
raise Exception('Error emptying audio_chunks folder. Delete all files in folder manually and try again.') from e
# take youtube link as input
youtube_link = input("Enter the youtube link: ")
# take language of video as input
# hi-IN: Hindi, bn-IN: Bengali, kn-IN: Kannada, ml-IN: Malayalam, mr-IN: Marathi, od-IN: Odia, pa-IN: Punjabi, ta-IN: Tamil, te-IN: Telugu, gu-IN: Gujarati, en-IN: English
num_to_language_mapping = {
0: 'Unknown',
1: 'Hindi',
2: 'Telugu',
3: 'Malayalam',
4: 'Kannada',
5: 'Bengali',
6: 'Marathi',
7: 'Odia',
8: 'Punjabi',
9: 'Tamil',
10: 'English',
11: 'Gujarati'
}
print("""
Language of the video and the number to be entered:
Unknow language: 0
Hindi: 1
Telugu: 2
Malayalam: 3
Kannada: 4
Bengali: 5
Marathi: 6
Odia: 7
Punjabi: 8
Tamil: 9
English: 10
Gujarati: 11
""")
language_num = int(input("Enter the language number: "))
if language_num not in num_to_language_mapping.keys():
language_num = 0
if language_num == 0:
prompt = None
else:
prompt = f"{num_to_language_mapping[language_num]} language audio"
print('The prompt to assist is:')
print(prompt)
# Download audio from the youtube video
print('Downloading audio from the youtube video...')
downloaded_audio = download_youtube_audio(youtube_link, output_path='audio_files', audio_format='mp3')
if downloaded_audio:
print(f"Downloaded audio file path: {downloaded_audio}")
else:
raise Exception("Failed to download audio file")
# Split audio into chunks
print('Splitting audio into chunks...')
if downloaded_audio:
try:
chunks = split_audio_with_sliding_window(
downloaded_audio
)
except Exception as e:
raise Exception(f"Failed to split audio into chunks: {e}")
else:
raise Exception("Download audio file not found")
# Send API request to Sarvam for transcribing the audio chunks
print('Sending API request to Sarvam for transcribing the audio chunks...')
try:
transcripts = process_chunks_and_collect_transcripts(chunks, SARVAM_KEY, prompt=prompt)
except Exception as e:
raise Exception(f"Failed to collect transcripts: {e}")
# create subtitle file named subtitles.srt'
video_name = downloaded_audio.split('/')[-1].rstrip('.mp3')
subtitle_filename = f'{video_name}.srt'
print(f'Creating subtitle file named {subtitle_filename}')
if transcripts:
try:
create_srt_file(transcripts, output_file=subtitle_filename, max_chars=42)
except Exception as e:
raise Exception(f"Failed to create srt file: {e}")
else:
raise Exception("No transcripts found")