50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
import logging
|
|
from typing import List
|
|
|
|
from src.file_manager import FileManager
|
|
from src.transcriber import Transcriber
|
|
|
|
# Set up logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class TranscriptGenerator:
|
|
"""
|
|
TranscriptGenerator class that coordinates FileManager and Transcriber classes
|
|
to generate transcriptions from given input sources.
|
|
"""
|
|
|
|
def __init__(self, job_name, language_code="en"):
|
|
self.file_manager = FileManager()
|
|
self.transcriber = Transcriber(language_code)
|
|
self.job_name = job_name
|
|
|
|
def generate(self, input_sources: List[str]) -> None:
|
|
"""
|
|
Generates transcriptions for the given list of input sources.
|
|
|
|
Args:
|
|
input_sources (List[str]): List of input sources which could be URLs or local file paths.
|
|
"""
|
|
for i, source in enumerate(input_sources):
|
|
try:
|
|
if source.startswith("http"):
|
|
mp4_filename = f"audio_{self.job_name}_{i + 1}.mp4"
|
|
wav_filename = f"audio_{self.job_name}_{i + 1}.wav"
|
|
self.file_manager.download_youtube_audio(source, mp4_filename)
|
|
self.file_manager.convert_to_wav(mp4_filename, wav_filename)
|
|
else:
|
|
wav_filename = self.file_manager.load_local_file(source)
|
|
self.transcriber.transcribe(wav_filename)
|
|
json_output_file = f"result_{self.job_name}_{i + 1}.json"
|
|
txt_output_file = f"transcripts_{self.job_name}_{i + 1}.txt"
|
|
self.transcriber.save_transcript(json_output_file, txt_output_file)
|
|
except Exception as e:
|
|
logger.exception(
|
|
f"Failed to generate transcript for source {source}. Error: {str(e)}"
|
|
)
|
|
continue
|
|
|
|
logger.info("Transcription generation completed.")
|