Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Refactor: Improve audio conversion pipeline
Jade Ellis604ff47cf1c8
diff --git a/src/api/recordings.py b/src/api/recordings.pyindex c83dbc6f..2323d451 100644--- a/src/api/recordings.py+++ b/src/api/recordings.py@@ -22,8 +22,9 @@ from email.utils import encode_rfc2231from src.database import dbfrom src.models import *from src.utils import *-from src.config.app_config import ASR_MIN_SPEAKERS, ASR_MAX_SPEAKERS, ASR_DIARIZE, AUDIO_COMPRESS_UPLOADS, AUDIO_CODEC, AUDIO_BITRATE-from src.tasks.processing import format_transcription_for_llm, transcribe_with_chunking, compress_lossless_audio+from src.config.app_config import ASR_MIN_SPEAKERS, ASR_MAX_SPEAKERS, ASR_DIARIZE+from src.tasks.processing import format_transcription_for_llm, transcribe_with_chunking+from src.utils.ffmpeg_utils import FFmpegError, FFmpegNotFoundErrorfrom src.services.speaker import update_speaker_usage, identify_unidentified_speakers_from_textfrom src.services.speaker_embedding_matcher import update_speaker_embeddingfrom src.services.speaker_snippets import create_speaker_snippets@@ -31,6 +32,8 @@ from src.services.document import process_markdown_to_docxfrom src.services.llm import client, chat_client, call_llm_completion, call_chat_completion, process_streaming_with_thinkingfrom src.services.embeddings import process_recording_chunksfrom src.file_exporter import export_recording, mark_export_as_deleted+from src.utils.ffprobe import get_codec_info, FFProbeError+from src.utils.audio_conversion import convert_if_needed# Create blueprintrecordings_bp = Blueprint('recordings', __name__)@@ -206,7 +209,7 @@ def download_summary_word(recording_id):from docx.shared import Inchesimport refrom io import BytesIO-+recording = db.session.get(Recording, recording_id)if not recording:return jsonify({'error': 'Recording not found'}), 404@@ -216,10 +219,10 @@ def download_summary_word(recording_id):if not recording.summary:return jsonify({'error': 'No summary available for this recording'}), 400-+# Create Word documentdoc = Document()-+# Add titletitle_text = f'Summary: {recording.title or "Untitled Recording"}'title = doc.add_heading(title_text, 0)@@ -261,12 +264,12 @@ def download_summary_word(recording_id):# Process markdown content using the helper functionprocess_markdown_to_docx(doc, recording.summary)-+# Save to BytesIOdoc_stream = BytesIO()doc.save(doc_stream)doc_stream.seek(0)-+# Create safe filenamesafe_title = re.sub(r'[<>:"/\\|?*]', '', recording.title or 'Untitled')safe_title = re.sub(r'[-\s]+', '-', safe_title).strip('-')@@ -302,7 +305,7 @@ def download_summary_word(recording_id):current_app.logger.error(f"RFC2231 encoding failed: {e}, using fallback")response.headers['Content-Disposition'] = f'attachment; filename="download-{recording_id}.docx"'return response-+except Exception as e:current_app.logger.error(f"Error generating summary Word document: {e}")return jsonify({'error': 'Failed to generate Word document'}), 500@@ -318,7 +321,7 @@ def download_chat_word(recording_id):from docx.shared import Inchesimport refrom io import BytesIO-+recording = db.session.get(Recording, recording_id)if not recording:return jsonify({'error': 'Recording not found'}), 404@@ -330,14 +333,14 @@ def download_chat_word(recording_id):data = request.jsonif not data or 'messages' not in data:return jsonify({'error': 'No messages provided'}), 400-+messages = data['messages']if not messages:return jsonify({'error': 'No messages to download'}), 400-+# Create Word documentdoc = Document()-+# Add titletitle_text = f'Chat Conversation: {recording.title or "Untitled Recording"}'title = doc.add_heading(title_text, 0)@@ -350,7 +353,7 @@ def download_chat_word(recording_id):run.font.name = 'Arial'r = run._elementr.rPr.rFonts.set(qn('w:eastAsia'), 'Arial')-+# Helper function to add paragraph with Unicode supportdef add_unicode_paragraph(doc, text):p = doc.add_paragraph(text)@@ -368,13 +371,13 @@ def download_chat_word(recording_id):add_unicode_paragraph(doc, f'Recording Date: {recording.created_at.strftime("%Y-%m-%d %H:%M")}')add_unicode_paragraph(doc, f'Chat Export Date: {datetime.utcnow().strftime("%Y-%m-%d %H:%M")}')doc.add_paragraph('') # Empty line-+# Add chat messagesfor message in messages:role = message.get('role', 'unknown')content = message.get('content', '')thinking = message.get('thinking', '')-+# Add role headerif role == 'user':p = doc.add_paragraph()@@ -388,24 +391,24 @@ def download_chat_word(recording_id):p = doc.add_paragraph()run = p.add_run(f'{role.title()}: ')run.bold = True-+# Add thinking content if presentif thinking and role == 'assistant':p = doc.add_paragraph()p.add_run('[Model Reasoning]\n').italic = Truep.add_run(thinking).italic = Truedoc.add_paragraph('') # Empty line-+# Add message content with markdown formattingprocess_markdown_to_docx(doc, content)-+doc.add_paragraph('') # Empty line between messages-+# Save to BytesIOdoc_stream = BytesIO()doc.save(doc_stream)doc_stream.seek(0)-+# Create safe filenamesafe_title = re.sub(r'[<>:"/\\|?*]', '', recording.title or 'Untitled')safe_title = re.sub(r'[-\s]+', '-', safe_title).strip('-')@@ -442,7 +445,7 @@ def download_chat_word(recording_id):current_app.logger.error(f"RFC2231 encoding failed: {e}, using fallback")response.headers['Content-Disposition'] = f'attachment; filename="download-{recording_id}.docx"'return response-+except Exception as e:current_app.logger.error(f"Error generating chat Word document: {e}")return jsonify({'error': 'Failed to generate Word document'}), 500@@ -458,7 +461,7 @@ def download_notes_word(recording_id):from docx.shared import Inchesimport refrom io import BytesIO-+recording = db.session.get(Recording, recording_id)if not recording:return jsonify({'error': 'Recording not found'}), 404@@ -468,10 +471,10 @@ def download_notes_word(recording_id):if not recording.notes:return jsonify({'error': 'No notes available for this recording'}), 400-+# Create Word documentdoc = Document()-+# Add titletitle_text = f'Notes: {recording.title or "Untitled Recording"}'title = doc.add_heading(title_text, 0)@@ -512,12 +515,12 @@ def download_notes_word(recording_id):# Process markdown content using the helper functionprocess_markdown_to_docx(doc, recording.notes)-+# Save to BytesIOdoc_stream = BytesIO()doc.save(doc_stream)doc_stream.seek(0)-+# Create safe filenamesafe_title = re.sub(r'[<>:"/\\|?*]', '', recording.title or 'Untitled')safe_title = re.sub(r'[-\s]+', '-', safe_title).strip('-')@@ -553,7 +556,7 @@ def download_notes_word(recording_id):current_app.logger.error(f"RFC2231 encoding failed: {e}, using fallback")response.headers['Content-Disposition'] = f'attachment; filename="download-{recording_id}.docx"'return response-+except Exception as e:current_app.logger.error(f"Error generating notes Word document: {e}")return jsonify({'error': 'Failed to generate Word document'}), 500@@ -571,19 +574,19 @@ def generate_summary_endpoint(recording_id):if not has_recording_access(recording, current_user, require_edit=True):return jsonify({'error': 'You do not have permission to generate summary for this recording'}), 403-+# Check if transcription existsif not recording.transcription or len(recording.transcription.strip()) < 10:return jsonify({'error': 'No valid transcription available for summary generation'}), 400-+# Check if already processingif recording.status in ['PROCESSING', 'SUMMARIZING']:return jsonify({'error': 'Recording is already being processed'}), 400-+# Check if OpenRouter client is availableif client is None:return jsonify({'error': 'Summary service is not available (OpenRouter client not configured)'}), 503-+current_app.logger.info(f"Queueing summary generation for recording {recording_id}")# Queue summary generation job@@ -598,7 +601,7 @@ def generate_summary_endpoint(recording_id):'success': True,'message': 'Summary generation queued'})-+except Exception as e:db.session.rollback()current_app.logger.error(f"Error starting summary generation for recording {recording_id}: {e}")@@ -651,9 +654,9 @@ def update_speakers(recording_id):segment['speaker'] = new_nameif new_name not in speaker_names_used:speaker_names_used.append(new_name)-+recording.transcription = json.dumps(transcription_data)-+# Update participants only from speakers that were actually given names (not default labels)final_speakers = set()for seg in transcription_data:@@ -678,7 +681,7 @@ def update_speakers(recording_id):transcription_text = re.sub(r'\[\s*' + re.escape(speaker_label) + r'\s*\]', f'[{new_name}]', transcription_text, flags=re.IGNORECASE)if new_name not in new_participants:new_participants.append(new_name)-+recording.transcription = transcription_textif new_participants:recording.participants = ', '.join(new_participants)@@ -1149,15 +1152,15 @@ def reprocess_summary(recording_id):if not has_recording_access(recording, current_user, require_edit=True):return jsonify({'error': 'You do not have permission to reprocess this recording'}), 403-+# Check if transcription existsif not recording.transcription or len(recording.transcription.strip()) < 10:return jsonify({'error': 'No valid transcription available for summary generation'}), 400-+# Check if already processingif recording.status in ['PROCESSING', 'SUMMARIZING']:return jsonify({'error': 'Recording is already being processed'}), 400-+# Check if OpenRouter client is availableif client is None:return jsonify({'error': 'Summary service is not available (OpenRouter client not configured)'}), 503@@ -1206,7 +1209,7 @@ def reprocess_summary(recording_id):'message': 'Summary reprocessing started','recording': recording_dict})-+except Exception as e:db.session.rollback()current_app.logger.error(f"Error reprocessing summary for recording {recording_id}: {e}")@@ -1861,7 +1864,7 @@ def update_transcription(recording_id):# The incoming data could be a JSON string (from ASR edit) or plain textrecording.transcription = new_transcription-+# Optional: If the transcription changes, we might want to indicate that the summary is outdated.# For now, we'll just save the transcript. A "regenerate summary" button could be a good follow-up.@@ -1956,7 +1959,7 @@ def upload_file():# Check size limit before saving - only enforce if chunking is disabled or using ASR endpointmax_content_length = current_app.config.get('MAX_CONTENT_LENGTH')-+# Skip size check if chunking is enabled and using OpenAI Whisper APIshould_enforce_size_limit = Trueif ENABLE_CHUNKING and chunking_service and not USE_ASR_ENDPOINT:@@ -1967,7 +1970,7 @@ def upload_file():current_app.logger.info(f"Size-based chunking enabled ({limit_value}MB limit) - skipping {original_file_size/1024/1024:.1f}MB size limit check")else:current_app.logger.info(f"Duration-based chunking enabled ({limit_value}s limit) - 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()@@ -1976,92 +1979,52 @@ def upload_file():# --- Convert files only when chunking is needed ---filename_lower = original_filename.lower()-- # Check if chunking will be needed for this file- needs_chunking_for_processing = (chunking_service and- ENABLE_CHUNKING and- not USE_ASR_ENDPOINT and- chunking_service.needs_chunking(filepath, USE_ASR_ENDPOINT))-- # Define supported formats based on whether chunking is needed- if needs_chunking_for_processing:- # For chunking: only support formats that work well with chunking- supported_formats = ('.wav', '.mp3', '.flac')- convertible_formats = ('.amr', '.3gp', '.3gpp', '.m4a', '.aac', '.ogg', '.wma', '.webm', '.weba', '.mp4', '.mov', '.opus', '.caf', '.aiff', '.ts', '.mts', '.mkv', '.avi', '.m4v', '.wmv', '.flv', '.mpeg', '.mpg', '.ogv', '.vob', '.asf')- else:- # For direct transcription: support WebM and other formats directly- supported_formats = ('.wav', '.mp3', '.flac', '.webm', '.weba', '.m4a', '.aac', '.ogg')- convertible_formats = ('.amr', '.3gp', '.3gpp', '.wma', '.mp4', '.mov', '.opus', '.caf', '.aiff', '.ts', '.mts', '.mkv', '.avi', '.m4v', '.wmv', '.flv', '.mpeg', '.mpg', '.ogv', '.vob', '.asf')-- # Special handling for problematic AAC files when using ASR endpoint- is_problematic_aac = (USE_ASR_ENDPOINT and- (filename_lower.endswith('.aac') or- 'aac' in filename_lower.lower()))-- # Convert if file is not in supported formats OR is problematic AAC for ASR- # Also convert unknown formats proactively to avoid downstream failures- is_unknown_format = not filename_lower.endswith(supported_formats) and not filename_lower.endswith(convertible_formats)- should_convert = ((not filename_lower.endswith(supported_formats) and needs_chunking_for_processing) or- is_problematic_aac or- is_unknown_format)-- if should_convert:- if is_problematic_aac:- current_app.logger.info(f"Converting AAC-encoded file {filename_lower} to high-quality MP3 for ASR endpoint compatibility.")- elif is_unknown_format:- current_app.logger.info(f"Attempting to convert unknown format ({filename_lower}) to MP3 via FFmpeg.")- elif filename_lower.endswith(convertible_formats):- current_app.logger.info(f"Converting {filename_lower} format to high-quality MP3 for processing.")-- # Use compression settings if enabled, otherwise default to MP3 128k- codec = AUDIO_CODEC if AUDIO_COMPRESS_UPLOADS else 'mp3'- bitrate = AUDIO_BITRATE if AUDIO_COMPRESS_UPLOADS else '128k'- # Determine output extension based on codec- codec_extensions = {'mp3': '.mp3', 'flac': '.flac', 'opus': '.opus'}- output_ext = codec_extensions.get(codec, '.mp3')+ # Check if chunking will be needed for this file+ needs_chunking_for_processing = bool(+ chunking_service and+ ENABLE_CHUNKING and+ not USE_ASR_ENDPOINT and+ chunking_service.needs_chunking(filepath, USE_ASR_ENDPOINT)+ )- base_filepath, _ = os.path.splitext(filepath)- temp_filepath = f"{base_filepath}_temp{output_ext}"- final_filepath = f"{base_filepath}{output_ext}"+ # Probe once and use shared conversion utility+ codec_info = None+ try:+ codec_info = get_codec_info(filepath, timeout=10)+ current_app.logger.info(+ f"Detected codec for {original_filename}: "+ f"audio_codec={codec_info.get('audio_codec')}, "+ f"has_video={codec_info.get('has_video', False)}"+ )+ except FFProbeError as e:+ current_app.logger.warning(f"Failed to probe {original_filename}: {e}. Will attempt conversion.")+ codec_info = None- try:- # Build FFmpeg command based on codec- if codec == 'flac':- cmd = ['ffmpeg', '-i', filepath, '-y', '-acodec', 'flac', '-compression_level', '8', temp_filepath]- elif codec == 'opus':- cmd = ['ffmpeg', '-i', filepath, '-y', '-acodec', 'libopus', '-b:a', bitrate, temp_filepath]- else: # mp3 (default)- cmd = ['ffmpeg', '-i', filepath, '-y', '-acodec', 'libmp3lame', '-b:a', bitrate, '-ac', '1', temp_filepath]-- subprocess.run(cmd, check=True, capture_output=True, text=True)- current_app.logger.info(f"Successfully converted {filepath} to {temp_filepath} ({codec.upper()} {bitrate})")-- # If the original file is not the same as the final file, remove it- if filepath.lower() != final_filepath.lower():- os.remove(filepath)-- # Rename the temporary file to the final filename- os.rename(temp_filepath, final_filepath)-- filepath = final_filepath- except FileNotFoundError:- current_app.logger.error("ffmpeg command not found. Please ensure ffmpeg is installed and in the system's PATH.")- return jsonify({'error': 'Audio conversion tool (ffmpeg) not found on server.'}), 500- except subprocess.CalledProcessError as e:- current_app.logger.error(f"ffmpeg conversion failed for {filepath}: {e.stderr}")- return jsonify({'error': f'Failed to convert audio file: {e.stderr}'}), 500-- # Compress lossless audio files (WAV, AIFF) if enabled (for files that weren't already converted above)- if AUDIO_COMPRESS_UPLOADS:- try:- new_filepath, new_mime = compress_lossless_audio(filepath, AUDIO_CODEC, AUDIO_BITRATE)- if new_filepath != filepath:- filepath = new_filepath- # MIME type will be updated below- except Exception as e:- current_app.logger.warning(f"Audio compression failed, continuing with original: {e}")- # Don't fail the upload if compression fails - just continue with original+ # Use shared conversion utility - handles ALL conversion needs (codec conversion + compression)+ try:+ result = convert_if_needed(+ filepath,+ original_filename=original_filename,+ codec_info=codec_info,+ needs_chunking=needs_chunking_for_processing,+ is_asr_endpoint=USE_ASR_ENDPOINT,+ delete_original=True+ )+ filepath = result.output_path++ # Log what happened+ if result.was_converted:+ current_app.logger.info(f"File converted: {result.original_codec} -> {result.final_codec}")+ if result.was_compressed:+ current_app.logger.info(f"File compressed: {result.size_reduction_percent:.1f}% size reduction")++ except FFmpegNotFoundError as e:+ current_app.logger.error(f"FFmpeg not found: {e}")+ return jsonify({'error': 'Audio conversion tool (FFmpeg) not found on server.'}), 500+ except FFmpegError as e:+ current_app.logger.error(f"FFmpeg conversion failed for {filepath}: {e}")+ return jsonify({'error': f'Failed to convert audio file: {str(e)}'}), 500# Get final file size (of original or converted file)final_file_size = os.path.getsize(filepath)@@ -2072,7 +2035,7 @@ def upload_file():# Get notes from the formnotes = request.form.get('notes')-+# Get selected tags if provided (multiple tags support)selected_tags = []tag_index = 0@@ -2098,12 +2061,12 @@ def upload_file():tag = Tag.query.filter_by(id=single_tag_id).first()if tag and (tag.user_id == current_user.id or (tag.group_id and GroupMembership.query.filter_by(group_id=tag.group_id, user_id=current_user.id).first())):selected_tags.append(tag)-+# Get ASR advanced options if providedlanguage = request.form.get('language', '')min_speakers = request.form.get('min_speakers') or Nonemax_speakers = request.form.get('max_speakers') or None-+# Convert to int if providedif min_speakers:try:@@ -2115,9 +2078,9 @@ def upload_file():max_speakers = int(max_speakers)except (ValueError, TypeError):max_speakers = None-+# Apply precedence hierarchy: user input > tag defaults > environment variables > auto-detect-+# Apply tag defaults if tags are selected and values are not explicitly provided by user# Use first tag's defaults (highest priority)if selected_tags:@@ -2128,7 +2091,7 @@ def upload_file():min_speakers = first_tag.default_min_speakersif max_speakers is None and first_tag.default_max_speakers:max_speakers = first_tag.default_max_speakers-+# Apply environment variable defaults if still no values are setif min_speakers is None and ASR_MIN_SPEAKERS:try:@@ -2157,7 +2120,7 @@ def upload_file():)db.session.add(recording)db.session.commit()-+# Add tags to recording if selected (preserve order)for order, tag in enumerate(selected_tags, 1):new_association = RecordingTag(@@ -2167,12 +2130,12 @@ def upload_file():added_at=datetime.utcnow())db.session.add(new_association)-+if selected_tags:db.session.commit()tag_names = [tag.name for tag in selected_tags]current_app.logger.info(f"Added {len(selected_tags)} tags to recording {recording.id}: {', '.join(tag_names)}")-+current_app.logger.info(f"Initial recording record created with ID: {recording.id}")# --- Queue transcription job ---@@ -2219,7 +2182,7 @@ def delete_recording(recording_id):recording = db.session.get(Recording, recording_id)if not recording:return jsonify({'error': 'Recording not found'}), 404-+# Check if the recording belongs to the current userif recording.user_id and recording.user_id != current_user.id:return jsonify({'error': 'You do not have permission to delete this recording'}), 403@@ -3088,6 +3051,3 @@ def process_recording_chunks_endpoint(recording_id):# --- Inquire Mode API Endpoints ------diff --git a/src/api/speakers.py b/src/api/speakers.pyindex 9a8c4124..ed23511f 100644--- a/src/api/speakers.py+++ b/src/api/speakers.py@@ -15,6 +15,7 @@ from werkzeug.utils import secure_filenamefrom src.database import dbfrom src.models import *from src.utils import *+from src.utils.ffmpeg_utils import extract_audio_segment, FFmpegError, FFmpegNotFoundErrorfrom src.services.speaker_embedding_matcher import find_matching_speakersfrom src.services.speaker_snippets import get_speaker_snippets, get_speaker_recordings_with_snippetsfrom src.services.speaker_merge import merge_speakers, preview_merge, can_merge_speakers@@ -424,7 +425,6 @@ def get_snippet_audio(recording_id):Returns:Audio file segment in the original format"""- import subprocessimport tempfileimport osfrom pathlib import Path@@ -453,18 +453,13 @@ def get_snippet_audio(recording_id):output_path = tmp_file.nametry:- # Use FFmpeg to extract the audio segment- ffmpeg_cmd = [- 'ffmpeg',- '-i', recording.audio_path,- '-ss', str(start_time),- '-t', str(duration),- '-c', 'copy',- '-y',- output_path- ]-- subprocess.run(ffmpeg_cmd, check=True, capture_output=True)+ # Use centralized FFmpeg utility to extract the audio segment+ extract_audio_segment(+ recording.audio_path,+ output_path,+ start_time,+ duration+ )# Send the fileresponse = send_file(@@ -484,7 +479,14 @@ def get_snippet_audio(recording_id):return response- except subprocess.CalledProcessError as e:+ except FFmpegNotFoundError as e:+ current_app.logger.error(f"FFmpeg not found: {e}")+ try:+ os.unlink(output_path)+ except:+ pass+ return jsonify({'error': 'FFmpeg not found on server'}), 500+ except FFmpegError as e:current_app.logger.error(f"FFmpeg error extracting snippet: {e}")try:os.unlink(output_path)diff --git a/src/audio_chunking.py b/src/audio_chunking.pyindex 92bbe9cf..9e498434 100644--- a/src/audio_chunking.py+++ b/src/audio_chunking.py@@ -17,6 +17,8 @@ from typing import List, Dict, Any, Optional, Tuplefrom datetime import datetimeimport mimetypes+from src.utils.ffmpeg_utils import convert_to_mp3, FFmpegError, FFmpegNotFoundError+# Configure logginglogger = logging.getLogger(__name__)@@ -121,22 +123,10 @@ class AudioChunkingService:mp3_filename = f"{base_name}_converted.mp3"mp3_path = os.path.join(temp_dir, mp3_filename)- # Convert to high-quality MP3 for better transcription accuracy- cmd = [- 'ffmpeg', '-i', file_path,- '-codec:a', 'libmp3lame', # Use LAME MP3 encoder explicitly- '-b:a', '128k', # 128kbps bitrate for high quality- '-ar', '44100', # 44.1kHz sample rate for better quality- '-ac', '1', # Mono (sufficient for speech)- '-compression_level', '2', # Better compression- '-y', # Overwrite output file- mp3_path- ]-logger.info(f"Converting {file_path} to 128kbps MP3 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}")++ # Use centralized FFmpeg utility for conversion+ convert_to_mp3(file_path, mp3_path)if not os.path.exists(mp3_path):raise ValueError("MP3 file was not created")@@ -163,6 +153,9 @@ class AudioChunkingService:return mp3_path, mp3_duration, mp3_size+ except (FFmpegError, FFmpegNotFoundError) as e:+ logger.error(f"Error converting file to MP3: {e}")+ raiseexcept Exception as e:logger.error(f"Error converting file to MP3: {e}")raisediff --git a/src/file_monitor.py b/src/file_monitor.pyindex c265e8d7..dc78d7eb 100644--- a/src/file_monitor.py+++ b/src/file_monitor.py@@ -15,8 +15,10 @@ import loggingfrom datetime import datetimefrom pathlib import Pathimport mimetypes-import subprocessfrom werkzeug.utils import secure_filename+from src.utils.ffprobe import get_codec_info, FFProbeError+from src.utils.ffmpeg_utils import FFmpegError, FFmpegNotFoundError+from src.utils.audio_conversion import convert_if_needed# Flask app components will be imported inside functions to avoid circular imports@@ -44,9 +46,13 @@ class FileMonitor:self.logger.setLevel(logging.INFO)# Supported audio file extensions- self.supported_extensions = {+ # We'll use ffprobe to detect audio files instead of extensions+ # Keep a basic list for initial filtering to avoid probing every file+ self.potential_audio_extensions = {'.wav', '.mp3', '.flac', '.amr', '.3gp', '.3gpp',- '.m4a', '.aac', '.ogg', '.wma', '.webm', '.mp4', '.mov'+ '.m4a', '.aac', '.ogg', '.wma', '.webm', '.mp4', '.mov',+ '.opus', '.caf', '.aiff', '.ts', '.mts', '.mkv', '.avi',+ '.m4v', '.wmv', '.flv', '.mpeg', '.mpg', '.ogv', '.vob', '.asf'}# Cache for admin user and valid users@@ -170,7 +176,7 @@ class FileMonitor:if file_path.name.startswith('.') or file_path.suffix == '.processing':continue- if file_path.suffix.lower() not in self.supported_extensions:+ if file_path.suffix.lower() not in self.potential_audio_extensions:continue# Check if file is still being written (size stability check)@@ -309,8 +315,38 @@ class FileMonitor:# This should not happen if the lock is held, but good to logself.logger.warning(f"Locked file {processing_path} was already deleted.")- # Convert file if necessary (same logic as upload_file)- final_path = self._convert_file_if_needed(destination_path, original_filename)+ # Probe once to get codec info, then pass through pipeline to avoid redundant calls+ codec_info = None+ try:+ codec_info = get_codec_info(str(destination_path), timeout=10)+ self.logger.info(f"Detected codec for {original_filename}: audio_codec={codec_info.get('audio_codec')}, has_video={codec_info.get('has_video', False)}")+ except FFProbeError as e:+ self.logger.warning(f"Failed to probe {original_filename}: {e}. Will attempt conversion.")++ # Convert/compress file if necessary - convert_if_needed handles ALL conversion needs+ try:+ result = convert_if_needed(+ str(destination_path),+ original_filename=original_filename,+ codec_info=codec_info,+ needs_chunking=False,+ is_asr_endpoint=False,+ delete_original=True # Clean up original after conversion+ )+ final_path = Path(result.output_path)++ # Log what happened+ if result.was_converted:+ self.logger.info(f"File converted: {result.original_codec} -> {result.final_codec}")+ if result.was_compressed:+ self.logger.info(f"File compressed: {result.size_reduction_percent:.1f}% size reduction")++ except FFmpegNotFoundError as e:+ self.logger.error(f"FFmpeg not found: {e}")+ raise+ except FFmpegError as e:+ self.logger.error(f"FFmpeg conversion failed: {e}")+ raise# Get file size and MIME typefile_size = final_path.stat().st_size@@ -354,60 +390,7 @@ class FileMonitor:# Re-raise the exception to be caught by the calling method, which will handle unlocking.raise- def _convert_file_if_needed(self, file_path, original_filename):- """- Convert audio file to supported format if needed.- Uses 32kbps MP3 for optimal size/quality balance.-- Args:- file_path (Path): Current file path- original_filename (str): Original filename for format detection-- Returns:- Path: Path to the final (possibly converted) file- """- filename_lower = original_filename.lower()-- # Support WebM and other formats directly when possible- supported_formats = ('.wav', '.mp3', '.flac', '.webm', '.weba', '.m4a', '.aac', '.ogg')- convertible_formats = ('.amr', '.3gp', '.3gpp', '.wma', '.mp4', '.mov', '.opus', '.caf', '.aiff', '.ts', '.mts', '.mkv', '.avi', '.m4v', '.wmv', '.flv', '.mpeg', '.mpg', '.ogv', '.vob', '.asf')-- if filename_lower.endswith(supported_formats):- self.logger.info(f"File format {filename_lower} is supported, no conversion needed")- return file_path-- if not filename_lower.endswith(convertible_formats):- self.logger.warning(f"Unknown file format {filename_lower}, attempting conversion anyway")-- # Need to convert to high-quality MP3 for better transcription accuracy- self.logger.info(f"Converting {filename_lower} format to high-quality MP3")-- base_path = file_path.with_suffix('')- temp_mp3_path = base_path.with_suffix('.temp.mp3')- final_mp3_path = base_path.with_suffix('.mp3')-- try:- # Convert to high-quality MP3 (128kbps, 44.1kHz) for better transcription accuracy- subprocess.run([- 'ffmpeg', '-i', str(file_path), '-y',- '-acodec', 'libmp3lame', '-b:a', '128k', '-ar', '44100', '-ac', '1',- str(temp_mp3_path)- ], check=True, capture_output=True, text=True)-- self.logger.info(f"Successfully converted {file_path} to {temp_mp3_path} (128kbps MP3)")-- # Remove original and rename temp file- file_path.unlink()- temp_mp3_path.rename(final_mp3_path)-- return final_mp3_path-- except FileNotFoundError:- self.logger.error("ffmpeg not found. Please ensure ffmpeg is installed.")- raise- except subprocess.CalledProcessError as e:- self.logger.error(f"ffmpeg conversion failed: {e.stderr}")- raise+# Global file monitor instancediff --git a/src/tasks/processing.py b/src/tasks/processing.pyindex 2a945126..7b39657a 100644--- a/src/tasks/processing.py+++ b/src/tasks/processing.py@@ -25,6 +25,9 @@ from src.models import Recording, Tag, Event, TranscriptChunk, SystemSetting, Grfrom src.services.embeddings import process_recording_chunksfrom src.services.llm import is_using_openai_api, call_llm_completion, format_api_error_message, TEXT_MODEL_NAME, client, http_client_no_proxyfrom src.utils import extract_json_object, safe_json_loads+from src.utils.ffprobe import get_codec_info, is_video_file, is_lossless_audio, FFProbeError+from src.utils.ffmpeg_utils import convert_to_mp3, extract_audio_from_video as ffmpeg_extract_audio, compress_audio, FFmpegError, FFmpegNotFoundError+from src.config.app_config import AUDIO_COMPRESS_UPLOADS, AUDIO_CODEC, AUDIO_BITRATEfrom src.audio_chunking import AudioChunkingService, ChunkProcessingError, ChunkingNotSupportedErrorfrom src.config.app_config import ASR_DIARIZE, ASR_BASE_URL, ASR_RETURN_SPEAKER_EMBEDDINGS, transcription_api_key, transcription_base_url, chunking_service, ENABLE_CHUNKINGfrom src.file_exporter import export_recording, ENABLE_AUTO_EXPORT@@ -197,7 +200,7 @@ def generate_title_task(app_context, recording_id, will_auto_summarize=False):if not recording:current_app.logger.error(f"Error: Recording {recording_id} not found for title generation.")return-+if client is None:current_app.logger.warning(f"Skipping title generation for {recording_id}: OpenRouter client not configured.")# Only mark as completed if auto-summarization won't happen next@@ -215,25 +218,25 @@ def generate_title_task(app_context, recording_id, will_auto_summarize=False):recording.completed_at = datetime.utcnow()db.session.commit()return-+# Get configurable transcript length limit and format transcription for LLMtranscript_limit = SystemSetting.get_setting('transcript_length_limit', 30000)if transcript_limit == -1:raw_transcription = recording.transcriptionelse:raw_transcription = recording.transcription[:transcript_limit]-+# Convert ASR JSON to clean text formattranscript_text = format_transcription_for_llm(raw_transcription)--++# Get user language preferenceuser_output_language = Noneif recording.owner:user_output_language = recording.owner.output_language-+language_directive = f"Please provide the title in {user_output_language}." if user_output_language else ""-+prompt_text = f"""Create a short title for this conversation:{transcript_text}@@ -322,26 +325,26 @@ def generate_summary_only_task(app_context, recording_id, custom_prompt_overrideif not recording:current_app.logger.error(f"Error: Recording {recording_id} not found for summary generation.")return-+if client is None:current_app.logger.warning(f"Skipping summary generation for {recording_id}: OpenRouter client not configured.")recording.summary = "[Summary skipped: OpenRouter client not configured]"db.session.commit()return-+recording.status = 'SUMMARIZING'summarization_start_time = time.time()db.session.commit()current_app.logger.info(f"Requesting summary from OpenRouter for recording {recording_id} using model {TEXT_MODEL_NAME}...")-+if not recording.transcription or len(recording.transcription.strip()) < 10:current_app.logger.warning(f"Transcription for recording {recording_id} is too short or empty. Skipping summarization.")recording.summary = "[Summary skipped due to short transcription]"recording.status = 'COMPLETED'db.session.commit()return-+# Get user preferences and tag custom promptsuser_summary_prompt = Noneuser_output_language = None@@ -379,7 +382,7 @@ def generate_summary_only_task(app_context, recording_id, custom_prompt_overridecurrent_app.logger.info(f"Found custom prompt from tag '{tag.name}' for recording {recording_id}")else:current_app.logger.warning(f"No viewer user available for tag filtering on recording {recording_id}")-+# Create merged prompt if we have multiple tag promptsif tag_custom_prompts:if len(tag_custom_prompts) == 1:@@ -395,21 +398,21 @@ def generate_summary_only_task(app_context, recording_id, custom_prompt_overridecurrent_app.logger.info(f"Combined custom prompts from {len(tag_custom_prompts)} tags in order added ({', '.join(tag_names)}) for recording {recording_id}")else:tag_custom_prompt = None-+if recording.owner:user_summary_prompt = recording.owner.summary_promptuser_output_language = recording.owner.output_language-+# Format transcription for LLM (convert JSON to clean text format like clipboard copy)formatted_transcription = format_transcription_for_llm(recording.transcription)-+# Get configurable transcript length limittranscript_limit = SystemSetting.get_setting('transcript_length_limit', 30000)if transcript_limit == -1:transcript_text = formatted_transcriptionelse:transcript_text = formatted_transcription[:transcript_limit]-+language_directive = f"IMPORTANT: You MUST provide the summary in {user_output_language}. The entire response must be in {user_output_language}." if user_output_language else ""# Determine which summarization instructions to use@@ -819,63 +822,60 @@ You must respond with valid JSON format only."""def extract_audio_from_video(video_filepath, output_format='mp3', cleanup_original=True):"""Extract audio from video containers using FFmpeg.- Uses MP3 codec for optimal compatibility and predictable file sizes.- 64kbps MP3 provides good speech quality at ~480KB per minute.+ Behavior depends on AUDIO_COMPRESS_UPLOADS setting:+ - If enabled: Re-encodes to specified format (mp3/flac/opus) with configured bitrate+ - If disabled: Copies audio stream without re-encoding (fast, preserves quality)++ Args:+ video_filepath: Path to input video file+ output_format: Audio format ('mp3', 'wav', 'flac', 'copy'), default 'mp3'+ cleanup_original: If True, deletes original video after extraction++ Returns:+ tuple: (audio_filepath, mime_type)++ Raises:+ FFmpegError: If audio extraction fails+ FFmpegNotFoundError: If FFmpeg is not installed"""try:- # Generate output filename with audio extension- base_filepath, file_ext = os.path.splitext(video_filepath)- temp_audio_filepath = f"{base_filepath}_audio_temp.{output_format}"- final_audio_filepath = f"{base_filepath}_audio.{output_format}"-- current_app.logger.info(f"Extracting audio from video: {video_filepath} -> {temp_audio_filepath}")-- # Extract audio using FFmpeg - using high-quality MP3 for better transcription- subprocess.run([- 'ffmpeg', '-i', video_filepath, '-y',- '-vn', # No video- '-codec:a', 'libmp3lame', # Use LAME MP3 encoder explicitly- '-b:a', '128k', # 128kbps bitrate for high quality- '-ar', '44100', # 44.1kHz sample rate for better quality- '-ac', '1', # Mono (sufficient for speech, reduces file size)- '-compression_level', '2', # Better compression- temp_audio_filepath- ], check=True, capture_output=True, text=True)-- current_app.logger.info(f"Successfully extracted audio to {temp_audio_filepath}")-- # Optionally preserve temp file for debugging (set PRESERVE_TEMP_AUDIO=true in env)- if os.getenv('PRESERVE_TEMP_AUDIO', 'false').lower() == 'true':- import shutil- shutil.copy2(temp_audio_filepath, temp_audio_filepath.replace('_temp', '_debug'))- current_app.logger.info(f"Debug: Preserved temp audio file as {temp_audio_filepath.replace('_temp', '_debug')}")-- # Rename temp file to final filename- os.rename(temp_audio_filepath, final_audio_filepath)-- # Clean up original video file if requested- if cleanup_original:- try:- os.remove(video_filepath)- current_app.logger.info(f"Cleaned up original video file: {video_filepath}")- except Exception as e:- current_app.logger.warning(f"Failed to clean up original video file {video_filepath}: {str(e)}")-- return final_audio_filepath, f'audio/{output_format}'-- except subprocess.CalledProcessError as e:- current_app.logger.error(f"FFmpeg audio extraction failed for {video_filepath}: {e.stderr}")- raise Exception(f"Audio extraction failed: {e.stderr}")- except FileNotFoundError:- current_app.logger.error("FFmpeg command not found. Please ensure FFmpeg is installed and in the system's PATH.")+ # Determine whether to copy stream or re-encode based on compression settings+ if AUDIO_COMPRESS_UPLOADS:+ # Re-encode to configured codec+ current_app.logger.info(f"Extracting and compressing audio from video: {video_filepath} (codec: {AUDIO_CODEC})")+ audio_filepath, mime_type = ffmpeg_extract_audio(+ video_filepath,+ output_format=AUDIO_CODEC,+ bitrate=AUDIO_BITRATE,+ cleanup_original=cleanup_original,+ copy_stream=False+ )+ else:+ # Copy audio stream without re-encoding (fast, preserves quality)+ current_app.logger.info(f"Extracting audio from video (stream copy, no re-encoding): {video_filepath}")+ audio_filepath, mime_type = ffmpeg_extract_audio(+ video_filepath,+ output_format='copy', # Will auto-detect format+ cleanup_original=cleanup_original,+ copy_stream=True+ )++ current_app.logger.info(f"Successfully extracted audio to {audio_filepath}")+ return audio_filepath, mime_type++ except FFmpegNotFoundError as e:+ current_app.logger.error(str(e))raise Exception("Audio conversion tool (FFmpeg) not found on server.")+ except FFmpegError as e:+ current_app.logger.error(f"FFmpeg audio extraction failed for {video_filepath}: {str(e)}")+ raise Exception(f"Audio extraction failed: {str(e)}")except Exception as e:current_app.logger.error(f"Error extracting audio from {video_filepath}: {str(e)}")raise-def compress_lossless_audio(filepath, codec='mp3', bitrate='128k'):- """Compress lossless audio files (WAV, AIFF) to save storage.+def compress_lossless_audio(filepath, codec='mp3', bitrate='128k', codec_info=None):+ """Compress lossless audio files to save storage.Only compresses lossless formats - already-compressed formats are skippedto avoid quality degradation from re-encoding.@@ -884,23 +884,31 @@ def compress_lossless_audio(filepath, codec='mp3', bitrate='128k'):filepath: Path to the audio filecodec: Target codec - 'mp3', 'flac', or 'opus'bitrate: Bitrate for lossy codecs (ignored for FLAC)+ codec_info: Optional pre-fetched codec info to avoid redundant probe callsReturns:tuple: (new_filepath, new_mime_type) or (original_filepath, None) if skipped"""- # Lossless formats that benefit from compression- lossless_extensions = {'.wav', '.aiff', '.aif'}+ # Use codec detection to check if file is lossless+ try:+ if not is_lossless_audio(filepath, timeout=10, codec_info=codec_info):+ current_app.logger.debug(f"Skipping compression for {filepath} - not a lossless format")+ return filepath, None- ext = os.path.splitext(filepath)[1].lower()+ # Get current codec info (use provided or fetch)+ if codec_info is None:+ codec_info_result = get_codec_info(filepath, timeout=10)+ else:+ codec_info_result = codec_info+ current_codec = codec_info_result.get('audio_codec')- # Skip if not a lossless format- if ext not in lossless_extensions:- current_app.logger.debug(f"Skipping compression for {filepath} - not a lossless format")- return filepath, None+ # Skip if target is same as source (e.g., FLAC to FLAC when source is already FLAC)+ if current_codec == codec:+ current_app.logger.debug(f"Skipping compression for {filepath} - already in target codec")+ return filepath, None- # Skip if target is same as source (e.g., FLAC to FLAC when source is already FLAC)- if ext == '.flac' and codec == 'flac':- current_app.logger.debug(f"Skipping compression for {filepath} - already FLAC")+ except FFProbeError as e:+ current_app.logger.warning(f"Failed to probe {filepath} for compression: {e}. Skipping compression.")return filepath, None# Determine output extension and MIME type@@ -925,62 +933,27 @@ def compress_lossless_audio(filepath, codec='mp3', bitrate='128k'):# Get original file size for loggingoriginal_size = os.path.getsize(filepath)- # Build FFmpeg command based on codec- if codec == 'mp3':- cmd = [- 'ffmpeg', '-i', filepath, '-y',- '-acodec', 'libmp3lame',- '-b:a', bitrate,- '-ac', '1', # Mono for speech- temp_filepath- ]- elif codec == 'flac':- cmd = [- 'ffmpeg', '-i', filepath, '-y',- '-acodec', 'flac',- '-compression_level', '8', # Maximum compression- temp_filepath- ]- elif codec == 'opus':- cmd = [- 'ffmpeg', '-i', filepath, '-y',- '-acodec', 'libopus',- '-b:a', bitrate,- temp_filepath- ]-current_app.logger.info(f"Compressing {filepath} to {codec.upper()}...")- subprocess.run(cmd, check=True, capture_output=True, text=True)-- # Get compressed file size- compressed_size = os.path.getsize(temp_filepath)- ratio = (1 - compressed_size / original_size) * 100 if original_size > 0 else 0- current_app.logger.info(- f"Compressed {filepath}: {original_size / 1024 / 1024:.1f}MB -> "- f"{compressed_size / 1024 / 1024:.1f}MB ({ratio:.1f}% reduction)"+ # Use centralized compression utility+ final_filepath, output_mime, _ = compress_audio(+ filepath,+ codec=codec,+ bitrate=bitrate,+ delete_original=True,+ codec_info=None)- # Remove original and rename temp to final- os.remove(filepath)- os.rename(temp_filepath, final_filepath)-return final_filepath, output_mime- except subprocess.CalledProcessError as e:- current_app.logger.error(f"FFmpeg compression failed for {filepath}: {e.stderr}")- # Clean up temp file if it exists- if os.path.exists(temp_filepath):- os.remove(temp_filepath)- raise Exception(f"Audio compression failed: {e.stderr}")- except FileNotFoundError:- current_app.logger.error("FFmpeg command not found for compression.")+ except FFmpegNotFoundError as e:+ current_app.logger.error(str(e))raise Exception("Audio conversion tool (FFmpeg) not found on server.")+ except FFmpegError as e:+ current_app.logger.error(f"FFmpeg compression failed for {filepath}: {str(e)}")+ raise Exception(f"Audio compression failed: {str(e)}")except Exception as e:current_app.logger.error(f"Error compressing audio {filepath}: {str(e)}")- # Clean up temp file if it exists- if os.path.exists(temp_filepath):- os.remove(temp_filepath)raise@@ -1003,35 +976,39 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,actual_content_type = mime_type or mimetypes.guess_type(original_filename)[0] or 'application/octet-stream'actual_filename = original_filename- # List of video MIME types that need audio extraction- video_mime_types = [- 'video/mp4', 'video/quicktime', 'video/x-msvideo', 'video/webm',- 'video/avi', 'video/x-ms-wmv', 'video/3gpp'- ]-- # Check if file is a video container by MIME type or extension- is_video = (- actual_content_type.startswith('video/') or- actual_content_type in video_mime_types or- original_filename.lower().endswith(('.mp4', '.mov', '.avi', '.mkv', '.webm', '.wmv', '.3gp'))- )-+ # Use codec detection to check if file is a video+ try:+ is_video = is_video_file(filepath, timeout=10)+ if is_video:+ current_app.logger.info(f"Video detected via codec analysis for {original_filename}")+ except FFProbeError as e:+ current_app.logger.warning(f"Failed to probe {original_filename}: {e}. Falling back to MIME type detection.")+ # Fallback to MIME type detection+ video_mime_types = [+ 'video/mp4', 'video/quicktime', 'video/x-msvideo', 'video/webm',+ 'video/avi', 'video/x-ms-wmv', 'video/3gpp'+ ]+ is_video = (+ actual_content_type.startswith('video/') or+ actual_content_type in video_mime_types+ )+if is_video:current_app.logger.info(f"Video container detected ({actual_content_type}), extracting audio...")try:- # Extract audio from video- audio_filepath, audio_mime_type = extract_audio_from_video(filepath, 'mp3')-+ # Extract audio from video (uses compression settings)+ audio_filepath, audio_mime_type = extract_audio_from_video(filepath)+# Update paths and MIME type for ASR processingactual_filepath = audio_filepathactual_content_type = audio_mime_typeactual_filename = os.path.basename(audio_filepath)-+# Update recording with extracted audio path and new MIME typerecording.audio_path = audio_filepathrecording.mime_type = audio_mime_typedb.session.commit()-+current_app.logger.info(f"Audio extracted successfully: {audio_filepath}")except Exception as e:current_app.logger.error(f"Failed to extract audio from video: {str(e)}")@@ -1040,19 +1017,18 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,db.session.commit()return- # Keep track of whether we've already tried WAV conversion- wav_conversion_attempted = False- wav_converted_filepath = None-+ # Keep track of converted filepath for retry logic+ converted_filepath = None+# Retry loop for handling 500 errors with WAV conversionmax_attempts = 2for attempt in range(max_attempts):try:# Use converted MP3 if available from previous attempt- current_filepath = wav_converted_filepath if wav_converted_filepath else actual_filepath- current_content_type = 'audio/mpeg' if wav_converted_filepath else actual_content_type+ current_filepath = converted_filepath if converted_filepath else actual_filepath+ current_content_type = 'audio/mpeg' if converted_filepath else actual_content_typecurrent_filename = os.path.basename(current_filepath)-+with open(current_filepath, 'rb') as audio_file:url = f"{ASR_BASE_URL}/asr"params = {@@ -1079,7 +1055,7 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,content_type = current_content_typecurrent_app.logger.info(f"Using MIME type {content_type} for ASR upload.")files = {'audio_file': (current_filename, audio_file, content_type)}-+with httpx.Client() as client:# Get configurable ASR timeout from database (default 30 minutes)asr_timeout_seconds = SystemSetting.get_setting('asr_timeout_seconds', 1800)@@ -1113,80 +1089,80 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,# If we reach here, the request was successfulbreak-+except httpx.HTTPStatusError as e:- # Check if it's a 500 error and we haven't tried WAV conversion yet- if e.response.status_code == 500 and attempt == 0 and not wav_conversion_attempted:+ # Check if it's a 500 error and we haven't tried MP3 conversion yet+ if e.response.status_code == 500 and attempt == 0 and not converted_filepath:current_app.logger.warning(f"ASR returned 500 error for recording {recording_id}, attempting high-quality MP3 conversion and retry...")-- # Convert to high-quality MP3 for better compatibility- filename_lower = actual_filename.lower()- if not filename_lower.endswith('.mp3'):++ # Check if file is already MP3 using codec detection+ try:+ codec_info = get_codec_info(actual_filepath, timeout=10)+ audio_codec = codec_info.get('audio_codec')+ needs_conversion = audio_codec != 'mp3'+ except FFProbeError:+ # Fallback to extension check if probe fails+ needs_conversion = not actual_filename.lower().endswith('.mp3')++ if needs_conversion:try:- base_filepath, file_ext = os.path.splitext(actual_filepath)- temp_mp3_filepath = f"{base_filepath}_temp.mp3"-current_app.logger.info(f"Converting {actual_filename} to high-quality MP3 format for retry...")- subprocess.run(- ['ffmpeg', '-i', actual_filepath, '-y', '-acodec', 'libmp3lame', '-b:a', '128k', '-ar', '44100', temp_mp3_filepath],- check=True, capture_output=True, text=True- )- current_app.logger.info(f"Successfully converted {actual_filepath} to {temp_mp3_filepath}")-- wav_converted_filepath = temp_mp3_filepath # Keep variable name for compatibility- wav_conversion_attempted = True- # Continue to next iteration to retry with WAV+ temp_mp3_filepath = convert_to_mp3(actual_filepath)+ current_app.logger.info(f"Successfully converted to MP3: {temp_mp3_filepath}")++ converted_filepath = temp_mp3_filepath+ # Continue to next iteration to retry with MP3continue- except subprocess.CalledProcessError as conv_error:- current_app.logger.error(f"Failed to convert to WAV: {conv_error}")+ except (FFmpegError, FFmpegNotFoundError) as conv_error:+ current_app.logger.error(f"Failed to convert to MP3: {conv_error}")# Re-raise the original HTTP error if conversion failsraise eelse:- # Already a WAV file, can't convert further- current_app.logger.error(f"File is already WAV but still getting 500 error")+ # Already an MP3 file, can't convert further+ current_app.logger.error(f"File is already MP3 but still getting 500 error")raise eelse:# Not a 500 error or already tried conversion, propagate the errorraise e-- # DEBUG: Preserve converted file for quality checking- if wav_converted_filepath and os.path.exists(wav_converted_filepath):- try:- # Get file size and basic info for debugging- converted_size = os.path.getsize(wav_converted_filepath)- converted_size_mb = converted_size / (1024 * 1024)-- # Create a debug copy in a known location- debug_dir = os.path.join(current_app.config['UPLOAD_FOLDER'], 'debug_converted')- os.makedirs(debug_dir, exist_ok=True)-- # Copy the converted file with a timestamp (MP3 now)- from shutil import copy2- file_ext = os.path.splitext(wav_converted_filepath)[1] or '.mp3'- debug_filename = f"debug_{recording_id}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}{file_ext}"- debug_filepath = os.path.join(debug_dir, debug_filename)- copy2(wav_converted_filepath, debug_filepath)-- current_app.logger.info(f"DEBUG: Converted file preserved at: {debug_filepath}")- current_app.logger.info(f"DEBUG: Converted file size: {converted_size_mb:.2f} MB ({converted_size} bytes)")- current_app.logger.info(f"DEBUG: Original file: {actual_filename}")- current_app.logger.info(f"DEBUG: Recording ID: {recording_id}")- current_app.logger.info(f"DEBUG: You can download this file from the container at: {debug_filepath}")-- except Exception as debug_error:- current_app.logger.warning(f"DEBUG: Failed to preserve converted file: {debug_error}")-- # Clean up the original temporary converted file (but keep debug copy)++ # Optional: Preserve converted file for debugging+ if os.getenv('PRESERVE_DEBUG_CONVERSIONS', 'false').lower() == 'true':+ if converted_filepath and os.path.exists(converted_filepath):+ try:+ # Get file size and basic info for debugging+ converted_size = os.path.getsize(converted_filepath)+ converted_size_mb = converted_size / (1024 * 1024)++ # Create a debug copy in a known location+ debug_dir = os.path.join(current_app.config['UPLOAD_FOLDER'], 'debug_converted')+ os.makedirs(debug_dir, exist_ok=True)++ # Copy the converted file with a timestamp+ from shutil import copy2+ file_ext = os.path.splitext(converted_filepath)[1] or '.mp3'+ debug_filename = f"debug_{recording_id}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}{file_ext}"+ debug_filepath = os.path.join(debug_dir, debug_filename)+ copy2(converted_filepath, debug_filepath)++ current_app.logger.info(f"DEBUG: Converted file preserved at: {debug_filepath}")+ current_app.logger.info(f"DEBUG: Converted file size: {converted_size_mb:.2f} MB ({converted_size} bytes)")+ current_app.logger.info(f"DEBUG: Original file: {actual_filename}")+ current_app.logger.info(f"DEBUG: Recording ID: {recording_id}")++ except Exception as debug_error:+ current_app.logger.warning(f"DEBUG: Failed to preserve converted file: {debug_error}")++ # Clean up the temporary converted filetry:- if wav_converted_filepath and os.path.exists(wav_converted_filepath):- os.remove(wav_converted_filepath)- current_app.logger.info(f"Cleaned up original temporary converted file: {wav_converted_filepath}")+ if converted_filepath and os.path.exists(converted_filepath):+ os.remove(converted_filepath)+ current_app.logger.info(f"Cleaned up temporary converted file: {converted_filepath}")except Exception as cleanup_error:current_app.logger.warning(f"Failed to clean up temporary converted file: {cleanup_error}")-+# Debug logging for ASR responsecurrent_app.logger.info(f"ASR response keys: {list(asr_response_data.keys())}")-+# Log the complete raw JSON response (truncated for readability)import json as json_moduleraw_json_str = json_module.dumps(asr_response_data, indent=2)@@ -1194,40 +1170,40 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,current_app.logger.info(f"Raw ASR response (first 5000 chars): {raw_json_str[:5000]}...")else:current_app.logger.info(f"Raw ASR response: {raw_json_str}")-+if 'segments' in asr_response_data:current_app.logger.info(f"Number of segments: {len(asr_response_data['segments'])}")-+# Collect all unique speakers from the responseall_speakers = set()segments_with_speakers = 0segments_without_speakers = 0-+for segment in asr_response_data['segments']:if 'speaker' in segment and segment['speaker'] is not None:all_speakers.add(segment['speaker'])segments_with_speakers += 1else:segments_without_speakers += 1-+current_app.logger.info(f"Unique speakers found in raw response: {sorted(list(all_speakers))}")current_app.logger.info(f"Segments with speakers: {segments_with_speakers}, without speakers: {segments_without_speakers}")-+# Log first few segments for debuggingfor i, segment in enumerate(asr_response_data['segments'][:5]):segment_keys = list(segment.keys())current_app.logger.info(f"Segment {i} keys: {segment_keys}")current_app.logger.info(f"Segment {i}: speaker='{segment.get('speaker')}', text='{segment.get('text', '')[:50]}...'")-+# Simplify the JSON datasimplified_segments = []if 'segments' in asr_response_data and isinstance(asr_response_data['segments'], list):last_known_speaker = None-+for i, segment in enumerate(asr_response_data['segments']):speaker = segment.get('speaker')text = segment.get('text', '').strip()-+# If segment doesn't have a speaker, use the previous segment's speakerif speaker is None:if last_known_speaker is not None:@@ -1239,23 +1215,23 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,else:# Update the last known speaker when we have a valid onelast_known_speaker = speaker-+simplified_segments.append({'speaker': speaker,'sentence': text,'start_time': segment.get('start'),'end_time': segment.get('end')})-+# Log final simplified segments countcurrent_app.logger.info(f"Created {len(simplified_segments)} simplified segments")null_speaker_count = sum(1 for seg in simplified_segments if seg['speaker'] is None)if null_speaker_count > 0:current_app.logger.warning(f"Found {null_speaker_count} segments with null speakers in final output")-+# Store the simplified JSON as a stringrecording.transcription = json.dumps(simplified_segments)-+# Commit the transcription datadb.session.commit()@@ -1305,7 +1281,7 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,user_error_msg = f"ASR processing timed out. Error: {error_msg}"else:user_error_msg = f"ASR processing failed: {error_msg}"-+recording = db.session.get(Recording, recording_id)if recording:recording.status = 'FAILED'@@ -1316,7 +1292,7 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr, start_time, language=None, min_speakers=None, max_speakers=None, tag_id=None):"""Runs the transcription and summarization in a background thread.-+Args:app_context: Flask app contextrecording_id: ID of the recording to process@@ -1339,7 +1315,7 @@ def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr,diarize_setting = ASR_DIARIZEelse:diarize_setting = recording.owner.diarize if recording.owner else False-+# Use language from upload form if provided, otherwise use user's defaultif language:user_transcription_language = language@@ -1349,15 +1325,15 @@ def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr,# If None, ASR will auto-detect the number of speakersfinal_min_speakers = min_speakersfinal_max_speakers = max_speakers-- transcribe_audio_asr(app_context, recording_id, filepath, filename_for_asr, start_time,- mime_type=recording.mime_type,- language=user_transcription_language,++ transcribe_audio_asr(app_context, recording_id, filepath, filename_for_asr, start_time,+ mime_type=recording.mime_type,+ language=user_transcription_language,diarize=diarize_setting,min_speakers=final_min_speakers,max_speakers=final_max_speakers,tag_id=tag_id)-+# After ASR task completes, calculate processing timewith app_context:recording = db.session.get(Recording, recording_id)@@ -1380,17 +1356,17 @@ def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr,db.session.commit()# Check if chunking is needed for large files- needs_chunking = (chunking_service and- ENABLE_CHUNKING and+ needs_chunking = (chunking_service and+ ENABLE_CHUNKING andchunking_service.needs_chunking(filepath, USE_ASR_ENDPOINT))-+if needs_chunking:current_app.logger.info(f"File {filepath} is large ({os.path.getsize(filepath)/1024/1024:.1f}MB), using chunking for transcription")transcription_text = transcribe_with_chunking(app_context, recording_id, filepath, filename_for_asr)else:# --- Standard transcription for smaller files ---transcription_text = transcribe_single_file(filepath, recording)-+recording.transcription = transcription_text# Calculate and save transcription duration@@ -1437,7 +1413,7 @@ def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr,# Add error note to summary if appropriate stage was reachedif recording.status == 'SUMMARIZING' and not recording.summary:recording.summary = f"[Processing failed during summarization: {str(e)}]"-+end_time = datetime.utcnow()recording.processing_time_seconds = (end_time - start_time).total_seconds()db.session.commit()@@ -1446,32 +1422,37 @@ def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr,def transcribe_single_file(filepath, recording):"""Transcribe a single audio file using OpenAI Whisper API."""-+# Check if we need to extract audio from video containeractual_filepath = filepathmime_type = recording.mime_type if recording else None-- # Detect video containers- is_video = False- if mime_type:- is_video = mime_type.startswith('video/')- else:- # Fallback to extension-based detection- is_video = filepath.lower().endswith(('.mp4', '.mov', '.avi', '.mkv', '.webm', '.wmv', '.3gp'))-++ # Use codec detection to check if file is a video+ try:+ is_video = is_video_file(filepath, timeout=10)+ if is_video:+ current_app.logger.info(f"Video detected via codec analysis for {filepath}")+ except FFProbeError as e:+ current_app.logger.warning(f"Failed to probe {filepath}: {e}. Falling back to MIME type detection.")+ # Fallback to MIME type detection+ if mime_type:+ is_video = mime_type.startswith('video/')+ else:+ is_video = False+if is_video:current_app.logger.info(f"Video container detected for Whisper transcription, extracting audio...")try:- # Extract audio from video- audio_filepath, audio_mime_type = extract_audio_from_video(filepath, 'wav')+ # Extract audio from video (uses compression settings)+ audio_filepath, audio_mime_type = extract_audio_from_video(filepath)actual_filepath = audio_filepath-+# Update recording with extracted audio path and new MIME type if recording existsif recording:recording.audio_path = audio_filepathrecording.mime_type = audio_mime_typedb.session.commit()-+current_app.logger.info(f"Audio extracted successfully for Whisper: {audio_filepath}")except Exception as e:current_app.logger.error(f"Failed to extract audio from video for Whisper: {str(e)}")@@ -1480,14 +1461,17 @@ def transcribe_single_file(filepath, recording):recording.error_msg = f"Audio extraction failed: {str(e)}"db.session.commit()raise Exception(f"Audio extraction failed: {str(e)}")-+# List of formats supported by Whisper APIWHISPER_SUPPORTED_FORMATS = ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm']-- # Check if the file format needs conversion- file_ext = os.path.splitext(actual_filepath)[1].lower().lstrip('.')- converted_filepath = None-++ # Get user transcription language preference+ user_transcription_language = None+ if recording and recording.owner:+ user_transcription_language = recording.owner.transcription_language++ transcription_language = user_transcription_language+try:with open(actual_filepath, 'rb') as audio_file:transcription_client = OpenAI(@@ -1496,12 +1480,6 @@ def transcribe_single_file(filepath, recording):http_client=http_client_no_proxy)whisper_model = os.environ.get("WHISPER_MODEL", "Systran/faster-distil-whisper-large-v3")-- user_transcription_language = None- if recording and recording.owner:- user_transcription_language = recording.owner.transcription_language-- transcription_language = user_transcription_languagetranscription_params = {"model": whisper_model,@@ -1516,37 +1494,29 @@ def transcribe_single_file(filepath, recording):transcript = transcription_client.audio.transcriptions.create(**transcription_params)return transcript.text-+except Exception as e:# Check if it's a format errorerror_message = str(e)if "Invalid file format" in error_message or "Supported formats" in error_message:+ file_ext = os.path.splitext(actual_filepath)[1].lower().lstrip('.')current_app.logger.warning(f"Unsupported audio format '{file_ext}' detected, converting to MP3...")-- # Convert to MP3- import tempfile++ # Convert to MP3 using centralized utilitytemp_mp3_filepath = Nonetry:- with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_mp3:- temp_mp3_filepath = temp_mp3.name-- # Use ffmpeg to convert to MP3 with consistent settings- subprocess.run(- ['ffmpeg', '-i', actual_filepath, '-y', '-acodec', 'libmp3lame', '-b:a', '128k', '-ar', '44100', temp_mp3_filepath],- check=True,- capture_output=True- )+ temp_mp3_filepath = convert_to_mp3(actual_filepath)current_app.logger.info(f"Successfully converted {actual_filepath} to MP3 format")- converted_filepath = temp_mp3_filepath-+# Retry transcription with converted file- with open(converted_filepath, 'rb') as audio_file:+ with open(temp_mp3_filepath, 'rb') as audio_file:transcription_client = OpenAI(api_key=transcription_api_key,base_url=transcription_base_url,http_client=http_client_no_proxy)-++ whisper_model = os.environ.get("WHISPER_MODEL", "Systran/faster-distil-whisper-large-v3")transcription_params = {"model": whisper_model,"file": audio_file@@ -1557,15 +1527,18 @@ def transcribe_single_file(filepath, recording):transcript = transcription_client.audio.transcriptions.create(**transcription_params)return transcript.text-++ except (FFmpegError, FFmpegNotFoundError) as conv_error:+ current_app.logger.error(f"Failed to convert audio format: {conv_error}")+ raise Exception(f"Audio format conversion failed: {str(conv_error)}")finally:# Clean up temporary converted file- if converted_filepath and os.path.exists(converted_filepath):+ if temp_mp3_filepath and os.path.exists(temp_mp3_filepath):try:- os.unlink(converted_filepath)- current_app.logger.info(f"Cleaned up temporary converted file: {converted_filepath}")+ os.unlink(temp_mp3_filepath)+ current_app.logger.info(f"Cleaned up temporary converted file: {temp_mp3_filepath}")except Exception as cleanup_error:- current_app.logger.warning(f"Failed to clean up temporary file {converted_filepath}: {cleanup_error}")+ current_app.logger.warning(f"Failed to clean up temporary file {temp_mp3_filepath}: {cleanup_error}")else:# Re-raise if it's not a format errorraise@@ -1575,27 +1548,27 @@ def transcribe_single_file(filepath, recording):def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_asr):"""Transcribe a large audio file using chunking."""import tempfile-+with app_context:recording = db.session.get(Recording, recording_id)if not recording:raise ValueError(f"Recording {recording_id} not found")-+# Create temporary directory for chunkswith tempfile.TemporaryDirectory() as temp_dir:try:# Create chunkscurrent_app.logger.info(f"Creating chunks for large file: {filepath}")chunks = chunking_service.create_chunks(filepath, temp_dir)-+if not chunks:raise ChunkProcessingError("No chunks were created from the audio file")-+current_app.logger.info(f"Created {len(chunks)} chunks, processing each with Whisper API...")-+# Process each chunk with proper timeout and retry handlingchunk_results = []-+# Create HTTP client with proper timeoutstimeout_config = httpx.Timeout(connect=30.0, # 30 seconds to establish connection@@ -1603,13 +1576,13 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_awrite=60.0, # 1 minute to write requestpool=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,@@ -1618,54 +1591,54 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_atimeout=300.0 # 5 minute timeout for API calls)whisper_model = os.environ.get("WHISPER_MODEL", "Systran/faster-distil-whisper-large-v3")-+# Get user language preferenceuser_transcription_language = Nonewith app_context:recording = db.session.get(Recording, recording_id)if recording and recording.owner:user_transcription_language = recording.owner.transcription_language-+for i, chunk in enumerate(chunks):max_chunk_retries = 3chunk_retry_count = 0chunk_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 ""current_app.logger.info(f"Processing chunk {i+1}/{len(chunks)}: {chunk['filename']} ({chunk['size_mb']:.1f}MB){retry_suffix}")-+# Log detailed timing for each stepstep_start_time = time.time()-+# Step 1: File openingfile_open_start = time.time()with open(chunk['path'], 'rb') as chunk_file:file_open_time = time.time() - file_open_startcurrent_app.logger.info(f"Chunk {i+1}: File opened in {file_open_time:.2f}s")-+# Step 2: Prepare transcription parametersparam_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_startcurrent_app.logger.info(f"Chunk {i+1}: Parameters prepared in {param_time:.2f}s")-+# Step 3: API call with detailed timingapi_start = time.time()current_app.logger.info(f"Chunk {i+1}: Starting API call to {transcription_base_url}")-+# Log connection detailscurrent_app.logger.info(f"Chunk {i+1}: Using timeout config - connect: 30s, read: 300s, write: 60s")current_app.logger.info(f"Chunk {i+1}: Max retries: 2, API timeout: 300s")-+try:transcript = transcription_client.audio.transcriptions.create(**transcription_params)except Exception as chunk_error:@@ -1674,27 +1647,24 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_aif "Invalid file format" in error_msg or "Supported formats" in error_msg:current_app.logger.warning(f"Chunk {i+1} format issue, attempting conversion...")# Convert chunk to MP3 if needed- import tempfile- with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_mp3:- temp_mp3_path = temp_mp3.name+ temp_mp3_path = Nonetry:- subprocess.run(- ['ffmpeg', '-i', chunk['path'], '-y', '-acodec', 'libmp3lame', '-b:a', '128k', '-ar', '44100', temp_mp3_path],- check=True,- capture_output=True- )+ temp_mp3_path = convert_to_mp3(chunk['path'])with open(temp_mp3_path, 'rb') as converted_chunk:transcription_params['file'] = converted_chunktranscript = transcription_client.audio.transcriptions.create(**transcription_params)+ except (FFmpegError, FFmpegNotFoundError) as conv_error:+ current_app.logger.error(f"Failed to convert chunk {i+1}: {conv_error}")+ raise chunk_errorfinally:- if os.path.exists(temp_mp3_path):+ if temp_mp3_path and os.path.exists(temp_mp3_path):os.unlink(temp_mp3_path)else:raise-+api_time = time.time() - api_startcurrent_app.logger.info(f"Chunk {i+1}: API call completed in {api_time:.2f}s")-+# Step 4: Process responseresponse_start = time.time()chunk_result = {@@ -1709,17 +1679,17 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_a}chunk_results.append(chunk_result)response_time = time.time() - response_start-+total_time = time.time() - step_start_timecurrent_app.logger.info(f"Chunk {i+1}: Response processed in {response_time:.2f}s")current_app.logger.info(f"Chunk {i+1}: Total processing time: {total_time:.2f}s")current_app.logger.info(f"Chunk {i+1} transcribed successfully: {len(transcript.text)} characters")chunk_success = True-+except Exception as chunk_error:chunk_retry_count += 1error_msg = str(chunk_error)-+if chunk_retry_count < max_chunk_retries:# Determine wait time based on error typeif "timeout" in error_msg.lower() or "timed out" in error_msg.lower():@@ -1728,7 +1698,7 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_await_time = 60 # 1 minute for rate limit errorselse:wait_time = 15 # 15 seconds for other errors-+current_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:@@ -1742,21 +1712,21 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_a'filename': chunk['filename']}chunk_results.append(chunk_result)-+# Add small delay between chunks to avoid overwhelming the APIif i < len(chunks) - 1: # Don't delay after the last chunktime.sleep(2)-+# Merge transcriptionscurrent_app.logger.info(f"Merging {len(chunk_results)} chunk transcriptions...")merged_transcription = chunking_service.merge_transcriptions(chunk_results)-+if not merged_transcription.strip():raise ChunkProcessingError("Merged transcription is empty")-+# Log detailed performance statistics and analysischunking_service.log_processing_statistics(chunk_results)-+# Get performance recommendationsrecommendations = chunking_service.get_performance_recommendations(chunk_results)if recommendations:@@ -1764,10 +1734,10 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_afor i, rec in enumerate(recommendations, 1):current_app.logger.info(f"{i}. {rec}")current_app.logger.info("=== END RECOMMENDATIONS ===")-+current_app.logger.info(f"Chunked transcription completed. Final length: {len(merged_transcription)} characters")return merged_transcription-+except Exception as e:current_app.logger.error(f"Chunking transcription failed for {filepath}: {e}")# Clean up chunks if they exist@@ -1777,6 +1747,3 @@ def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_afinally:# Cleanup is handled by tempfile.TemporaryDirectory context managerpass---diff --git a/src/utils/audio_conversion.py b/src/utils/audio_conversion.pynew file mode 100644index 00000000..343de38f--- /dev/null+++ b/src/utils/audio_conversion.py@@ -0,0 +1,333 @@+"""+Audio conversion utility for handling codec detection and file conversion.++This module provides a single, unified interface for handling ALL audio/video+conversion needs:+- Video to audio extraction+- Unsupported codec conversion+- Lossless audio compression++Callers should ONLY use convert_if_needed() - it handles everything.+"""++import os+import logging+from pathlib import Path+from typing import Optional, Tuple, Set, Dict, Any++from src.utils.ffprobe import get_codec_info, is_lossless_audio, FFProbeError+from src.utils.ffmpeg_utils import compress_audio, extract_audio_from_video, FFmpegError, FFmpegNotFoundError+from src.config.app_config import AUDIO_COMPRESS_UPLOADS, AUDIO_CODEC, AUDIO_BITRATE++logger = logging.getLogger(__name__)+++class ConversionResult:+ """Result of a conversion operation."""++ def __init__(+ self,+ output_path: str,+ mime_type: str,+ was_converted: bool,+ was_compressed: bool,+ original_size: int,+ final_size: int,+ original_codec: Optional[str] = None,+ final_codec: Optional[str] = None+ ):+ self.output_path = output_path+ self.mime_type = mime_type+ self.was_converted = was_converted+ self.was_compressed = was_compressed+ self.original_size = original_size+ self.final_size = final_size+ self.original_codec = original_codec+ self.final_codec = final_codec++ @property+ def size_reduction_percent(self) -> float:+ """Calculate size reduction percentage."""+ if self.original_size == 0:+ return 0.0+ return ((self.original_size - self.final_size) / self.original_size) * 100++ @property+ def original_size_mb(self) -> float:+ """Original size in megabytes."""+ return self.original_size / (1024 * 1024)++ @property+ def final_size_mb(self) -> float:+ """Final size in megabytes."""+ return self.final_size / (1024 * 1024)+++def get_supported_codecs(needs_chunking: bool = False) -> Set[str]:+ """+ Get the set of supported audio codecs.++ Args:+ needs_chunking: If True, return only codecs that work well with chunking++ Returns:+ Set of supported codec names+ """+ if needs_chunking:+ # For chunking: only support codecs that work well with chunking+ return {'pcm_s16le', 'pcm_s24le', 'pcm_f32le', 'mp3', 'flac'}+ else:+ # For direct transcription: support more codecs including WebM/Opus+ return {'pcm_s16le', 'pcm_s24le', 'pcm_f32le', 'mp3', 'flac', 'opus', 'vorbis', 'aac'}+++def convert_if_needed(+ filepath: str,+ original_filename: Optional[str] = None,+ codec_info: Optional[Dict[str, Any]] = None,+ needs_chunking: bool = False,+ is_asr_endpoint: bool = False,+ delete_original: bool = True+) -> ConversionResult:+ """+ Handle ALL audio conversion needs in one place.++ This is the ONLY function callers should use. It handles:+ 1. Video to audio extraction (if has_video)+ 2. Unsupported codec conversion (if codec not supported)+ 3. Lossless audio compression (if AUDIO_COMPRESS_UPLOADS enabled)++ The function makes intelligent decisions about what processing is needed+ and performs it in the optimal order.++ Args:+ filepath: Path to the audio/video file+ original_filename: Original filename for logging (defaults to basename)+ codec_info: Optional pre-fetched codec info to avoid redundant probe calls+ needs_chunking: Whether chunking will be used (affects supported codecs)+ is_asr_endpoint: Whether using ASR endpoint (affects AAC handling)+ delete_original: Whether to delete original file after successful conversion++ Returns:+ ConversionResult with output path, mime type, and conversion stats++ Raises:+ FFmpegNotFoundError: If FFmpeg is not available+ FFmpegError: If conversion fails+ """+ if original_filename is None:+ original_filename = os.path.basename(filepath)++ # Get original file size+ original_size = os.path.getsize(filepath)++ # Probe if codec info not provided+ if codec_info is None:+ try:+ codec_info = get_codec_info(filepath, timeout=10)+ logger.info(+ f"Detected codec for {original_filename}: "+ f"audio_codec={codec_info.get('audio_codec')}, "+ f"has_video={codec_info.get('has_video', False)}"+ )+ except FFProbeError as e:+ logger.warning(f"Failed to probe {filepath}: {e}. Will attempt conversion.")+ codec_info = None++ original_codec = codec_info.get('audio_codec') if codec_info else None+ audio_codec = original_codec+ has_video = codec_info.get('has_video', False) if codec_info else False++ # Get supported codecs based on processing mode+ supported_codecs = get_supported_codecs(needs_chunking)++ # Handle video files - extract audio+ if has_video:+ # Check if we can remux (copy) instead of transcode+ can_remux = False+ if audio_codec and audio_codec in supported_codecs:+ try:+ # Remux if audio is lossy, or if lossless but compression is disabled+ is_lossless = is_lossless_audio(filepath, codec_info=codec_info)+ can_remux = not is_lossless or not AUDIO_COMPRESS_UPLOADS+ except Exception as e:+ logger.warning(f"Could not determine if audio is lossless: {e}. Will transcode.")++ try:+ if can_remux:+ logger.info(f"Extracting audio from video (remux, no transcoding): {original_filename}")+ output_filepath, mime_type = extract_audio_from_video(+ filepath,+ output_format='copy',+ cleanup_original=delete_original,+ copy_stream=True+ )+ final_codec = audio_codec+ else:+ logger.info(f"Extracting and converting audio from video to {AUDIO_CODEC.upper()}: {original_filename}")+ output_filepath, mime_type = extract_audio_from_video(+ filepath,+ output_format=AUDIO_CODEC,+ bitrate=AUDIO_BITRATE,+ cleanup_original=delete_original,+ copy_stream=False+ )+ final_codec = AUDIO_CODEC++ final_size = os.path.getsize(output_filepath)+ reduction = ((original_size - final_size) / original_size * 100) if original_size > 0 else 0++ logger.info(+ f"Successfully extracted audio from {original_filename}: "+ f"{original_size/1024/1024:.1f}MB -> {final_size/1024/1024:.1f}MB "+ f"({reduction:.1f}% reduction)"+ )++ return ConversionResult(+ output_path=output_filepath,+ mime_type=mime_type,+ was_converted=not can_remux,+ was_compressed=False,+ original_size=original_size,+ final_size=final_size,+ original_codec=original_codec,+ final_codec=final_codec+ )+ except FFmpegNotFoundError:+ logger.error("FFmpeg not found")+ raise+ except FFmpegError as e:+ logger.error(f"Failed to extract audio from video {filepath}: {e}")+ raise++ # Handle audio files - check if conversion needed+ needs_conversion = False+ if audio_codec is None:+ needs_conversion = True+ logger.info(f"Unknown codec for {original_filename}, will attempt conversion")+ elif is_asr_endpoint and audio_codec == 'aac':+ needs_conversion = True+ logger.info(f"Converting AAC-encoded file for ASR endpoint compatibility")+ elif audio_codec not in supported_codecs:+ needs_conversion = True+ logger.info(f"Converting {original_filename} (codec: {audio_codec}) - unsupported for processing")++ if needs_conversion:+ logger.info(f"Converting {original_filename} to {AUDIO_CODEC.upper()}")++ try:+ output_filepath, mime_type, _ = compress_audio(+ filepath,+ codec=AUDIO_CODEC,+ bitrate=AUDIO_BITRATE,+ delete_original=delete_original,+ codec_info=codec_info+ )++ final_size = os.path.getsize(output_filepath)+ reduction = ((original_size - final_size) / original_size * 100) if original_size > 0 else 0++ logger.info(+ f"Successfully converted {original_filename}: "+ f"{original_size/1024/1024:.1f}MB -> {final_size/1024/1024:.1f}MB "+ f"({reduction:.1f}% reduction)"+ )++ return ConversionResult(+ output_path=output_filepath,+ mime_type=mime_type,+ was_converted=True,+ was_compressed=False,+ original_size=original_size,+ final_size=final_size,+ original_codec=original_codec,+ final_codec=AUDIO_CODEC+ )+ except FFmpegNotFoundError:+ logger.error("FFmpeg not found")+ raise+ except FFmpegError as e:+ logger.error(f"FFmpeg conversion failed for {filepath}: {e}")+ raise++ # Audio file with supported codec - check if we should compress lossless+ logger.info(f"Codec {audio_codec} is supported, no conversion needed")++ if AUDIO_COMPRESS_UPLOADS:+ try:+ # Check if file is lossless+ if is_lossless_audio(filepath, codec_info=codec_info):+ # Skip if already in target codec (e.g., FLAC to FLAC)+ if audio_codec == AUDIO_CODEC:+ logger.info(f"File already in target codec {AUDIO_CODEC}, no compression needed")+ return ConversionResult(+ output_path=filepath,+ mime_type=_guess_mime_type(filepath),+ was_converted=False,+ was_compressed=False,+ original_size=original_size,+ final_size=original_size,+ original_codec=original_codec,+ final_codec=audio_codec+ )++ logger.info(f"Compressing lossless audio ({audio_codec}) to {AUDIO_CODEC.upper()}")++ # Perform compression+ compressed_path, mime_type, _ = compress_audio(+ filepath,+ codec=AUDIO_CODEC,+ bitrate=AUDIO_BITRATE,+ delete_original=delete_original,+ codec_info=codec_info+ )++ final_size = os.path.getsize(compressed_path)+ reduction = ((original_size - final_size) / original_size * 100) if original_size > 0 else 0++ logger.info(+ f"Successfully compressed {original_filename}: "+ f"{original_size/1024/1024:.1f}MB -> {final_size/1024/1024:.1f}MB "+ f"({reduction:.1f}% reduction)"+ )++ return ConversionResult(+ output_path=compressed_path,+ mime_type=mime_type,+ was_converted=False,+ was_compressed=True,+ original_size=original_size,+ final_size=final_size,+ original_codec=original_codec,+ final_codec=AUDIO_CODEC+ )+ except Exception as e:+ logger.warning(f"Failed to compress lossless audio: {e}. Continuing with original.")+ # Fall through to return original file++ # No processing needed - return original file+ return ConversionResult(+ output_path=filepath,+ mime_type=_guess_mime_type(filepath),+ was_converted=False,+ was_compressed=False,+ original_size=original_size,+ final_size=original_size,+ original_codec=original_codec,+ final_codec=audio_codec+ )+++def _guess_mime_type(filepath: str) -> str:+ """+ Guess MIME type from file extension.++ Args:+ filepath: Path to the file++ Returns:+ MIME type string+ """+ import mimetypes+ mime_type, _ = mimetypes.guess_type(filepath)+ return mime_type or 'application/octet-stream'diff --git a/src/utils/ffmpeg_utils.py b/src/utils/ffmpeg_utils.pynew file mode 100644index 00000000..50afba84--- /dev/null+++ b/src/utils/ffmpeg_utils.py@@ -0,0 +1,447 @@+"""Centralized FFmpeg utilities for consistent audio/video processing."""++import os+import subprocess+import tempfile+from contextlib import contextmanager+from typing import Optional, Tuple+from flask import current_app+++# Configuration constants+DEFAULT_MP3_BITRATE = os.getenv('AUDIO_BITRATE', '128k')+DEFAULT_SAMPLE_RATE = os.getenv('AUDIO_SAMPLE_RATE', '44100')+DEFAULT_CHANNELS = int(os.getenv('AUDIO_CHANNELS', '1')) # Mono for speech+DEFAULT_COMPRESSION_LEVEL = int(os.getenv('AUDIO_COMPRESSION_LEVEL', '2'))+++class FFmpegError(Exception):+ """Custom exception for FFmpeg-related errors."""+ pass+++class FFmpegNotFoundError(FFmpegError):+ """Raised when FFmpeg executable is not found."""+ pass+++def convert_to_mp3(+ input_path: str,+ output_path: Optional[str] = None,+ bitrate: str = DEFAULT_MP3_BITRATE,+ sample_rate: str = DEFAULT_SAMPLE_RATE,+ channels: int = DEFAULT_CHANNELS,+ compression_level: int = DEFAULT_COMPRESSION_LEVEL+) -> str:+ """+ Convert audio/video file to MP3 format using FFmpeg.++ Args:+ input_path: Path to input audio/video file+ output_path: Path for output MP3 file (auto-generated if None)+ bitrate: MP3 bitrate (e.g., '128k', '192k')+ sample_rate: Sample rate in Hz (e.g., '44100', '48000')+ channels: Number of audio channels (1=mono, 2=stereo)+ compression_level: MP3 compression level (0-9, higher=better compression)++ Returns:+ Path to the created MP3 file++ Raises:+ FFmpegNotFoundError: If FFmpeg is not installed+ FFmpegError: If conversion fails+ """+ if output_path is None:+ base = os.path.splitext(input_path)[0]+ output_path = f"{base}.mp3"++ cmd = [+ 'ffmpeg',+ '-i', input_path,+ '-y', # Overwrite output+ '-acodec', 'libmp3lame',+ '-b:a', bitrate,+ '-ar', sample_rate,+ '-ac', str(channels),+ '-compression_level', str(compression_level),+ output_path+ ]++ _run_ffmpeg_command(cmd, f"MP3 conversion of {os.path.basename(input_path)}")+ return output_path+++def extract_audio_from_video(+ video_path: str,+ output_format: str = 'mp3',+ bitrate: str = DEFAULT_MP3_BITRATE,+ cleanup_original: bool = True,+ copy_stream: bool = False+) -> Tuple[str, str]:+ """+ Extract audio track from video file.++ Args:+ video_path: Path to video file+ output_format: Audio format ('mp3', 'wav', 'flac', 'copy')+ bitrate: Audio bitrate for lossy formats (ignored if copy_stream=True)+ cleanup_original: Whether to delete the original video file+ copy_stream: If True, copy audio stream without re-encoding (fast, preserves quality)+ If False, re-encode to specified format++ Returns:+ Tuple of (audio_filepath, mime_type)++ Raises:+ FFmpegNotFoundError: If FFmpeg is not installed+ FFmpegError: If extraction fails+ """+ base_path = os.path.splitext(video_path)[0]++ try:+ if copy_stream or output_format == 'copy':+ # Copy audio stream without re-encoding - need to detect the format first+ from src.utils.ffprobe import get_codec_info++ try:+ codec_info = get_codec_info(video_path, timeout=10)+ audio_codec = codec_info.get('audio_codec', 'unknown')++ # Map codec to extension and MIME type+ codec_map = {+ 'aac': {'ext': 'm4a', 'mime': 'audio/mp4'},+ 'mp3': {'ext': 'mp3', 'mime': 'audio/mpeg'},+ 'opus': {'ext': 'opus', 'mime': 'audio/opus'},+ 'vorbis': {'ext': 'ogg', 'mime': 'audio/ogg'},+ 'flac': {'ext': 'flac', 'mime': 'audio/flac'},+ }++ if audio_codec in codec_map:+ output_ext = codec_map[audio_codec]['ext']+ mime_type = codec_map[audio_codec]['mime']+ else:+ # Default to m4a for unknown codecs+ current_app.logger.warning(f"Unknown audio codec '{audio_codec}', defaulting to m4a container")+ output_ext = 'm4a'+ mime_type = 'audio/mp4'++ temp_audio_path = f"{base_path}_audio_temp.{output_ext}"+ final_audio_path = f"{base_path}_audio.{output_ext}"++ cmd = [+ 'ffmpeg',+ '-i', video_path,+ '-y',+ '-vn', # No video+ '-acodec', 'copy', # Copy audio stream without re-encoding+ temp_audio_path+ ]++ current_app.logger.info(f"Copying audio stream (codec: {audio_codec}) without re-encoding")++ except Exception as probe_error:+ current_app.logger.warning(f"Failed to detect audio codec: {probe_error}. Falling back to MP3 encoding.")+ # Fallback to MP3 encoding if we can't detect the codec+ output_ext = 'mp3'+ mime_type = 'audio/mpeg'+ temp_audio_path = f"{base_path}_audio_temp.{output_ext}"+ final_audio_path = f"{base_path}_audio.{output_ext}"++ cmd = [+ 'ffmpeg',+ '-i', video_path,+ '-y',+ '-vn',+ '-acodec', 'libmp3lame',+ '-b:a', bitrate,+ '-ar', DEFAULT_SAMPLE_RATE,+ '-ac', str(DEFAULT_CHANNELS),+ '-compression_level', str(DEFAULT_COMPRESSION_LEVEL),+ temp_audio_path+ ]++ elif output_format == 'mp3':+ temp_audio_path = f"{base_path}_audio_temp.mp3"+ final_audio_path = f"{base_path}_audio.mp3"+ cmd = [+ 'ffmpeg',+ '-i', video_path,+ '-y',+ '-vn', # No video+ '-acodec', 'libmp3lame',+ '-b:a', bitrate,+ '-ar', DEFAULT_SAMPLE_RATE,+ '-ac', str(DEFAULT_CHANNELS),+ '-compression_level', str(DEFAULT_COMPRESSION_LEVEL),+ temp_audio_path+ ]+ mime_type = 'audio/mpeg'+ elif output_format == 'wav':+ temp_audio_path = f"{base_path}_audio_temp.wav"+ final_audio_path = f"{base_path}_audio.wav"+ cmd = [+ 'ffmpeg',+ '-i', video_path,+ '-y',+ '-vn',+ '-acodec', 'pcm_s16le',+ '-ar', DEFAULT_SAMPLE_RATE,+ temp_audio_path+ ]+ mime_type = 'audio/wav'+ elif output_format == 'flac':+ temp_audio_path = f"{base_path}_audio_temp.flac"+ final_audio_path = f"{base_path}_audio.flac"+ cmd = [+ 'ffmpeg',+ '-i', video_path,+ '-y',+ '-vn',+ '-acodec', 'flac',+ '-compression_level', '12',+ temp_audio_path+ ]+ mime_type = 'audio/flac'+ elif output_format == 'opus':+ temp_audio_path = f"{base_path}_audio_temp.opus"+ final_audio_path = f"{base_path}_audio.opus"+ cmd = [+ 'ffmpeg',+ '-i', video_path,+ '-y',+ '-vn',+ '-acodec', 'libopus',+ '-b:a', bitrate,+ temp_audio_path+ ]+ mime_type = 'audio/opus'+ else:+ raise ValueError(f"Unsupported output format: {output_format}")++ _run_ffmpeg_command(cmd, f"Audio extraction from {os.path.basename(video_path)}")++ # Optionally preserve temp file for debugging+ if os.getenv('PRESERVE_TEMP_AUDIO', 'false').lower() == 'true':+ import shutil+ debug_path = temp_audio_path.replace('_temp', '_debug')+ shutil.copy2(temp_audio_path, debug_path)+ current_app.logger.info(f"Debug: Preserved temp audio file as {debug_path}")++ # Rename temp file to final filename+ os.rename(temp_audio_path, final_audio_path)++ if cleanup_original:+ try:+ os.remove(video_path)+ current_app.logger.info(f"Cleaned up original video: {os.path.basename(video_path)}")+ except Exception as e:+ current_app.logger.warning(f"Failed to cleanup video {video_path}: {e}")++ return final_audio_path, mime_type++ except Exception as e:+ # Clean up temp file on error+ if os.path.exists(temp_audio_path):+ try:+ os.remove(temp_audio_path)+ except:+ pass+ raise+++def compress_audio(+ input_path: str,+ codec: str = 'mp3',+ bitrate: str = DEFAULT_MP3_BITRATE,+ delete_original: bool = True,+ codec_info: Optional[dict] = None+) -> Tuple[str, str, Optional[dict]]:+ """+ Compress audio file to specified codec.++ Args:+ input_path: Path to input audio file+ codec: Target codec ('mp3', 'flac', 'opus')+ bitrate: Bitrate for lossy codecs (ignored for FLAC)+ delete_original: Whether to delete the original file after compression+ codec_info: Optional pre-fetched codec info (returned as-is, not updated)++ Returns:+ Tuple of (output_path, mime_type, codec_info)+ Note: codec_info is returned unchanged (None after compression)++ Raises:+ FFmpegNotFoundError: If FFmpeg is not installed+ FFmpegError: If compression fails+ """+ codec_config = {+ 'mp3': {+ 'ext': '.mp3',+ 'mime': 'audio/mpeg',+ 'cmd_args': [+ '-acodec', 'libmp3lame',+ '-b:a', bitrate,+ '-ar', DEFAULT_SAMPLE_RATE,+ '-ac', str(DEFAULT_CHANNELS)+ ]+ },+ 'flac': {+ 'ext': '.flac',+ 'mime': 'audio/flac',+ 'cmd_args': ['-acodec', 'flac', '-compression_level', '12']+ },+ 'opus': {+ 'ext': '.opus',+ 'mime': 'audio/opus',+ 'cmd_args': ['-acodec', 'libopus', '-b:a', bitrate]+ }+ }++ if codec not in codec_config:+ raise ValueError(f"Unsupported codec: {codec}. Supported: {list(codec_config.keys())}")++ config = codec_config[codec]+ base_path = os.path.splitext(input_path)[0]+ temp_output_path = f"{base_path}_compressed_temp{config['ext']}"+ final_output_path = f"{base_path}{config['ext']}"++ try:+ # Get original file size for logging+ original_size = os.path.getsize(input_path)++ cmd = ['ffmpeg', '-i', input_path, '-y'] + config['cmd_args'] + [temp_output_path]++ _run_ffmpeg_command(cmd, f"Compression of {os.path.basename(input_path)} to {codec}")++ # Get compressed file size+ compressed_size = os.path.getsize(temp_output_path)+ ratio = (1 - compressed_size / original_size) * 100 if original_size > 0 else 0++ current_app.logger.info(+ f"Compressed {os.path.basename(input_path)}: "+ f"{original_size / 1024 / 1024:.1f}MB -> "+ f"{compressed_size / 1024 / 1024:.1f}MB ({ratio:.1f}% reduction)"+ )++ # Remove original and rename temp to final+ if delete_original:+ os.remove(input_path)+ current_app.logger.debug(f"Deleted original file: {input_path}")+ os.rename(temp_output_path, final_output_path)++ # Return codec_info as None since file was converted (codec changed)+ return final_output_path, config['mime'], None++ except Exception as e:+ # Clean up temp file if it exists+ if os.path.exists(temp_output_path):+ try:+ os.remove(temp_output_path)+ except:+ pass+ # Re-raise with codec_info preservation+ raise+++def extract_audio_segment(+ input_path: str,+ output_path: str,+ start_time: float,+ duration: float+) -> None:+ """+ Extract a segment from an audio file.++ Args:+ input_path: Path to input audio file+ output_path: Path for output segment+ start_time: Start time in seconds+ duration: Duration in seconds++ Raises:+ FFmpegNotFoundError: If FFmpeg is not installed+ FFmpegError: If extraction fails+ """+ cmd = [+ 'ffmpeg',+ '-i', input_path,+ '-ss', str(start_time),+ '-t', str(duration),+ '-c', 'copy', # Copy codec (no re-encoding)+ '-y',+ output_path+ ]++ _run_ffmpeg_command(cmd, f"Segment extraction from {os.path.basename(input_path)}")+++@contextmanager+def temp_audio_conversion(input_path: str, target_format: str = 'mp3'):+ """+ Context manager for temporary audio conversion.+ Automatically cleans up temp file on exit.++ Example:+ with temp_audio_conversion(input_path, 'mp3') as mp3_path:+ # Use mp3_path+ process_audio(mp3_path)+ # mp3_path is automatically deleted++ Args:+ input_path: Path to input audio file+ target_format: Target format ('mp3', 'wav', etc.)++ Yields:+ Path to temporary converted file+ """+ temp_path = None+ try:+ with tempfile.NamedTemporaryFile(suffix=f'.{target_format}', delete=False) as temp_file:+ temp_path = temp_file.name++ if target_format == 'mp3':+ convert_to_mp3(input_path, temp_path)+ else:+ raise ValueError(f"Unsupported target format: {target_format}")++ yield temp_path++ finally:+ if temp_path and os.path.exists(temp_path):+ try:+ os.unlink(temp_path)+ except Exception as e:+ current_app.logger.warning(f"Failed to cleanup temp file {temp_path}: {e}")+++def _run_ffmpeg_command(cmd: list, operation_description: str) -> None:+ """+ Execute FFmpeg command with consistent error handling.++ Args:+ cmd: FFmpeg command as list of strings+ operation_description: Human-readable description for error messages++ Raises:+ FFmpegNotFoundError: If FFmpeg is not installed+ FFmpegError: If FFmpeg command fails+ """+ try:+ current_app.logger.debug(f"Running FFmpeg command: {' '.join(cmd)}")+ result = subprocess.run(+ cmd,+ check=True,+ capture_output=True,+ text=True+ )+ current_app.logger.debug(f"FFmpeg {operation_description} completed successfully")++ except FileNotFoundError:+ error_msg = "FFmpeg not found. Please ensure FFmpeg is installed and in the system's PATH."+ current_app.logger.error(error_msg)+ raise FFmpegNotFoundError(error_msg)++ except subprocess.CalledProcessError as e:+ error_msg = f"{operation_description} failed: {e.stderr}"+ current_app.logger.error(f"FFmpeg error: {error_msg}")+ raise FFmpegError(error_msg)\ No newline at end of filediff --git a/src/utils/ffprobe.py b/src/utils/ffprobe.pynew file mode 100644index 00000000..94a3b103--- /dev/null+++ b/src/utils/ffprobe.py@@ -0,0 +1,290 @@+"""+FFprobe utility for detecting audio/video codecs and format information.++This module provides functions to inspect media files using ffprobe and return+structured information about their codecs, streams, and formats.+"""++import json+import logging+import subprocess+from typing import Optional, Dict, Any, Tuple++logger = logging.getLogger(__name__)+++class FFProbeError(Exception):+ """Raised when ffprobe fails to analyze a file."""+ pass+++def probe(filename: str, cmd: str = 'ffprobe', timeout: Optional[int] = None) -> Dict[str, Any]:+ """+ Run ffprobe on the specified file and return a JSON representation of the output.++ Args:+ filename: Path to the media file to probe+ cmd: Command to use (default: 'ffprobe')+ timeout: Optional timeout in seconds++ Returns:+ Dictionary containing streams and format information++ Raises:+ FFProbeError: if ffprobe returns a non-zero exit code+ """+ args = [cmd, '-show_format', '-show_streams', '-of', 'json', filename]+ p = None++ try:+ p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)+ communicate_kwargs = {}+ if timeout is not None:+ communicate_kwargs['timeout'] = timeout+ out, err = p.communicate(**communicate_kwargs)++ if p.returncode != 0:+ error_msg = err.decode('utf-8', errors='ignore')+ raise FFProbeError(f'ffprobe failed: {error_msg}')++ return json.loads(out.decode('utf-8'))+ except subprocess.TimeoutExpired:+ if p:+ p.kill()+ raise FFProbeError(f'ffprobe timed out after {timeout} seconds')+ except FileNotFoundError:+ raise FFProbeError('ffprobe command not found. Please ensure ffmpeg is installed.')+ except json.JSONDecodeError as e:+ raise FFProbeError(f'Failed to parse ffprobe output: {e}')+++def get_codec_info(filename: str, timeout: Optional[int] = None) -> Dict[str, Any]:+ """+ Get codec information for a media file.++ Args:+ filename: Path to the media file+ timeout: Optional timeout in seconds++ Returns:+ Dictionary with keys:+ - audio_codec: Audio codec name (e.g., 'pcm_s16le', 'aac', 'mp3')+ - video_codec: Video codec name if present, or None+ - has_video: Boolean indicating if file contains video stream+ - has_audio: Boolean indicating if file contains audio stream+ - format_name: Container format name (e.g., 'wav', 'mov,mp4,m4a')+ - duration: Duration in seconds (float)+ - sample_rate: Audio sample rate if available+ - channels: Number of audio channels if available+ - bit_rate: Bit rate if available++ Raises:+ FFProbeError: if ffprobe fails to analyze the file+ """+ try:+ probe_data = probe(filename, timeout=timeout)+ except FFProbeError:+ raise++ result = {+ 'audio_codec': None,+ 'video_codec': None,+ 'has_video': False,+ 'has_audio': False,+ 'format_name': None,+ 'duration': None,+ 'sample_rate': None,+ 'channels': None,+ 'bit_rate': None+ }++ # Extract format information+ if 'format' in probe_data:+ fmt = probe_data['format']+ result['format_name'] = fmt.get('format_name')++ if 'duration' in fmt:+ try:+ result['duration'] = float(fmt['duration'])+ except (ValueError, TypeError):+ pass++ if 'bit_rate' in fmt:+ try:+ result['bit_rate'] = int(fmt['bit_rate'])+ except (ValueError, TypeError):+ pass++ # Extract stream information+ if 'streams' in probe_data:+ for stream in probe_data['streams']:+ codec_type = stream.get('codec_type')+ codec_name = stream.get('codec_name')++ if codec_type == 'audio':+ result['has_audio'] = True+ if result['audio_codec'] is None: # Use first audio stream+ result['audio_codec'] = codec_name+ result['sample_rate'] = stream.get('sample_rate')+ result['channels'] = stream.get('channels')++ elif codec_type == 'video':+ result['has_video'] = True+ if result['video_codec'] is None: # Use first video stream+ result['video_codec'] = codec_name++ return result+++def is_video_file(filename: str, timeout: Optional[int] = None, codec_info: Optional[Dict[str, Any]] = None) -> bool:+ """+ Check if a file contains video streams.++ Args:+ filename: Path to the media file+ timeout: Optional timeout in seconds+ codec_info: Optional pre-fetched codec info to avoid redundant probe calls++ Returns:+ True if file contains video streams, False otherwise+ """+ try:+ if codec_info is None:+ codec_info = get_codec_info(filename, timeout=timeout)+ return codec_info['has_video']+ except FFProbeError as e:+ logger.warning(f"Failed to probe {filename}: {e}")+ return False+++def is_audio_file(filename: str, timeout: Optional[int] = None, codec_info: Optional[Dict[str, Any]] = None) -> bool:+ """+ Check if a file contains audio streams.++ Args:+ filename: Path to the media file+ timeout: Optional timeout in seconds+ codec_info: Optional pre-fetched codec info to avoid redundant probe calls++ Returns:+ True if file contains audio streams, False otherwise+ """+ try:+ if codec_info is None:+ codec_info = get_codec_info(filename, timeout=timeout)+ return codec_info['has_audio']+ except FFProbeError as e:+ logger.warning(f"Failed to probe {filename}: {e}")+ return False+++def get_audio_codec(filename: str, timeout: Optional[int] = None, codec_info: Optional[Dict[str, Any]] = None) -> Optional[str]:+ """+ Get the audio codec name for a file.++ Args:+ filename: Path to the media file+ timeout: Optional timeout in seconds+ codec_info: Optional pre-fetched codec info to avoid redundant probe calls++ Returns:+ Audio codec name (e.g., 'pcm_s16le', 'aac', 'mp3', 'opus'), or None if no audio+ """+ try:+ if codec_info is None:+ codec_info = get_codec_info(filename, timeout=timeout)+ return codec_info['audio_codec']+ except FFProbeError as e:+ logger.warning(f"Failed to probe {filename}: {e}")+ return None+++def needs_audio_conversion(filename: str, supported_codecs: list, timeout: Optional[int] = None, codec_info: Optional[Dict[str, Any]] = None) -> Tuple[bool, Optional[str]]:+ """+ Check if a file needs audio conversion based on its codec.++ Args:+ filename: Path to the media file+ supported_codecs: List of supported audio codec names+ timeout: Optional timeout in seconds+ codec_info: Optional pre-fetched codec info to avoid redundant probe calls++ Returns:+ Tuple of (needs_conversion: bool, current_codec: str or None)+ """+ try:+ if codec_info is None:+ codec_info = get_codec_info(filename, timeout=timeout)++ # If it has video, it likely needs conversion+ if codec_info['has_video']:+ return True, codec_info.get('audio_codec')++ # If no audio at all, cannot convert+ if not codec_info['has_audio']:+ logger.warning(f"File {filename} has no audio streams")+ return False, None++ audio_codec = codec_info['audio_codec']++ # Check if codec is in supported list+ if audio_codec in supported_codecs:+ return False, audio_codec++ return True, audio_codec++ except FFProbeError as e:+ logger.warning(f"Failed to probe {filename}: {e}")+ # Default to attempting conversion on error+ return True, None+++def is_lossless_audio(filename: str, timeout: Optional[int] = None, codec_info: Optional[Dict[str, Any]] = None) -> bool:+ """+ Check if a file uses a lossless audio codec.++ Args:+ filename: Path to the media file+ timeout: Optional timeout in seconds+ codec_info: Optional pre-fetched codec info to avoid redundant probe calls++ Returns:+ True if file uses lossless audio codec, False otherwise+ """+ lossless_codecs = {+ 'pcm_s16le', 'pcm_s24le', 'pcm_s32le',+ 'pcm_f32le', 'pcm_f64le',+ 'pcm_u8', 'pcm_u16le', 'pcm_u24le', 'pcm_u32le',+ 'flac', 'alac', 'ape', 'wavpack', 'tta',+ 'mlp', 'truehd'+ }++ try:+ if codec_info is None:+ codec_info = get_codec_info(filename, timeout=timeout)+ audio_codec = codec_info['audio_codec']+ return audio_codec in lossless_codecs if audio_codec else False+ except FFProbeError as e:+ logger.warning(f"Failed to probe {filename}: {e}")+ return False+++def get_duration(filename: str, timeout: Optional[int] = None, codec_info: Optional[Dict[str, Any]] = None) -> Optional[float]:+ """+ Get the duration of a media file in seconds.++ Args:+ filename: Path to the media file+ timeout: Optional timeout in seconds+ codec_info: Optional pre-fetched codec info to avoid redundant probe calls++ Returns:+ Duration in seconds, or None if unable to determine+ """+ try:+ if codec_info is None:+ codec_info = get_codec_info(filename, timeout=timeout)+ return codec_info['duration']+ except FFProbeError as e:+ logger.warning(f"Failed to probe {filename}: {e}")+ return None\ No newline at end of filediff --git a/tests/test_ffprobe_codec_detection.py b/tests/test_ffprobe_codec_detection.pynew file mode 100644index 00000000..be7f70b0--- /dev/null+++ b/tests/test_ffprobe_codec_detection.py@@ -0,0 +1,319 @@+#!/usr/bin/env python3+"""+Test script for ffprobe codec detection functionality.++This script tests the new codec-based detection system to ensure it correctly+identifies audio codecs, video files, and lossless formats.+"""++import os+import sys+import tempfile+import subprocess+from pathlib import Path++# Add parent directory to path for imports+sys.path.insert(0, str(Path(__file__).parent.parent))++from src.utils.ffprobe import (+ get_codec_info,+ is_video_file,+ is_audio_file,+ get_audio_codec,+ needs_audio_conversion,+ is_lossless_audio,+ get_duration,+ FFProbeError+)+++def create_test_audio_file(codec, output_path, duration=1.0):+ """Create a test audio file with specific codec."""+ codec_map = {+ 'mp3': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'libmp3lame', '-b:a', '128k', output_path],+ 'aac': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'aac', '-b:a', '128k', output_path],+ 'opus': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'libopus', '-b:a', '64k', output_path],+ 'flac': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'flac', output_path],+ 'pcm_s16le': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'pcm_s16le', '-ar', '44100', output_path],+ 'vorbis': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'libvorbis', '-b:a', '128k', output_path],+ }++ if codec not in codec_map:+ raise ValueError(f"Unknown codec: {codec}")++ subprocess.run(codec_map[codec], check=True, capture_output=True)+++def create_test_video_file(output_path, duration=1.0):+ """Create a test video file with audio."""+ subprocess.run([+ 'ffmpeg', '-f', 'lavfi', '-i', f'testsrc=duration={duration}:size=320x240:rate=1',+ '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}',+ '-acodec', 'aac', '-vcodec', 'libx264', '-pix_fmt', 'yuv420p',+ output_path+ ], check=True, capture_output=True)+++def test_codec_detection():+ """Test basic codec detection."""+ print("\n=== Testing Codec Detection ===\n")++ with tempfile.TemporaryDirectory() as tmpdir:+ test_files = {+ 'mp3': 'test.mp3',+ 'aac': 'test.m4a',+ 'opus': 'test.opus',+ 'flac': 'test.flac',+ 'pcm_s16le': 'test.wav',+ 'vorbis': 'test.ogg',+ }++ for codec, filename in test_files.items():+ filepath = os.path.join(tmpdir, filename)+ try:+ print(f"Creating test file: {filename} with codec {codec}...")+ create_test_audio_file(codec, filepath)++ print(f" Probing {filename}...")+ codec_info = get_codec_info(filepath)++ detected_codec = codec_info['audio_codec']+ print(f" ✓ Detected codec: {detected_codec}")+ print(f" Has audio: {codec_info['has_audio']}")+ print(f" Has video: {codec_info['has_video']}")+ print(f" Format: {codec_info['format_name']}")+ print(f" Duration: {codec_info['duration']:.2f}s" if codec_info['duration'] else " Duration: N/A")++ if detected_codec != codec:+ print(f" ⚠️ Warning: Expected {codec}, got {detected_codec}")++ print()++ except Exception as e:+ print(f" ✗ Failed to test {codec}: {e}\n")+++def test_video_detection():+ """Test video file detection."""+ print("\n=== Testing Video Detection ===\n")++ with tempfile.TemporaryDirectory() as tmpdir:+ video_path = os.path.join(tmpdir, 'test_video.mp4')+ audio_path = os.path.join(tmpdir, 'test_audio.mp3')++ try:+ print("Creating test video file...")+ create_test_video_file(video_path)++ print("Creating test audio file...")+ create_test_audio_file('mp3', audio_path)++ print(f"\nProbing video file...")+ codec_info = get_codec_info(video_path)+ print(f" Audio codec: {codec_info['audio_codec']}")+ print(f" Video codec: {codec_info['video_codec']}")+ print(f" Has audio: {codec_info['has_audio']}")+ print(f" Has video: {codec_info['has_video']}")++ is_video = is_video_file(video_path)+ print(f" is_video_file(): {is_video}")++ if not is_video:+ print(" ✗ Video file not detected as video!")+ else:+ print(" ✓ Video file correctly detected")++ print(f"\nProbing audio file...")+ codec_info = get_codec_info(audio_path)+ print(f" Audio codec: {codec_info['audio_codec']}")+ print(f" Video codec: {codec_info['video_codec']}")+ print(f" Has audio: {codec_info['has_audio']}")+ print(f" Has video: {codec_info['has_video']}")++ is_video = is_video_file(audio_path)+ print(f" is_video_file(): {is_video}")++ if is_video:+ print(" ✗ Audio file incorrectly detected as video!")+ else:+ print(" ✓ Audio file correctly identified as audio-only")++ print()++ except Exception as e:+ print(f"✗ Failed to test video detection: {e}\n")+++def test_lossless_detection():+ """Test lossless audio detection."""+ print("\n=== Testing Lossless Detection ===\n")++ with tempfile.TemporaryDirectory() as tmpdir:+ test_cases = {+ 'pcm_s16le': ('test.wav', True),+ 'flac': ('test.flac', True),+ 'mp3': ('test.mp3', False),+ 'aac': ('test.m4a', False),+ 'opus': ('test.opus', False),+ }++ for codec, (filename, expected_lossless) in test_cases.items():+ filepath = os.path.join(tmpdir, filename)+ try:+ print(f"Creating {filename} with codec {codec}...")+ create_test_audio_file(codec, filepath)++ is_lossless = is_lossless_audio(filepath)+ status = "✓" if is_lossless == expected_lossless else "✗"++ print(f" {status} {codec}: is_lossless={is_lossless} (expected {expected_lossless})")++ except Exception as e:+ print(f" ✗ Failed to test {codec}: {e}")++ print()+++def test_conversion_check():+ """Test conversion requirement detection."""+ print("\n=== Testing Conversion Check ===\n")++ with tempfile.TemporaryDirectory() as tmpdir:+ # Supported codecs for direct transcription+ supported_codecs = ['pcm_s16le', 'mp3', 'flac', 'opus', 'aac']++ test_cases = {+ 'mp3': ('test.mp3', False), # Supported, no conversion needed+ 'aac': ('test.m4a', False), # Supported, no conversion needed+ 'opus': ('test.opus', False), # Supported, no conversion needed+ 'vorbis': ('test.ogg', True), # Not in supported list, needs conversion+ }++ for codec, (filename, should_convert) in test_cases.items():+ filepath = os.path.join(tmpdir, filename)+ try:+ print(f"Creating {filename} with codec {codec}...")+ create_test_audio_file(codec, filepath)++ needs_conversion, detected_codec = needs_audio_conversion(filepath, supported_codecs)+ status = "✓" if needs_conversion == should_convert else "✗"++ print(f" {status} {codec}: needs_conversion={needs_conversion} (expected {should_convert})")+ print(f" Detected codec: {detected_codec}")++ except Exception as e:+ print(f" ✗ Failed to test {codec}: {e}")++ print()+++def test_misnamed_file():+ """Test detection of files with wrong extensions."""+ print("\n=== Testing Misnamed File Detection ===\n")++ with tempfile.TemporaryDirectory() as tmpdir:+ # Create an MP3 file but name it .wav+ wrong_name_path = os.path.join(tmpdir, 'actually_mp3.wav')++ try:+ print("Creating MP3 file with .wav extension...")+ create_test_audio_file('mp3', wrong_name_path)++ codec_info = get_codec_info(wrong_name_path)+ detected_codec = codec_info['audio_codec']++ print(f" Filename: actually_mp3.wav")+ print(f" Detected codec: {detected_codec}")++ if detected_codec == 'mp3':+ print(" ✓ Correctly detected MP3 codec despite .wav extension")+ else:+ print(f" ✗ Incorrectly detected as {detected_codec}")++ # Create a FLAC file but name it .mp3+ wrong_name_path2 = os.path.join(tmpdir, 'actually_flac.mp3')+ print("\nCreating FLAC file with .mp3 extension...")+ create_test_audio_file('flac', wrong_name_path2)++ codec_info = get_codec_info(wrong_name_path2)+ detected_codec = codec_info['audio_codec']++ print(f" Filename: actually_flac.mp3")+ print(f" Detected codec: {detected_codec}")++ if detected_codec == 'flac':+ print(" ✓ Correctly detected FLAC codec despite .mp3 extension")+ else:+ print(f" ✗ Incorrectly detected as {detected_codec}")++ print()++ except Exception as e:+ print(f"✗ Failed to test misnamed files: {e}\n")+++def test_duration():+ """Test duration extraction."""+ print("\n=== Testing Duration Extraction ===\n")++ with tempfile.TemporaryDirectory() as tmpdir:+ durations = [1.0, 2.5, 5.0]++ for expected_duration in durations:+ filepath = os.path.join(tmpdir, f'test_{expected_duration}s.mp3')+ try:+ print(f"Creating {expected_duration}s audio file...")+ create_test_audio_file('mp3', filepath, duration=expected_duration)++ detected_duration = get_duration(filepath)++ # Allow 0.1s tolerance for encoding variations+ if detected_duration and abs(detected_duration - expected_duration) < 0.1:+ print(f" ✓ Duration: {detected_duration:.2f}s (expected {expected_duration}s)")+ else:+ print(f" ✗ Duration: {detected_duration:.2f}s (expected {expected_duration}s)")++ except Exception as e:+ print(f" ✗ Failed to test duration: {e}")++ print()+++def main():+ """Run all tests."""+ print("=" * 60)+ print("FFProbe Codec Detection Test Suite")+ print("=" * 60)++ # Check if ffmpeg/ffprobe are available+ try:+ subprocess.run(['ffprobe', '-version'], capture_output=True, check=True)+ subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)+ except (FileNotFoundError, subprocess.CalledProcessError):+ print("\n✗ Error: ffmpeg/ffprobe not found. Please install ffmpeg to run tests.\n")+ return 1++ try:+ test_codec_detection()+ test_video_detection()+ test_lossless_detection()+ test_conversion_check()+ test_misnamed_file()+ test_duration()++ print("=" * 60)+ print("All tests completed!")+ print("=" * 60)+ print()++ return 0++ except Exception as e:+ print(f"\n✗ Test suite failed with error: {e}\n")+ import traceback+ traceback.print_exc()+ return 1+++if __name__ == '__main__':+ sys.exit(main())\ No newline at end of file