Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dkodurul authored Sep 10, 2024
1 parent 2b49c18 commit dc1b9d4
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 0 deletions.
14 changes: 14 additions & 0 deletions audio_chunker.py
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
7 changes: 7 additions & 0 deletions audio_converter.py
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")
20 changes: 20 additions & 0 deletions audio_recognizer.py
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
23 changes: 23 additions & 0 deletions folder_manager.py
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}")
39 changes: 39 additions & 0 deletions main.py
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')
15 changes: 15 additions & 0 deletions text_saver.py
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)
14 changes: 14 additions & 0 deletions text_to_speech.py
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}")
15 changes: 15 additions & 0 deletions translator_module.py
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

0 comments on commit dc1b9d4

Please sign in to comment.