|
5 | 5 | import pandas as pd |
6 | 6 | import json |
7 | 7 | from typing import Dict |
8 | | -import subprocess |
9 | | -import base64 |
10 | 8 |
|
11 | 9 | sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) |
12 | 10 | from config import MODEL_DIR |
13 | | - |
14 | | -def convert_video_to_audio(input_file: str) -> str: |
15 | | - os.makedirs('output/audio', exist_ok=True) |
16 | | - audio_file = 'output/audio/raw_full_audio.wav' |
17 | | - |
18 | | - if not os.path.exists(audio_file): |
19 | | - ffmpeg_cmd = [ |
20 | | - 'ffmpeg', |
21 | | - '-i', input_file, |
22 | | - '-vn', |
23 | | - '-acodec', 'libmp3lame', |
24 | | - '-ar', '16000', |
25 | | - '-b:a', '64k', |
26 | | - audio_file |
27 | | - ] |
28 | | - print(f"🎬➡️🎵 Converting to audio......") |
29 | | - subprocess.run(ffmpeg_cmd, check=True, stderr=subprocess.PIPE) |
30 | | - print(f"🎬➡️🎵 Converted <{input_file}> to <{audio_file}>\n") |
31 | | - |
32 | | - return audio_file |
33 | | - |
34 | | -def encode_file_to_base64(file_path: str) -> str: |
35 | | - print("🔄 Encoding audio file to base64...") |
36 | | - with open(file_path, 'rb') as file: |
37 | | - encoded = base64.b64encode(file.read()).decode('utf-8') |
38 | | - print("✅ File successfully encoded to base64") |
39 | | - return encoded |
| 11 | +from core.all_whisper_methods.whisperXapi import process_transcription, convert_video_to_audio |
40 | 12 |
|
41 | 13 | def transcribe_audio(audio_file: str) -> Dict: |
42 | 14 | from config import WHISPER_LANGUAGE |
@@ -69,39 +41,6 @@ def transcribe_audio(audio_file: str) -> Dict: |
69 | 41 | except Exception as e: |
70 | 42 | raise Exception(f"WhisperX processing error: {e}") |
71 | 43 |
|
72 | | -def process_transcription(result: Dict) -> pd.DataFrame: |
73 | | - from config import get_joiner, WHISPER_LANGUAGE |
74 | | - language = result['language'] if WHISPER_LANGUAGE == 'auto' else WHISPER_LANGUAGE # consider force english case |
75 | | - joiner = get_joiner(language) |
76 | | - |
77 | | - all_words = [] |
78 | | - for segment in result['segments']: |
79 | | - for word in segment['words']: |
80 | | - # ! For French, we need to convert guillemets to empty strings |
81 | | - word["word"] = word["word"].replace('»', '').replace('«', '') |
82 | | - |
83 | | - if 'start' not in word and 'end' not in word: |
84 | | - if all_words: |
85 | | - # Merge with the previous word |
86 | | - all_words[-1]['text'] = f'{all_words[-1]["text"]}{joiner}{word["word"]}' |
87 | | - else: |
88 | | - # If it's the first word, temporarily save it and wait for the next word with a timestamp |
89 | | - temp_word = word["word"] |
90 | | - else: |
91 | | - # Normal case, with start and end times |
92 | | - word_dict = { |
93 | | - 'text': f'{temp_word}{word["word"]}' if 'temp_word' in locals() else f'{word["word"]}', |
94 | | - 'start': word.get('start', all_words[-1]['end'] if all_words else 0), |
95 | | - 'end': word['end'], |
96 | | - 'score': word.get('score', 0) |
97 | | - } |
98 | | - |
99 | | - all_words.append(word_dict) |
100 | | - if 'temp_word' in locals(): |
101 | | - del temp_word |
102 | | - |
103 | | - return pd.DataFrame(all_words) |
104 | | - |
105 | 44 | def save_results(df: pd.DataFrame): |
106 | 45 | os.makedirs('output/log', exist_ok=True) |
107 | 46 | excel_path = os.path.join('output/log', "cleaned_chunks.xlsx") |
|
0 commit comments