Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

chunking fixes

Murtaza Nasir1ce3a173dc42
diff --git a/app.py b/app.py
index 403dffae..a785d499 100644
--- a/app.py
+++ b/app.py
@@ -36,6 +36,7 @@ from babel.dates import format_datetime
import ast
import logging
import secrets
+import time
from audio_chunking import AudioChunkingService, ChunkProcessingError, ChunkingNotSupportedError
# Load environment variables from .env file
@@ -1283,12 +1284,29 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_a
app.logger.info(f"Created {len(chunks)} chunks, processing each with Whisper API...")
- # Process each chunk
+ # Process each chunk with proper timeout and retry handling
chunk_results = []
+
+ # Create HTTP client with proper timeouts
+ timeout_config = httpx.Timeout(
+ connect=30.0, # 30 seconds to establish connection
+ read=300.0, # 5 minutes to read response (for large audio files)
+ write=60.0, # 1 minute to write request
+ pool=10.0 # 10 seconds to get connection from pool
+ )
+
+ http_client_with_timeout = httpx.Client(
+ verify=True,
+ timeout=timeout_config,
+ limits=httpx.Limits(max_connections=5, max_keepalive_connections=2)
+ )
+
transcription_client = OpenAI(
api_key=transcription_api_key,
base_url=transcription_base_url,
- http_client=http_client_no_proxy
+ http_client=http_client_with_timeout,
+ max_retries=2, # Limit retries to avoid excessive delays
+ timeout=300.0 # 5 minute timeout for API calls
)
whisper_model = os.environ.get("WHISPER_MODEL", "Systran/faster-distil-whisper-large-v3")
@@ -1300,42 +1318,101 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_a
user_transcription_language = recording.owner.transcription_language
for i, chunk in enumerate(chunks):
- try:
- app.logger.info(f"Processing chunk {i+1}/{len(chunks)}: {chunk['filename']} ({chunk['size_mb']:.1f}MB)")
-
- with open(chunk['path'], 'rb') as chunk_file:
- transcription_params = {
- "model": whisper_model,
- "file": chunk_file
- }
-
- if user_transcription_language:
- transcription_params["language"] = user_transcription_language
-
- transcript = transcription_client.audio.transcriptions.create(**transcription_params)
+ max_chunk_retries = 3
+ chunk_retry_count = 0
+ chunk_success = False
+
+ while chunk_retry_count < max_chunk_retries and not chunk_success:
+ try:
+ retry_suffix = f" (retry {chunk_retry_count + 1}/{max_chunk_retries})" if chunk_retry_count > 0 else ""
+ app.logger.info(f"Processing chunk {i+1}/{len(chunks)}: {chunk['filename']} ({chunk['size_mb']:.1f}MB){retry_suffix}")
- chunk_result = {
- 'index': chunk['index'],
- 'start_time': chunk['start_time'],
- 'end_time': chunk['end_time'],
- 'transcription': transcript.text,
- 'filename': chunk['filename']
- }
- chunk_results.append(chunk_result)
+ # Log detailed timing for each step
+ step_start_time = time.time()
- app.logger.info(f"Chunk {i+1} transcribed successfully: {len(transcript.text)} characters")
+ # Step 1: File opening
+ file_open_start = time.time()
+ with open(chunk['path'], 'rb') as chunk_file:
+ file_open_time = time.time() - file_open_start
+ app.logger.info(f"Chunk {i+1}: File opened in {file_open_time:.2f}s")
+
+ # Step 2: Prepare transcription parameters
+ param_start = time.time()
+ transcription_params = {
+ "model": whisper_model,
+ "file": chunk_file
+ }
+
+ if user_transcription_language:
+ transcription_params["language"] = user_transcription_language
+
+ param_time = time.time() - param_start
+ app.logger.info(f"Chunk {i+1}: Parameters prepared in {param_time:.2f}s")
+
+ # Step 3: API call with detailed timing
+ api_start = time.time()
+ app.logger.info(f"Chunk {i+1}: Starting API call to {transcription_base_url}")
+
+ # Log connection details
+ app.logger.info(f"Chunk {i+1}: Using timeout config - connect: 30s, read: 300s, write: 60s")
+ app.logger.info(f"Chunk {i+1}: Max retries: 2, API timeout: 300s")
+
+ transcript = transcription_client.audio.transcriptions.create(**transcription_params)
+
+ api_time = time.time() - api_start
+ app.logger.info(f"Chunk {i+1}: API call completed in {api_time:.2f}s")
+
+ # Step 4: Process response
+ response_start = time.time()
+ chunk_result = {
+ 'index': chunk['index'],
+ 'start_time': chunk['start_time'],
+ 'end_time': chunk['end_time'],
+ 'duration': chunk['duration'],
+ 'size_mb': chunk['size_mb'],
+ 'transcription': transcript.text,
+ 'filename': chunk['filename'],
+ 'processing_time': api_time # Store the actual API processing time
+ }
+ chunk_results.append(chunk_result)
+ response_time = time.time() - response_start
+
+ total_time = time.time() - step_start_time
+ app.logger.info(f"Chunk {i+1}: Response processed in {response_time:.2f}s")
+ app.logger.info(f"Chunk {i+1}: Total processing time: {total_time:.2f}s")
+ app.logger.info(f"Chunk {i+1} transcribed successfully: {len(transcript.text)} characters")
+ chunk_success = True
+
+ except Exception as chunk_error:
+ chunk_retry_count += 1
+ error_msg = str(chunk_error)
- except Exception as chunk_error:
- app.logger.error(f"Error processing chunk {i+1} ({chunk['filename']}): {chunk_error}")
- # Continue with other chunks, but note the failure
- chunk_result = {
- 'index': chunk['index'],
- 'start_time': chunk['start_time'],
- 'end_time': chunk['end_time'],
- 'transcription': f"[Chunk {i+1} transcription failed: {str(chunk_error)}]",
- 'filename': chunk['filename']
- }
- chunk_results.append(chunk_result)
+ if chunk_retry_count < max_chunk_retries:
+ # Determine wait time based on error type
+ if "timeout" in error_msg.lower() or "timed out" in error_msg.lower():
+ wait_time = 30 # 30 seconds for timeout errors
+ elif "rate limit" in error_msg.lower():
+ wait_time = 60 # 1 minute for rate limit errors
+ else:
+ wait_time = 15 # 15 seconds for other errors
+
+ app.logger.warning(f"Chunk {i+1} failed (attempt {chunk_retry_count}/{max_chunk_retries}): {chunk_error}. Retrying in {wait_time} seconds...")
+ time.sleep(wait_time)
+ else:
+ app.logger.error(f"Chunk {i+1} failed after {max_chunk_retries} attempts: {chunk_error}")
+ # Add failed chunk to results
+ chunk_result = {
+ 'index': chunk['index'],
+ 'start_time': chunk['start_time'],
+ 'end_time': chunk['end_time'],
+ 'transcription': f"[Chunk {i+1} transcription failed after {max_chunk_retries} attempts: {str(chunk_error)}]",
+ 'filename': chunk['filename']
+ }
+ chunk_results.append(chunk_result)
+
+ # Add small delay between chunks to avoid overwhelming the API
+ if i < len(chunks) - 1: # Don't delay after the last chunk
+ time.sleep(2)
# Merge transcriptions
app.logger.info(f"Merging {len(chunk_results)} chunk transcriptions...")
@@ -1344,6 +1421,17 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_a
if not merged_transcription.strip():
raise ChunkProcessingError("Merged transcription is empty")
+ # Log detailed performance statistics and analysis
+ chunking_service.log_processing_statistics(chunk_results)
+
+ # Get performance recommendations
+ recommendations = chunking_service.get_performance_recommendations(chunk_results)
+ if recommendations:
+ app.logger.info("=== PERFORMANCE RECOMMENDATIONS ===")
+ for i, rec in enumerate(recommendations, 1):
+ app.logger.info(f"{i}. {rec}")
+ app.logger.info("=== END RECOMMENDATIONS ===")
+
app.logger.info(f"Chunked transcription completed. Final length: {len(merged_transcription)} characters")
return merged_transcription
@@ -2920,9 +3008,16 @@ def upload_file():
original_file_size = file.tell()
file.seek(0)
- # Check size limit before saving
+ # Check size limit before saving - only enforce if chunking is disabled or using ASR endpoint
max_content_length = app.config.get('MAX_CONTENT_LENGTH')
- if max_content_length and original_file_size > max_content_length:
+
+ # Skip size check if chunking is enabled and using OpenAI Whisper API
+ should_enforce_size_limit = True
+ if ENABLE_CHUNKING and chunking_service and not USE_ASR_ENDPOINT:
+ should_enforce_size_limit = False
+ app.logger.info(f"Chunking enabled for OpenAI Whisper API - skipping {original_file_size/1024/1024:.1f}MB size limit check")
+
+ if should_enforce_size_limit and max_content_length and original_file_size > max_content_length:
raise RequestEntityTooLarge()
file.save(filepath)
diff --git a/audio_chunking.py b/audio_chunking.py
index 95e001bf..3b96c2eb 100644
--- a/audio_chunking.py
+++ b/audio_chunking.py
@@ -32,6 +32,7 @@ class AudioChunkingService:
self.max_chunk_size_mb = max_chunk_size_mb
self.overlap_seconds = overlap_seconds
self.max_chunk_size_bytes = max_chunk_size_mb * 1024 * 1024
+ self.chunk_stats = [] # Track processing statistics
def needs_chunking(self, file_path: str, use_asr_endpoint: bool = False) -> bool:
"""
@@ -77,47 +78,93 @@ class AudioChunkingService:
logger.error(f"Error getting audio duration for {file_path}: {e}")
return None
- def calculate_chunk_duration(self, file_path: str) -> Optional[float]:
+ def convert_to_wav_and_get_info(self, file_path: str, temp_dir: str) -> Tuple[str, float, float]:
"""
- Calculate optimal chunk duration based on file size and bitrate.
+ Convert the input file to WAV format and get its size and duration info.
Args:
- file_path: Path to the audio file
+ file_path: Path to the source audio file
+ temp_dir: Directory to store the temporary WAV file
Returns:
- Chunk duration in seconds, or None if unable to calculate
+ Tuple of (wav_file_path, duration_seconds, size_bytes)
"""
try:
- file_size = os.path.getsize(file_path)
- duration = self.get_audio_duration(file_path)
+ # Generate WAV filename
+ base_name = os.path.splitext(os.path.basename(file_path))[0]
+ wav_filename = f"{base_name}_converted.wav"
+ wav_path = os.path.join(temp_dir, wav_filename)
+
+ # Convert to WAV using the same settings we use for chunks
+ cmd = [
+ 'ffmpeg', '-i', file_path,
+ '-acodec', 'pcm_s16le',
+ '-ar', '16000',
+ '-ac', '1',
+ '-y', # Overwrite output file
+ wav_path
+ ]
+
+ logger.info(f"Converting {file_path} to WAV format for accurate chunking...")
+ result = subprocess.run(cmd, capture_output=True, text=True)
+ if result.returncode != 0:
+ raise ValueError(f"ffmpeg conversion failed: {result.stderr}")
+
+ if not os.path.exists(wav_path):
+ raise ValueError("WAV file was not created")
+
+ # Get the size and duration of the converted WAV file
+ wav_size = os.path.getsize(wav_path)
+ wav_duration = self.get_audio_duration(wav_path)
+
+ if not wav_duration:
+ raise ValueError("Could not determine WAV file duration")
+
+ logger.info(f"Converted WAV: {wav_size/1024/1024:.1f}MB, {wav_duration:.1f}s")
+ return wav_path, wav_duration, wav_size
+
+ except Exception as e:
+ logger.error(f"Error converting file to WAV: {e}")
+ raise
+
+ def calculate_chunk_duration_from_wav(self, wav_size: float, wav_duration: float) -> float:
+ """
+ Calculate optimal chunk duration based on actual WAV file size.
+
+ Args:
+ wav_size: Size of the WAV file in bytes
+ wav_duration: Duration of the WAV file in seconds
- if not duration:
- # Fallback: assume reasonable bitrate for chunk calculation
- # For 16kHz mono PCM: ~32KB/s, for MP3: ~128kbps = ~16KB/s
- estimated_bitrate_bytes_per_sec = 32000 # Conservative estimate
- chunk_duration = (self.max_chunk_size_bytes / estimated_bitrate_bytes_per_sec) - self.overlap_seconds
- return max(60, chunk_duration) # Minimum 60 seconds per chunk
+ Returns:
+ Chunk duration in seconds
+ """
+ try:
+ # Calculate actual bitrate from the WAV file
+ bitrate_bytes_per_sec = wav_size / wav_duration
- # Calculate actual bitrate
- bitrate_bytes_per_sec = file_size / duration
+ # Calculate chunk duration to stay under size limit with safety margin
+ safety_factor = 0.9 # Use 90% of max size for safety
+ target_chunk_size = self.max_chunk_size_bytes * safety_factor
- # Calculate chunk duration to stay under size limit
- chunk_duration = (self.max_chunk_size_bytes / bitrate_bytes_per_sec) - self.overlap_seconds
+ chunk_duration = (target_chunk_size / bitrate_bytes_per_sec) - self.overlap_seconds
- # Ensure reasonable chunk size (minimum 60 seconds, maximum 30 minutes)
- chunk_duration = max(60, min(1800, chunk_duration))
+ # Ensure reasonable chunk size (minimum 60 seconds, maximum 20 minutes)
+ chunk_duration = max(60, min(1200, chunk_duration))
- logger.info(f"Calculated chunk duration: {chunk_duration:.1f}s for file {file_path}")
+ logger.info(f"Calculated chunk duration: {chunk_duration:.1f}s based on WAV bitrate {bitrate_bytes_per_sec:.0f} bytes/sec")
return chunk_duration
except Exception as e:
- logger.error(f"Error calculating chunk duration for {file_path}: {e}")
+ logger.error(f"Error calculating chunk duration from WAV: {e}")
return 300 # Default 5 minutes
def create_chunks(self, file_path: str, temp_dir: str) -> List[Dict[str, Any]]:
"""
Split audio file into overlapping chunks.
+ First converts the file to WAV format to get accurate size information,
+ then calculates optimal chunk duration based on the actual WAV file size.
+
Args:
file_path: Path to the source audio file
temp_dir: Directory to store temporary chunk files
@@ -126,15 +173,14 @@ class AudioChunkingService:
List of chunk information dictionaries
"""
chunks = []
+ wav_path = None
try:
- duration = self.get_audio_duration(file_path)
- if not duration:
- raise ValueError("Could not determine audio duration")
+ # Step 1: Convert to WAV and get accurate size/duration info
+ wav_path, wav_duration, wav_size = self.convert_to_wav_and_get_info(file_path, temp_dir)
- chunk_duration = self.calculate_chunk_duration(file_path)
- if not chunk_duration:
- raise ValueError("Could not calculate chunk duration")
+ # Step 2: Calculate optimal chunk duration based on actual WAV file
+ chunk_duration = self.calculate_chunk_duration_from_wav(wav_size, wav_duration)
step_duration = chunk_duration - self.overlap_seconds
current_start = 0
@@ -142,9 +188,9 @@ class AudioChunkingService:
logger.info(f"Splitting {file_path} into chunks of {chunk_duration}s with {self.overlap_seconds}s overlap")
- while current_start < duration:
+ while current_start < wav_duration:
# Calculate end time for this chunk
- chunk_end = min(current_start + chunk_duration, duration)
+ chunk_end = min(current_start + chunk_duration, wav_duration)
actual_duration = chunk_end - current_start
# Skip very short chunks at the end
@@ -156,14 +202,12 @@ class AudioChunkingService:
chunk_filename = f"{base_name}_chunk_{chunk_index:03d}.wav"
chunk_path = os.path.join(temp_dir, chunk_filename)
- # Extract chunk using ffmpeg
+ # Extract chunk from the converted WAV file (more efficient than re-converting)
cmd = [
- 'ffmpeg', '-i', file_path,
+ 'ffmpeg', '-i', wav_path,
'-ss', str(current_start),
'-t', str(actual_duration),
- '-acodec', 'pcm_s16le',
- '-ar', '16000',
- '-ac', '1',
+ '-acodec', 'copy', # Copy codec since it's already in the right format
'-y', # Overwrite output file
chunk_path
]
@@ -177,6 +221,10 @@ class AudioChunkingService:
if os.path.exists(chunk_path):
chunk_size = os.path.getsize(chunk_path)
+ # Verify chunk size is within limits
+ if chunk_size > self.max_chunk_size_bytes:
+ logger.warning(f"Chunk {chunk_index} is {chunk_size/1024/1024:.1f}MB, exceeds {self.max_chunk_size_mb}MB limit")
+
chunk_info = {
'index': chunk_index,
'path': chunk_path,
@@ -210,6 +258,14 @@ class AudioChunkingService:
except Exception:
pass
raise
+ finally:
+ # Clean up the temporary WAV file
+ if wav_path and os.path.exists(wav_path):
+ try:
+ os.remove(wav_path)
+ logger.debug(f"Cleaned up temporary WAV file: {wav_path}")
+ except Exception as e:
+ logger.warning(f"Error cleaning up temporary WAV file: {e}")
def merge_transcriptions(self, chunk_results: List[Dict[str, Any]]) -> str:
"""
@@ -334,12 +390,173 @@ class AudioChunkingService:
similarity = intersection / union if union > 0 else 0
return similarity >= threshold
- def cleanup_chunks(self, chunks: List[Dict[str, Any]]) -> None:
+ def analyze_chunk_audio_properties(self, chunk_path: str) -> Dict[str, Any]:
+ """
+ Analyze audio properties of a chunk that might affect processing time.
+
+ Args:
+ chunk_path: Path to the chunk file
+
+ Returns:
+ Dictionary with audio analysis results
+ """
+ try:
+ # Get detailed audio information using ffprobe
+ cmd = [
+ 'ffprobe', '-v', 'quiet', '-print_format', 'json',
+ '-show_format', '-show_streams', chunk_path
+ ]
+
+ result = subprocess.run(cmd, capture_output=True, text=True, check=True)
+ probe_data = json.loads(result.stdout)
+
+ audio_stream = None
+ for stream in probe_data.get('streams', []):
+ if stream.get('codec_type') == 'audio':
+ audio_stream = stream
+ break
+
+ if not audio_stream:
+ return {'error': 'No audio stream found'}
+
+ format_info = probe_data.get('format', {})
+
+ analysis = {
+ 'duration': float(format_info.get('duration', 0)),
+ 'size_bytes': int(format_info.get('size', 0)),
+ 'bitrate': int(format_info.get('bit_rate', 0)),
+ 'sample_rate': int(audio_stream.get('sample_rate', 0)),
+ 'channels': int(audio_stream.get('channels', 0)),
+ 'codec': audio_stream.get('codec_name', 'unknown'),
+ 'bits_per_sample': int(audio_stream.get('bits_per_raw_sample', 0)),
+ }
+
+ # Calculate some derived metrics
+ if analysis['duration'] > 0:
+ analysis['effective_bitrate'] = (analysis['size_bytes'] * 8) / analysis['duration']
+ analysis['compression_ratio'] = analysis['bitrate'] / analysis['effective_bitrate'] if analysis['effective_bitrate'] > 0 else 0
+
+ return analysis
+
+ except Exception as e:
+ logger.warning(f"Error analyzing chunk audio properties: {e}")
+ return {'error': str(e)}
+
+ def log_processing_statistics(self, chunk_results: List[Dict[str, Any]]) -> None:
+ """
+ Log detailed statistics about chunk processing performance.
+
+ Args:
+ chunk_results: List of chunk processing results with timing info
+ """
+ if not chunk_results:
+ return
+
+ logger.info("=== CHUNK PROCESSING STATISTICS ===")
+
+ total_chunks = len(chunk_results)
+ processing_times = []
+ sizes = []
+ durations = []
+
+ for i, result in enumerate(chunk_results):
+ processing_time = result.get('processing_time', 0)
+ chunk_size = result.get('size_mb', 0)
+ chunk_duration = result.get('duration', 0)
+
+ processing_times.append(processing_time)
+ sizes.append(chunk_size)
+ durations.append(chunk_duration)
+
+ # Log individual chunk stats
+ rate = chunk_duration / processing_time if processing_time > 0 else 0
+ logger.info(f"Chunk {i+1}: {processing_time:.1f}s processing, {chunk_size:.1f}MB, {chunk_duration:.1f}s audio (rate: {rate:.2f}x)")
+
+ # Calculate summary statistics
+ if processing_times:
+ avg_time = sum(processing_times) / len(processing_times)
+ min_time = min(processing_times)
+ max_time = max(processing_times)
+
+ avg_size = sum(sizes) / len(sizes)
+ avg_duration = sum(durations) / len(durations)
+
+ total_audio_time = sum(durations)
+ total_processing_time = sum(processing_times)
+ overall_rate = total_audio_time / total_processing_time if total_processing_time > 0 else 0
+
+ logger.info(f"Summary: {total_chunks} chunks, {total_audio_time:.1f}s audio in {total_processing_time:.1f}s")
+ logger.info(f"Average: {avg_time:.1f}s processing, {avg_size:.1f}MB, {avg_duration:.1f}s audio")
+ logger.info(f"Range: {min_time:.1f}s - {max_time:.1f}s processing time")
+ logger.info(f"Overall rate: {overall_rate:.2f}x realtime")
+
+ # Identify performance outliers
+ if max_time > avg_time * 2:
+ slow_chunks = [i for i, t in enumerate(processing_times) if t > avg_time * 1.5]
+ logger.warning(f"Performance outliers detected: chunks {[i+1 for i in slow_chunks]} took significantly longer")
+
+ # Suggest possible causes
+ logger.info("Possible causes for slow processing:")
+ logger.info("- OpenAI API server load/performance variations")
+ logger.info("- Network latency or connection issues")
+ logger.info("- Audio content complexity (silence, noise, multiple speakers)")
+ logger.info("- Temporary API rate limiting or throttling")
+
+ logger.info("=== END STATISTICS ===")
+
+ def get_performance_recommendations(self, chunk_results: List[Dict[str, Any]]) -> List[str]:
"""
- Clean up temporary chunk files.
+ Generate performance recommendations based on processing results.
+
+ Args:
+ chunk_results: List of chunk processing results
+
+ Returns:
+ List of recommendation strings
+ """
+ recommendations = []
+
+ if not chunk_results:
+ return recommendations
+
+ processing_times = [r.get('processing_time', 0) for r in chunk_results]
+
+ if processing_times:
+ avg_time = sum(processing_times) / len(processing_times)
+ max_time = max(processing_times)
+
+ # Check for high variance in processing times
+ if max_time > avg_time * 3:
+ recommendations.append("High variance in processing times detected. Consider implementing retry logic with exponential backoff.")
+
+ # Check for overall slow processing
+ total_audio = sum(r.get('duration', 0) for r in chunk_results)
+ total_processing = sum(processing_times)
+ rate = total_audio / total_processing if total_processing > 0 else 0
+
+ if rate < 0.5: # Less than 0.5x realtime
+ recommendations.append("Overall processing is slow. Consider using smaller chunks or a different transcription service.")
+
+ # Check for timeout issues
+ if any(t > 300 for t in processing_times): # 5+ minutes
+ recommendations.append("Some chunks took over 5 minutes. Consider implementing timeout handling and chunk retry logic.")
+
+ # Check chunk size optimization
+ avg_size = sum(r.get('size_mb', 0) for r in chunk_results) / len(chunk_results)
+ if avg_size < 10:
+ recommendations.append("Chunks are relatively small. Consider increasing chunk size for better efficiency.")
+ elif avg_size > 22:
+ recommendations.append("Chunks are close to size limit. Consider reducing chunk size for more reliable processing.")
+
+ return recommendations
+
+ def cleanup_chunks(self, chunks: List[Dict[str, Any]], temp_wav_path: str = None) -> None:
+ """
+ Clean up temporary chunk files and WAV file.
Args:
chunks: List of chunk information dictionaries
+ temp_wav_path: Optional path to temporary WAV file to clean up
"""
for chunk in chunks:
try:
@@ -349,6 +566,14 @@ class AudioChunkingService:
logger.debug(f"Cleaned up chunk file: {chunk_path}")
except Exception as e:
logger.warning(f"Error cleaning up chunk {chunk.get('filename', 'unknown')}: {e}")
+
+ # Clean up temporary WAV file if provided
+ if temp_wav_path and os.path.exists(temp_wav_path):
+ try:
+ os.remove(temp_wav_path)
+ logger.debug(f"Cleaned up temporary WAV file: {temp_wav_path}")
+ except Exception as e:
+ logger.warning(f"Error cleaning up temporary WAV file: {e}")
class ChunkProcessingError(Exception):
"""Exception raised when chunk processing fails."""
diff --git a/static/js/app.js b/static/js/app.js
index 4741668e..55f12df9 100644
--- a/static/js/app.js
+++ b/static/js/app.js
@@ -1679,13 +1679,7 @@ document.addEventListener('DOMContentLoaded', () => {
);
if (isAudioFile) {
- // Check OpenAI-specific 25MB limit when not using ASR endpoint
- if (!useAsrEndpoint.value && fileObject.size > 25 * 1024 * 1024) {
- setGlobalError(`File "${fileObject.name}" exceeds OpenAI's 25MB limit. Please use a smaller file or enable the ASR endpoint for larger files.`);
- continue;
- }
-
- // Check general file size limit
+ // Only check general file size limit (chunking handles OpenAI 25MB limit automatically)
if (fileObject.size > maxFileSizeMB.value * 1024 * 1024) {
setGlobalError(`File "${fileObject.name}" exceeds the maximum size of ${maxFileSizeMB.value} MB and was skipped.`);
continue;
@@ -2045,8 +2039,51 @@ document.addEventListener('DOMContentLoaded', () => {
throw new Error('No audio streams available for recording.');
}
- // Setup MediaRecorder
- mediaRecorder.value = new MediaRecorder(combinedStream);
+ // Setup MediaRecorder with optimized settings for transcription
+ const getOptimizedRecorderOptions = () => {
+ // Define transcription-optimized options in order of preference
+ const optionsList = [
+ // Best option: Opus codec at 32kbps (excellent compression for speech)
+ {
+ mimeType: 'audio/webm;codecs=opus',
+ audioBitsPerSecond: 32000
+ },
+ // Fallback 1: Opus at 64kbps (slightly higher quality)
+ {
+ mimeType: 'audio/webm;codecs=opus',
+ audioBitsPerSecond: 64000
+ },
+ // Fallback 2: WebM with reduced bitrate
+ {
+ mimeType: 'audio/webm',
+ audioBitsPerSecond: 48000
+ },
+ // Fallback 3: MP4 with reduced bitrate
+ {
+ mimeType: 'audio/mp4',
+ audioBitsPerSecond: 48000
+ }
+ ];
+
+ // Test each option to find the first supported one
+ for (const options of optionsList) {
+ if (MediaRecorder.isTypeSupported(options.mimeType)) {
+ console.log(`Using optimized audio recording: ${options.mimeType} at ${options.audioBitsPerSecond}bps`);
+ showToast('Recording optimized for transcription', 'fa-compress-alt');
+ return options;
+ }
+ }
+
+ // Final fallback: no options (browser default)
+ console.log('Using browser default audio recording settings');
+ showToast('Recording with default quality', 'fa-microphone');
+ return undefined;
+ };
+
+ const recorderOptions = getOptimizedRecorderOptions();
+ mediaRecorder.value = recorderOptions
+ ? new MediaRecorder(combinedStream, recorderOptions)
+ : new MediaRecorder(combinedStream);
mediaRecorder.value.ondataavailable = event => audioChunks.value.push(event.data);
mediaRecorder.value.onstop = () => {
const audioBlob = new Blob(audioChunks.value, { type: 'audio/webm' });
@@ -3161,6 +3198,28 @@ document.addEventListener('DOMContentLoaded', () => {
}
};
+ // --- Audio Format Detection ---
+ const detectSupportedAudioFormats = () => {
+ const formats = [
+ 'audio/webm;codecs=opus',
+ 'audio/webm;codecs=vp9',
+ 'audio/webm',
+ 'audio/mp4;codecs=mp4a.40.2',
+ 'audio/mp4',
+ 'audio/ogg;codecs=opus',
+ 'audio/wav'
+ ];
+
+ const supportedFormats = formats.filter(format => MediaRecorder.isTypeSupported(format));
+ console.log('Supported audio recording formats:', supportedFormats);
+
+ if (supportedFormats.length === 0) {
+ console.warn('No optimized audio formats supported, will use browser default');
+ }
+
+ return supportedFormats;
+ };
+
// --- System Audio Detection ---
const detectSystemAudioCapabilities = async () => {
systemAudioSupported.value = false;