Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Fix WebM audio seeking by converting to seekable formats and adding server-side duration
Murtaza Nasir5b6422bbb561
diff --git a/src/api/shares.py b/src/api/shares.pyindex 96b30c07..b839920e 100644--- a/src/api/shares.py+++ b/src/api/shares.py@@ -55,7 +55,8 @@ def view_shared_recording(public_id):'notes_raw': recording.notes if share.share_notes else None,'meeting_date': f"{recording.meeting_date.isoformat()}T00:00:00" if recording.meeting_date else None,'mime_type': recording.mime_type,- 'audio_deleted_at': recording.audio_deleted_at.isoformat() if recording.audio_deleted_at else None+ 'audio_deleted_at': recording.audio_deleted_at.isoformat() if recording.audio_deleted_at else None,+ 'audio_duration': recording.get_audio_duration()}return render_template('share.html', recording=recording_data)diff --git a/src/models/recording.py b/src/models/recording.pyindex e475d0bb..adff1341 100644--- a/src/models/recording.py+++ b/src/models/recording.py@@ -4,11 +4,15 @@ Recording and TranscriptChunk database models.This module defines models for audio recordings and their chunked transcriptions."""+import logging+import osfrom datetime import datetimefrom sqlalchemy import funcfrom src.database import dbfrom src.utils import local_datetime_filter, md_to_html+logger = logging.getLogger(__name__)+class Recording(db.Model):"""Main recording model storing audio files and their metadata."""@@ -124,6 +128,28 @@ class Recording(db.Model):).first()return state.personal_notes if state else None+ def get_audio_duration(self):+ """+ Get the audio duration in seconds using ffprobe.++ Returns:+ Float duration in seconds, or None if unavailable+ """+ if self.audio_deleted_at is not None:+ return None++ if not self.audio_path or not os.path.exists(self.audio_path):+ return None++ try:+ from src.utils.ffprobe import get_duration+ # Allow longer timeout for packet scanning fallback on files without duration metadata+ duration = get_duration(self.audio_path, timeout=30)+ return duration+ except Exception as e:+ logger.warning(f"Failed to get duration for recording {self.id}: {e}")+ return None+def to_list_dict(self, viewer_user=None):"""Lightweight dict for list views - excludes expensive HTML conversions.@@ -216,6 +242,7 @@ class Recording(db.Model):'mime_type': self.mime_type,'audio_deleted_at': local_datetime_filter(self.audio_deleted_at),'audio_available': self.audio_deleted_at is None,+ 'audio_duration': self.get_audio_duration(),'deletion_exempt': self.deletion_exempt,'tags': [tag.to_dict() for tag in visible_tags] if visible_tags else [],'events': [event.to_dict() for event in self.events] if self.events else [],diff --git a/src/utils/audio_conversion.py b/src/utils/audio_conversion.pyindex 9c810a94..f0257c25 100644--- a/src/utils/audio_conversion.py+++ b/src/utils/audio_conversion.py@@ -81,8 +81,12 @@ def get_supported_codecs(needs_chunking: bool = False, connector_specs: Optional# For chunking: only support codecs that work well with chunkingbase_codecs = {'pcm_s16le', 'pcm_s24le', 'pcm_f32le', 'mp3', 'flac'}else:- # For direct transcription: support more codecs including WebM/Opus- base_codecs = {'pcm_s16le', 'pcm_s24le', 'pcm_f32le', 'mp3', 'flac', 'opus', 'vorbis', 'aac'}+ # For direct transcription: support common seekable formats+ # Note: opus/vorbis excluded as INPUT codecs because WebM containers from+ # MediaRecorder often lack seek cues, making browser audio players unable to seek.+ # However, opus is still valid as an OUTPUT format (AUDIO_CODEC=opus) since+ # ffmpeg creates proper .opus files with duration metadata.+ base_codecs = {'pcm_s16le', 'pcm_s24le', 'pcm_f32le', 'mp3', 'flac', 'aac'}# Remove connector-specific unsupported codecsif connector_specs and connector_specs.unsupported_codecs:diff --git a/src/utils/ffprobe.py b/src/utils/ffprobe.pyindex 67f2b67f..08add8b8 100644--- a/src/utils/ffprobe.py+++ b/src/utils/ffprobe.py@@ -274,6 +274,10 @@ def get_duration(filename: str, timeout: Optional[int] = None, codec_info: Optio"""Get the duration of a media file in seconds.+ Uses multiple methods to determine duration:+ 1. Format-level duration (fastest, works for most files)+ 2. Packet timestamps fallback (for files without duration metadata like some WebM)+Args:filename: Path to the media filetimeout: Optional timeout in seconds@@ -285,12 +289,77 @@ def get_duration(filename: str, timeout: Optional[int] = None, codec_info: Optiotry:if codec_info is None:codec_info = get_codec_info(filename, timeout=timeout)- return codec_info['duration']++ # Try format-level duration first+ if codec_info['duration'] is not None:+ return codec_info['duration']++ # Fallback: scan packets to find the last timestamp+ # This works for WebM and other files without duration metadata+ return _get_duration_from_packets(filename, timeout=timeout)except FFProbeError as e:logger.warning(f"Failed to probe {filename}: {e}")return None+def _get_duration_from_packets(filename: str, timeout: Optional[int] = None) -> Optional[float]:+ """+ Get duration by scanning packet timestamps (fallback for files without duration metadata).++ This is slower than format-level duration but works for WebM and similar files+ that don't store duration in the container metadata.++ Args:+ filename: Path to the media file+ timeout: Optional timeout in seconds++ Returns:+ Duration in seconds, or None if unable to determine+ """+ try:+ args = [+ 'ffprobe', '-v', 'error',+ '-show_entries', 'packet=pts_time',+ '-select_streams', 'a:0', # First audio stream+ '-of', 'csv=p=0',+ filename+ ]++ 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:+ logger.debug(f"Packet scan failed for {filename}")+ return None++ # Parse the output to find the last timestamp+ lines = out.decode('utf-8').strip().split('\n')+ last_valid_time = None+ for line in reversed(lines):+ line = line.strip()+ if line and line != 'N/A':+ try:+ last_valid_time = float(line)+ break+ except ValueError:+ continue++ if last_valid_time is not None:+ logger.debug(f"Got duration from packets for {filename}: {last_valid_time}")+ return last_valid_time++ return None+ except subprocess.TimeoutExpired:+ logger.warning(f"Packet scan timed out for {filename}")+ return None+ except Exception as e:+ logger.warning(f"Error scanning packets for {filename}: {e}")+ return None++def get_creation_date(filename: str, timeout: Optional[int] = None, use_file_mtime: bool = True) -> Optional[datetime]:"""Extract the creation/recording date from a media file's metadata.diff --git a/static/js/modules/composables/audioPlayer.js b/static/js/modules/composables/audioPlayer.jsnew file mode 100644index 00000000..951d333d--- /dev/null+++ b/static/js/modules/composables/audioPlayer.js@@ -0,0 +1,336 @@+/**+ * Audio Player Composable+ *+ * Centralized audio playback functionality for consistent behavior across the app.+ * This module handles:+ * - Playback state (playing, paused, loading)+ * - Time tracking (current time, duration)+ * - Volume/mute control+ * - Seeking with progress bar support+ * - Server-side duration support (for formats like WebM that don't report duration)+ *+ * Usage:+ * const player = useAudioPlayer(ref, computed);+ * // In template: @loadedmetadata="player.handleLoadedMetadata"+ * // When recording changes: player.setServerDuration(recording.audio_duration)+ */++export function useAudioPlayer(ref, computed) {+ // --- State ---+ const isPlaying = ref(false);+ const currentTime = ref(0);+ const duration = ref(0);+ const isMuted = ref(false);+ const isLoading = ref(false);+ const volume = ref(1.0);++ // Progress bar drag state+ const isDragging = ref(false);+ const dragPreviewPercent = ref(0);++ // Track if we have a reliable server-side duration+ let hasServerDuration = false;++ // --- Computed ---+ const progressPercent = computed(() => {+ // Use preview position while dragging for smooth UI+ if (isDragging.value) {+ return dragPreviewPercent.value;+ }+ if (!duration.value) return 0;+ return (currentTime.value / duration.value) * 100;+ });++ // Preview time display while dragging+ const displayCurrentTime = computed(() => {+ if (isDragging.value && duration.value) {+ return (dragPreviewPercent.value / 100) * duration.value;+ }+ return currentTime.value;+ });++ // --- Duration Management ---++ /**+ * Set duration from server-side ffprobe value.+ * This is more reliable than browser metadata for some formats (WebM, etc.)+ */+ const setServerDuration = (serverDuration) => {+ if (serverDuration && isFinite(serverDuration) && serverDuration > 0) {+ duration.value = serverDuration;+ hasServerDuration = true;+ } else {+ hasServerDuration = false;+ }+ };++ /**+ * Try to set duration from browser, only if we don't have a server-side value.+ * Browser duration can be Infinity for some WebM files.+ */+ const trySetBrowserDuration = (browserDuration) => {+ if (hasServerDuration) {+ // Don't overwrite reliable server-side duration+ return;+ }+ if (browserDuration && isFinite(browserDuration) && browserDuration > 0) {+ duration.value = browserDuration;+ }+ };++ // --- Event Handlers ---++ const handlePlayPause = (event) => {+ isPlaying.value = !event.target.paused;+ };++ const handleLoadedMetadata = (event) => {+ trySetBrowserDuration(event.target.duration);+ isLoading.value = false;+ };++ const handleDurationChange = (event) => {+ // WebM and some formats may initially report Infinity duration+ // This handler catches when the actual duration becomes available+ trySetBrowserDuration(event.target.duration);+ };++ const handleTimeUpdate = (event) => {+ currentTime.value = event.target.currentTime;++ // Fallback: if duration wasn't set yet, try to get it now+ if (!duration.value || duration.value === 0) {+ trySetBrowserDuration(event.target.duration);+ }+ };++ const handleEnded = () => {+ isPlaying.value = false;+ currentTime.value = 0;+ };++ const handleWaiting = () => {+ isLoading.value = true;+ };++ const handleCanPlay = (event) => {+ isLoading.value = false;++ // Fallback: try to get duration if not set yet+ if (!duration.value || duration.value === 0) {+ trySetBrowserDuration(event.target.duration);+ }+ };++ const handleVolumeChange = (event) => {+ volume.value = event.target.volume;+ isMuted.value = event.target.muted;+ };++ // --- Actions ---++ /**+ * Get the audio element. Override this for custom element selection.+ */+ let getAudioElement = () => {+ return document.querySelector('audio[ref="audioPlayerElement"]') ||+ document.querySelector('audio');+ };++ /**+ * Set custom audio element getter.+ */+ const setAudioElementGetter = (getter) => {+ getAudioElement = getter;+ };++ const play = () => {+ const audio = getAudioElement();+ if (audio) {+ audio.play().catch(err => console.warn('Play failed:', err));+ }+ };++ const pause = () => {+ const audio = getAudioElement();+ if (audio) {+ audio.pause();+ }+ };++ const togglePlayback = () => {+ const audio = getAudioElement();+ if (!audio) return;++ if (audio.paused) {+ audio.play().catch(err => console.warn('Play failed:', err));+ } else {+ audio.pause();+ }+ };++ const seekTo = (time) => {+ const audio = getAudioElement();+ if (!audio || !isFinite(time)) return;++ const maxTime = isFinite(audio.duration) ? audio.duration : time;+ audio.currentTime = Math.max(0, Math.min(time, maxTime));+ };++ const seekByPercent = (percent) => {+ const audio = getAudioElement();+ if (!audio || !duration.value || !isFinite(duration.value)) return;++ const time = (percent / 100) * duration.value;+ audio.currentTime = time;+ };++ const setVolume = (value) => {+ const audio = getAudioElement();+ if (audio) {+ audio.volume = Math.max(0, Math.min(1, value));+ volume.value = audio.volume;+ }+ };++ const toggleMute = () => {+ const audio = getAudioElement();+ if (!audio) return;++ if (audio.muted || audio.volume === 0) {+ audio.muted = false;+ if (audio.volume === 0) {+ audio.volume = 0.5;+ }+ isMuted.value = false;+ } else {+ audio.muted = true;+ isMuted.value = true;+ }+ };++ // --- Progress Bar Drag Support ---++ const startProgressDrag = (event) => {+ const bar = event.currentTarget.querySelector('.h-2') || event.currentTarget;+ const rect = bar.getBoundingClientRect();+ const isTouch = event.type === 'touchstart';++ const getPercent = (evt) => {+ const clientX = isTouch ? evt.touches[0].clientX : evt.clientX;+ return Math.max(0, Math.min(100, ((clientX - rect.left) / rect.width) * 100));+ };++ // Start dragging - show preview+ isDragging.value = true;+ dragPreviewPercent.value = getPercent(event);++ const onMove = (evt) => {+ evt.preventDefault();+ const clientX = isTouch ? evt.touches[0].clientX : evt.clientX;+ dragPreviewPercent.value = Math.max(0, Math.min(100, ((clientX - rect.left) / rect.width) * 100));+ };++ const onUp = () => {+ document.removeEventListener(isTouch ? 'touchmove' : 'mousemove', onMove);+ document.removeEventListener(isTouch ? 'touchend' : 'mouseup', onUp);+ // Seek to final position on release+ seekByPercent(dragPreviewPercent.value);+ isDragging.value = false;+ };++ document.addEventListener(isTouch ? 'touchmove' : 'mousemove', onMove, { passive: false });+ document.addEventListener(isTouch ? 'touchend' : 'mouseup', onUp);+ };++ // --- Utility ---++ const formatTime = (seconds) => {+ if (!seconds || isNaN(seconds)) return '0:00';+ const hours = Math.floor(seconds / 3600);+ const mins = Math.floor((seconds % 3600) / 60);+ const secs = Math.floor(seconds % 60);+ if (hours > 0) {+ return `${hours}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;+ }+ return `${mins}:${secs.toString().padStart(2, '0')}`;+ };++ /**+ * Reset all player state (call when changing recordings)+ */+ const reset = () => {+ isPlaying.value = false;+ currentTime.value = 0;+ duration.value = 0;+ isMuted.value = false;+ isLoading.value = false;+ hasServerDuration = false;+ };++ /**+ * Initialize with a recording object.+ * Automatically uses server-side duration if available.+ */+ const initWithRecording = (recording) => {+ reset();+ if (recording && recording.audio_duration) {+ setServerDuration(recording.audio_duration);+ }+ };++ return {+ // State+ isPlaying,+ currentTime,+ duration,+ isMuted,+ isLoading,+ volume,+ isDragging,+ dragPreviewPercent,++ // Computed+ progressPercent,+ displayCurrentTime,++ // Duration management+ setServerDuration,+ trySetBrowserDuration,++ // Event handlers (wire these to <audio> element)+ handlePlayPause,+ handleLoadedMetadata,+ handleDurationChange,+ handleTimeUpdate,+ handleEnded,+ handleWaiting,+ handleCanPlay,+ handleVolumeChange,++ // Actions+ play,+ pause,+ togglePlayback,+ seekTo,+ seekByPercent,+ setVolume,+ toggleMute,+ startProgressDrag,+ setAudioElementGetter,++ // Utility+ formatTime,+ reset,+ initWithRecording+ };+}++/**+ * Create a standalone audio player instance.+ * Use this for pages that don't have Vue's ref/computed (like share.html).+ */+export function createStandalonePlayer(Vue) {+ const { ref, computed } = Vue;+ return useAudioPlayer(ref, computed);+}diff --git a/static/js/modules/composables/ui.js b/static/js/modules/composables/ui.jsindex 2f3e6d9f..3157cf08 100644--- a/static/js/modules/composables/ui.js+++ b/static/js/modules/composables/ui.js@@ -779,20 +779,19 @@ export function useUI(state, utils, processedTranscription) {audioPlayer = document.querySelector('.main-content-area audio');}- if (audioPlayer) {+ if (!audioPlayer) {+ audioPlayer = document.querySelector('audio');+ }++ if (audioPlayer && isFinite(time)) {const wasPlaying = !audioPlayer.paused;- audioPlayer.currentTime = time;- if (wasPlaying) {- audioPlayer.play();- }- } else {- const oldPlayer = document.querySelector('audio');- if (oldPlayer) {- const wasPlaying = !oldPlayer.paused;- oldPlayer.currentTime = time;+ try {+ audioPlayer.currentTime = time;if (wasPlaying) {- oldPlayer.play();+ audioPlayer.play().catch(e => console.warn('Play after seek failed:', e));}+ } catch (e) {+ console.warn('Seek failed:', e);}}};@@ -868,17 +867,29 @@ export function useUI(state, utils, processedTranscription) {const seekAudioTo = (time) => {const audio = getAudioElement();- if (!audio) return;+ if (!audio || !isFinite(time)) return;- audio.currentTime = Math.max(0, Math.min(time, audio.duration || 0));+ // Use our tracked duration (server-side) as fallback if browser duration is broken+ const maxTime = audioDuration.value || (isFinite(audio.duration) ? audio.duration : time);+ try {+ audio.currentTime = Math.max(0, Math.min(time, maxTime));+ } catch (e) {+ console.warn('Seek failed:', e);+ }};const seekAudioByPercent = (percent) => {const audio = getAudioElement();- if (!audio || !audio.duration) return;+ // Use our tracked duration (server-side) which works for WebM files without duration metadata+ const dur = audioDuration.value || audio?.duration;+ if (!audio || !dur || !isFinite(dur)) return;- const time = (percent / 100) * audio.duration;- audio.currentTime = time;+ const time = (percent / 100) * dur;+ try {+ audio.currentTime = time;+ } catch (e) {+ console.warn('Seek by percent failed:', e);+ }};// Progress bar drag state@@ -929,9 +940,12 @@ export function useUI(state, utils, processedTranscription) {const handleAudioLoadedMetadata = (event) => {const duration = event.target.duration;- // Duration might be Infinity for some formats until more data loads- if (duration && isFinite(duration) && duration > 0) {- audioDuration.value = duration;+ // Only set browser duration if we don't already have a server-side duration+ // Server-side duration (from ffprobe) is more reliable for formats like WebM+ if (!audioDuration.value || audioDuration.value === 0) {+ if (duration && isFinite(duration) && duration > 0) {+ audioDuration.value = duration;+ }}audioIsLoading.value = false;};@@ -972,6 +986,18 @@ export function useUI(state, utils, processedTranscription) {}};+ const handleAudioDurationChange = (event) => {+ // WebM and some other formats may initially report Infinity duration+ // This handler catches when the actual duration becomes available+ // Only set if we don't already have a server-side duration+ if (!audioDuration.value || audioDuration.value === 0) {+ const duration = event.target.duration;+ if (duration && isFinite(duration) && duration > 0) {+ audioDuration.value = duration;+ }+ }+ };+const formatAudioTime = (seconds) => {if (!seconds || isNaN(seconds)) return '0:00';const mins = Math.floor(seconds / 60);@@ -1462,9 +1488,13 @@ export function useUI(state, utils, processedTranscription) {}// Watch for recording changes to reset active segment and audio player state- watch(selectedRecording, () => {+ watch(selectedRecording, (newRecording) => {currentPlayingSegmentIndex.value = null;resetAudioPlayerState();+ // Use server-side duration if available (more reliable than browser metadata)+ if (newRecording && newRecording.audio_duration) {+ audioDuration.value = newRecording.audio_duration;+ }});// Set up global click handler to close dropdowns when clicking outside@@ -1775,6 +1805,7 @@ export function useUI(state, utils, processedTranscription) {handleCustomAudioTimeUpdate,handleAudioWaiting,handleAudioCanPlay,+ handleAudioDurationChange,formatAudioTime,audioProgressPercent,displayCurrentTime,diff --git a/templates/components/detail/audio-player.html b/templates/components/detail/audio-player.htmlindex 839b81f8..9206f047 100644--- a/templates/components/detail/audio-player.html+++ b/templates/components/detail/audio-player.html@@ -16,6 +16,7 @@@pause="handleAudioPlayPause"@timeupdate="handleCustomAudioTimeUpdate"@loadedmetadata="handleAudioLoadedMetadata"+ @durationchange="handleAudioDurationChange"@ended="handleAudioEnded"@waiting="handleAudioWaiting"@canplay="handleAudioCanPlay"diff --git a/templates/components/detail/desktop-right-panel.html b/templates/components/detail/desktop-right-panel.htmlindex 835034db..75a9d0c7 100644--- a/templates/components/detail/desktop-right-panel.html+++ b/templates/components/detail/desktop-right-panel.html@@ -18,6 +18,7 @@@pause="handleAudioPlayPause"@timeupdate="handleCustomAudioTimeUpdate"@loadedmetadata="handleAudioLoadedMetadata"+ @durationchange="handleAudioDurationChange"@ended="handleAudioEnded"@waiting="handleAudioWaiting"@canplay="handleAudioCanPlay"diff --git a/templates/share.html b/templates/share.htmlindex 8e6c8e3c..119ea0dc 100644--- a/templates/share.html+++ b/templates/share.html@@ -115,6 +115,7 @@@pause="handleAudioPlayPause"@timeupdate="handleCustomAudioTimeUpdate"@loadedmetadata="handleAudioLoadedMetadata"+ @durationchange="handleAudioDurationChange"@ended="handleAudioEnded"></audio><!-- Play/Pause -->@@ -347,7 +348,8 @@// Custom audio player stateconst audioIsPlaying = ref(false);const audioCurrentTime = ref(0);- const audioDuration = ref(0);+ // Use server-side duration if available (more reliable for formats like WebM)+ const audioDuration = ref(recordingData.audio_duration || 0);const audioIsMuted = ref(false);const playerVolume = ref(1.0);@@ -379,7 +381,23 @@}function handleAudioLoadedMetadata(event) {- audioDuration.value = event.target.duration;+ // Only set browser duration if we don't already have a server-side duration+ if (!audioDuration.value || audioDuration.value === 0) {+ const duration = event.target.duration;+ if (duration && isFinite(duration) && duration > 0) {+ audioDuration.value = duration;+ }+ }+ }++ function handleAudioDurationChange(event) {+ // Only set browser duration if we don't already have a server-side duration+ if (!audioDuration.value || audioDuration.value === 0) {+ const duration = event.target.duration;+ if (duration && isFinite(duration) && duration > 0) {+ audioDuration.value = duration;+ }+ }}function handleAudioEnded() {@@ -413,7 +431,7 @@}function seekAudioByPercent(percent) {- if (!audioPlayer.value || !audioDuration.value) return;+ if (!audioPlayer.value || !audioDuration.value || !isFinite(audioDuration.value)) return;const time = (percent / 100) * audioDuration.value;audioPlayer.value.currentTime = time;}@@ -640,6 +658,7 @@handleAudioPlayPause,handleCustomAudioTimeUpdate,handleAudioLoadedMetadata,+ handleAudioDurationChange,handleAudioEnded,toggleAudioPlayback,toggleAudioMute,