Skip to content

Commit c18aa8b

Browse files
committed
Update whisperX.py
1 parent 81fcf56 commit c18aa8b

File tree

1 file changed

+1
-62
lines changed

1 file changed

+1
-62
lines changed

core/all_whisper_methods/whisperX.py

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,10 @@
55
import pandas as pd
66
import json
77
from typing import Dict
8-
import subprocess
9-
import base64
108

119
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
1210
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
4012

4113
def transcribe_audio(audio_file: str) -> Dict:
4214
from config import WHISPER_LANGUAGE
@@ -69,39 +41,6 @@ def transcribe_audio(audio_file: str) -> Dict:
6941
except Exception as e:
7042
raise Exception(f"WhisperX processing error: {e}")
7143

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-
10544
def save_results(df: pd.DataFrame):
10645
os.makedirs('output/log', exist_ok=True)
10746
excel_path = os.path.join('output/log', "cleaned_chunks.xlsx")

0 commit comments

Comments
 (0)