Skip to content

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

Python

Fix the pre-upload recording review: normalize live MediaRecorder blob duration so length and seeking work on audio and video, and make the stitch path attach review-pane tags with tag/folder ASR defaults and tag_id (previously dropped)

Murtaza Nasir9ba8945dd8bf
diff --git a/src/services/recording_stitch.py b/src/services/recording_stitch.py
index d987a95c..15df6d9a 100644
--- a/src/services/recording_stitch.py
+++ b/src/services/recording_stitch.py
@@ -221,6 +221,81 @@ def _assemble_session_audio(chunk_paths: list, output_path: str, mime_type: str)
shutil.rmtree(work_dir, ignore_errors=True)
+def _apply_finalize_metadata(recording, metadata):
+ """Attach the tags picked in the recorder's Upload Settings panel and
+ resolve tag/folder ASR-option defaults, mirroring ``upload_file``.
+
+ The finalize metadata carries ``tags`` (the tag objects selected in the
+ review pane), but the stitch path historically dropped them — tags chosen
+ before clicking Upload were silently lost. This creates the RecordingTag
+ associations (ownership-checked, order preserved) and fills any ASR
+ options the user left blank from the first tag's defaults, falling back
+ to the folder's defaults when no tags are selected — the same precedence
+ chain as a drag-drop upload. Mutates and returns ``metadata`` so the
+ transcribe kickoff sees the resolved options.
+ """
+ from src.models import Tag, RecordingTag
+ from src.models.organization import Folder, GroupMembership
+
+ raw_tags = metadata.get('tags') or []
+ tag_ids = []
+ for entry in raw_tags:
+ tid = entry.get('id') if isinstance(entry, dict) else entry
+ try:
+ tag_ids.append(int(tid))
+ except (TypeError, ValueError):
+ continue
+
+ selected_tags = []
+ for order, tid in enumerate(tag_ids, 1):
+ tag = db.session.get(Tag, tid)
+ if not tag:
+ continue
+ allowed = tag.user_id == recording.user_id or (
+ tag.group_id
+ and GroupMembership.query.filter_by(
+ group_id=tag.group_id, user_id=recording.user_id).first()
+ )
+ if not allowed:
+ logger.warning(
+ f"Skipping tag {tid} on recording {recording.id}: "
+ f"user {recording.user_id} has no access")
+ continue
+ if RecordingTag.query.filter_by(recording_id=recording.id, tag_id=tag.id).first():
+ selected_tags.append(tag)
+ continue
+ db.session.add(RecordingTag(
+ recording_id=recording.id,
+ tag_id=tag.id,
+ order=order,
+ added_at=datetime.utcnow(),
+ ))
+ selected_tags.append(tag)
+
+ first_tag = selected_tags[0] if selected_tags else None
+ folder = db.session.get(Folder, recording.folder_id) if recording.folder_id else None
+
+ def fill(key, attr):
+ if metadata.get(key):
+ return
+ if first_tag and getattr(first_tag, attr, None):
+ metadata[key] = getattr(first_tag, attr)
+ elif not selected_tags and folder and getattr(folder, attr, None):
+ metadata[key] = getattr(folder, attr)
+
+ fill('language', 'default_language')
+ fill('min_speakers', 'default_min_speakers')
+ fill('max_speakers', 'default_max_speakers')
+ fill('hotwords', 'default_hotwords')
+ fill('initial_prompt', 'default_initial_prompt')
+ fill('transcription_model', 'default_transcription_model')
+
+ if first_tag is not None:
+ metadata['tag_id'] = first_tag.id
+
+ return metadata
+
+
def stitch_recording_session(session_id: str) -> Tuple[int, str]:
"""Stitch a session's chunks into a final audio file.
@@ -318,6 +393,10 @@ def stitch_recording_session(session_id: str) -> Tuple[int, str]:
if not recording.meeting_date:
recording.meeting_date = session.created_at
+ # Attach the tags picked in the review pane and resolve tag/folder
+ # ASR-option defaults into the metadata (same precedence as upload_file).
+ metadata = _apply_finalize_metadata(recording, metadata)
+
session.status = 'finalized'
session.finalized_at = datetime.utcnow()
session.last_seen_at = datetime.utcnow()
@@ -354,6 +433,7 @@ def kickoff_transcription_for_stitched(
'language': metadata.get('language') or metadata.get('asr_language'),
'min_speakers': metadata.get('min_speakers'),
'max_speakers': metadata.get('max_speakers'),
+ 'tag_id': metadata.get('tag_id'),
'hotwords': metadata.get('hotwords'),
'initial_prompt': metadata.get('initial_prompt'),
'transcription_model': metadata.get('transcription_model'),
diff --git a/static/js/modules/composables/audio.js b/static/js/modules/composables/audio.js
index 4701219e..47427e83 100644
--- a/static/js/modules/composables/audio.js
+++ b/static/js/modules/composables/audio.js
@@ -119,6 +119,33 @@ export function useAudio(state, utils) {
}
}
+ // Fix duration/seeking on the finished-recording review players.
+ //
+ // A raw MediaRecorder blob is a "live" container: no duration metadata
+ // and no seek index, so <audio>/<video> report duration=Infinity — the
+ // length doesn't display and the seek bar is dead. (The server-side
+ // stitch fixes this for the STORED file with an ffmpeg remux pass; this
+ // is the client-side equivalent for the pre-upload review.) The standard
+ // fix: seek far past the end, which forces the browser to scan the
+ // stream, compute the real duration, and build its seek index; then snap
+ // back to the start. Wired to @loadedmetadata on both review players.
+ const normalizeLiveMediaDuration = (event) => {
+ const el = event && event.target;
+ if (!el || Number.isFinite(el.duration)) return;
+ const restore = () => {
+ el.removeEventListener('timeupdate', restore);
+ el.removeEventListener('seeked', restore);
+ if (el.currentTime > 0) el.currentTime = 0;
+ };
+ el.addEventListener('timeupdate', restore);
+ el.addEventListener('seeked', restore);
+ try {
+ el.currentTime = Number.MAX_SAFE_INTEGER;
+ } catch (_) {
+ restore();
+ }
+ };
+
// Detach the live stream from the preview element (called on stop — the
// tracks are ended at that point). recordingVideoActive stays true so the
// finished-recording review pane knows to render a <video> player for the
@@ -1350,6 +1377,7 @@ export function useAudio(state, utils) {
startRecording,
stopRecording,
discardRecording,
+ normalizeLiveMediaDuration,
uploadRecordedAudio,
uploadRecordedAudioIncognito,
acceptRecordingDisclaimer,
diff --git a/templates/components/recording-view.html b/templates/components/recording-view.html
index e979f465..28f492e3 100644
--- a/templates/components/recording-view.html
+++ b/templates/components/recording-view.html
@@ -43,10 +43,12 @@
recordings keep the compact audio bar. -->
<div v-if="recordingVideoActive" class="max-w-2xl mx-auto">
<video :src="audioBlobURL" controls playsinline
+ @loadedmetadata="normalizeLiveMediaDuration"
class="w-full max-h-64 md:max-h-80 rounded-lg bg-black object-contain"></video>
</div>
<div v-else class="audio-player-container">
- <audio :src="audioBlobURL" controls class="w-full">
+ <audio :src="audioBlobURL" controls class="w-full"
+ @loadedmetadata="normalizeLiveMediaDuration">
Your browser does not support the audio element.
</audio>
</div>
diff --git a/tests/test_recording_stitch.py b/tests/test_recording_stitch.py
index aab97786..f6e97d77 100644
--- a/tests/test_recording_stitch.py
+++ b/tests/test_recording_stitch.py
@@ -469,6 +469,92 @@ def test_stitch_probe_overrides_client_claimed_mime():
shutil.rmtree(upload_folder, ignore_errors=True)
+@_needs_webm
+def test_stitch_applies_review_pane_tags_and_tag_defaults():
+ """Tags picked in the recorder's Upload Settings panel arrive in the
+ finalize metadata; the stitch must create the RecordingTag associations
+ and resolve the first tag's ASR defaults into the transcribe metadata
+ (they were previously dropped)."""
+ from src.models import Tag, RecordingTag
+
+ upload_folder = tempfile.mkdtemp(prefix="speakr-stitch-tags-")
+ with app.app_context():
+ app.config["UPLOAD_FOLDER"] = upload_folder
+ user = _make_user()
+ tag = Tag(name=f"stitch-tag-{uuid.uuid4().hex[:6]}", user_id=user.id,
+ default_language="de", default_hotwords="Speakr, WhisperX")
+ db.session.add(tag)
+ db.session.commit()
+
+ recording, session, sess_dir = _plant_session(upload_folder, user, 'audio/webm', chunk_count=2)
+ session.finalize_metadata = json.dumps({
+ "tags": [{"id": tag.id, "name": tag.name}],
+ "language": None,
+ })
+ db.session.commit()
+
+ src = os.path.join(upload_folder, 'src.webm')
+ _generate_webm(src, duration_seconds=3)
+ _split_into_chunks(src, sess_dir, n_parts=2)
+ os.remove(src)
+
+ recording_id, media_path, metadata = stitch_recording_session(session.id)
+
+ assoc = RecordingTag.query.filter_by(recording_id=recording_id, tag_id=tag.id).first()
+ assert assoc is not None, "review-pane tag was not attached to the recording"
+ assert metadata.get('language') == 'de', "first tag's default language not resolved"
+ assert metadata.get('hotwords') == 'Speakr, WhisperX'
+ assert metadata.get('tag_id') == tag.id
+
+ os.remove(media_path)
+ db.session.delete(assoc)
+ db.session.delete(db.session.get(RecordingSession, session.id))
+ db.session.delete(db.session.get(Recording, recording_id))
+ db.session.delete(tag)
+ db.session.delete(user)
+ db.session.commit()
+ shutil.rmtree(upload_folder, ignore_errors=True)
+
+
+@_needs_webm
+def test_stitch_skips_foreign_tags():
+ """A tag id belonging to another user in the finalize metadata must NOT
+ be attached (ownership check mirrors upload_file)."""
+ from src.models import Tag, RecordingTag
+
+ upload_folder = tempfile.mkdtemp(prefix="speakr-stitch-foreigntag-")
+ with app.app_context():
+ app.config["UPLOAD_FOLDER"] = upload_folder
+ user = _make_user()
+ other = _make_user()
+ foreign_tag = Tag(name=f"foreign-{uuid.uuid4().hex[:6]}", user_id=other.id)
+ db.session.add(foreign_tag)
+ db.session.commit()
+
+ recording, session, sess_dir = _plant_session(upload_folder, user, 'audio/webm', chunk_count=1)
+ session.finalize_metadata = json.dumps({"tags": [{"id": foreign_tag.id}]})
+ db.session.commit()
+
+ src = os.path.join(upload_folder, 'src.webm')
+ _generate_webm(src, duration_seconds=3)
+ _split_into_chunks(src, sess_dir, n_parts=1)
+ os.remove(src)
+
+ recording_id, media_path, metadata = stitch_recording_session(session.id)
+
+ assert RecordingTag.query.filter_by(recording_id=recording_id).count() == 0
+ assert metadata.get('tag_id') is None
+
+ os.remove(media_path)
+ db.session.delete(db.session.get(RecordingSession, session.id))
+ db.session.delete(db.session.get(Recording, recording_id))
+ db.session.delete(foreign_tag)
+ db.session.delete(other)
+ db.session.delete(user)
+ db.session.commit()
+ shutil.rmtree(upload_folder, ignore_errors=True)
+
+
def test_stitch_raises_when_no_chunks_on_disk():
upload_folder = tempfile.mkdtemp(prefix="speakr-stitch-nochunks-")
with app.app_context():