-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import os | ||
from pydub import AudioSegment | ||
|
||
class AudioChunker: | ||
@staticmethod | ||
def chunk_audio(wav_file_path, output_folder, chunk_length_ms=60000): | ||
audio = AudioSegment.from_wav(wav_file_path) | ||
chunk_paths = [] | ||
for i in range(0, len(audio), chunk_length_ms): | ||
chunk = audio[i:i + chunk_length_ms] | ||
chunk_path = os.path.join(output_folder, f"chunk_{i // chunk_length_ms}.wav") | ||
chunk.export(chunk_path, format="wav") | ||
chunk_paths.append(chunk_path) | ||
return chunk_paths |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pydub import AudioSegment | ||
|
||
class AudioConverter: | ||
@staticmethod | ||
def convert_mp3_to_wav(mp3_file_path, wav_file_path): | ||
audio = AudioSegment.from_mp3(mp3_file_path) | ||
audio.export(wav_file_path, format="wav") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import speech_recognition as sr | ||
|
||
class AudioRecognizer: | ||
def __init__(self, language='en'): | ||
self.recognizer = sr.Recognizer() | ||
self.language = language | ||
|
||
def recognize_speech(self, audio_file): | ||
with sr.AudioFile(audio_file) as source: | ||
audio = self.recognizer.record(source) | ||
try: | ||
text = self.recognizer.recognize_google(audio, language=self.language) | ||
print(f"Recognized text from {audio_file}: {text}") # Debug print | ||
return text | ||
except sr.UnknownValueError: | ||
print(f"Could not understand audio from {audio_file}") | ||
return None | ||
except sr.RequestError as e: | ||
print(f"Could not request results from {audio_file}; {e}") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import shutil | ||
|
||
class FolderManager: | ||
@staticmethod | ||
def create_output_folder(file_name): | ||
new_folder_name = f'output_{file_name}' | ||
os.makedirs(new_folder_name, exist_ok=True) | ||
return new_folder_name | ||
|
||
@staticmethod | ||
def clean_up(folder_path): | ||
files_to_keep = ['transcribed_text.txt', 'translated_text.txt', 'translated_speech.mp3'] | ||
for filename in os.listdir(folder_path): | ||
if filename not in files_to_keep: | ||
file_path = os.path.join(folder_path, filename) | ||
try: | ||
if os.path.isdir(file_path): | ||
shutil.rmtree(file_path) | ||
else: | ||
os.remove(file_path) | ||
except Exception as e: | ||
print(f"Error deleting {file_path}: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
from folder_manager import FolderManager | ||
from audio_converter import AudioConverter | ||
from audio_chunker import AudioChunker | ||
from audio_recognizer import AudioRecognizer | ||
from translator_module import TextTranslator | ||
from text_to_speech import TextToSpeech | ||
from text_saver import TextSaver | ||
|
||
def main(mp3_file_path): | ||
base_file_name = os.path.splitext(os.path.basename(mp3_file_path))[0] | ||
output_folder = FolderManager.create_output_folder(base_file_name) | ||
|
||
wav_file_path = os.path.join(output_folder, 'converted_audio.wav') | ||
AudioConverter.convert_mp3_to_wav(mp3_file_path, wav_file_path) | ||
|
||
chunk_paths = AudioChunker.chunk_audio(wav_file_path, output_folder) | ||
|
||
recognizer = AudioRecognizer(language='en') | ||
translator = TextTranslator(source_language='en', target_language='te') | ||
|
||
combined_text = "" | ||
for chunk_path in chunk_paths: | ||
recognized_text = recognizer.recognize_speech(chunk_path) | ||
if recognized_text: | ||
combined_text += recognized_text + " " | ||
|
||
if combined_text.strip(): | ||
TextSaver.save_text_files(combined_text, "", output_folder) | ||
|
||
translated_text = translator.translate_text(combined_text) | ||
if translated_text: | ||
TextSaver.save_text_files("", translated_text, output_folder) | ||
TextToSpeech.convert_text_to_speech(translated_text, os.path.join(output_folder, 'translated_speech.mp3')) | ||
|
||
FolderManager.clean_up(output_folder) | ||
|
||
if __name__ == "__main__": | ||
main('C:/Users/dkodurul_stu/WIP-Projects/sample.mp3') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import os | ||
|
||
class TextSaver: | ||
@staticmethod | ||
def save_text_files(transcribed_text, translated_text, output_folder): | ||
transcribed_file_path = os.path.join(output_folder, 'transcribed_text.txt') | ||
translated_file_path = os.path.join(output_folder, 'translated_text.txt') | ||
|
||
if transcribed_text.strip(): | ||
with open(transcribed_file_path, 'w', encoding='utf-8') as f: | ||
f.write(transcribed_text) | ||
|
||
if translated_text.strip(): | ||
with open(translated_file_path, 'w', encoding='utf-8') as f: | ||
f.write(translated_text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from gtts import gTTS | ||
import os | ||
|
||
class TextToSpeech: | ||
@staticmethod | ||
def convert_text_to_speech(text, output_file): | ||
try: | ||
# Initialize gTTS with the text, language set to Telugu ('te'), and slow set to False | ||
tts = gTTS(text=text, lang='te', slow=False) | ||
# Save the generated speech to an MP3 file | ||
tts.save(output_file) | ||
print(f"Audio saved to {output_file}") | ||
except Exception as e: | ||
print(f"Failed to convert text to speech: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from googletrans import Translator | ||
|
||
class TextTranslator: | ||
def __init__(self, source_language='en', target_language='te'): | ||
self.translator = Translator() | ||
self.source_language = source_language | ||
self.target_language = target_language | ||
|
||
def translate_text(self, text): | ||
try: | ||
translated = self.translator.translate(text, src=self.source_language, dest=self.target_language) | ||
return translated.text | ||
except Exception as e: | ||
print(f"Translation failed: {e}") | ||
return None |