Skip to content

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

Python

fix: write moov atom first (faststart) in merged, stitched and video-extracted m4a files so the ASR webservice can decode them from a pipe (#372)

Murtaza Nasir03d8edb0156c
diff --git a/src/services/recording_merge.py b/src/services/recording_merge.py
index 77864b59..382bbd74 100644
--- a/src/services/recording_merge.py
+++ b/src/services/recording_merge.py
@@ -109,6 +109,11 @@ def _concat_audio(input_paths: list, output_path: str) -> None:
'-map', '[out]',
'-c:a', 'aac',
'-b:a', _MERGED_BITRATE,
+ # Put the moov atom first. Without this, MP4 output cannot be decoded
+ # from a non-seekable pipe — the WhisperX ASR webservice pipes uploads
+ # to ffmpeg stdin and silently decodes 0 samples, which surfaces as a
+ # pyannote "(channel, time) torch Tensor" 500 (#372).
+ '-movflags', '+faststart',
output_path,
]
_run_ffmpeg_command(cmd, f"merge concat of {len(input_paths)} recordings")
diff --git a/src/services/recording_stitch.py b/src/services/recording_stitch.py
index f0306f9a..8187d1fa 100644
--- a/src/services/recording_stitch.py
+++ b/src/services/recording_stitch.py
@@ -145,7 +145,11 @@ def _remux_copy(src_path: str, output_path: str) -> None:
losing the recording — logged loudly so it can be investigated."""
cmd = [
'ffmpeg', '-hide_banner', '-loglevel', 'error', '-y',
- '-i', src_path, '-c', 'copy', output_path,
+ '-i', src_path, '-c', 'copy',
+ # MP4 outputs (Safari MediaRecorder emits audio/mp4) must be
+ # pipe-streamable for the ASR webservice: moov first (#372).
+ *(['-movflags', '+faststart'] if output_path.lower().endswith(('.m4a', '.mp4', '.mov')) else []),
+ output_path,
]
try:
result = subprocess.run(cmd, capture_output=True, timeout=600)
@@ -176,7 +180,9 @@ def _concat_demux(segment_files: list, output_path: str, work_dir: str) -> None:
cmd = [
'ffmpeg', '-hide_banner', '-loglevel', 'error', '-y',
'-f', 'concat', '-safe', '0', '-i', manifest_path,
- '-c', 'copy', output_path,
+ '-c', 'copy',
+ *(['-movflags', '+faststart'] if output_path.lower().endswith(('.m4a', '.mp4', '.mov')) else []),
+ output_path,
]
try:
result = subprocess.run(cmd, capture_output=True, timeout=600)
diff --git a/src/utils/ffmpeg_utils.py b/src/utils/ffmpeg_utils.py
index c90677e3..b0fafb8f 100644
--- a/src/utils/ffmpeg_utils.py
+++ b/src/utils/ffmpeg_utils.py
@@ -149,6 +149,9 @@ def extract_audio_from_video(
'-y',
'-vn', # No video
'-acodec', 'copy', # Copy audio stream without re-encoding
+ # m4a output must be pipe-streamable for the ASR
+ # webservice: moov atom first (#372).
+ *(['-movflags', '+faststart'] if output_ext == 'm4a' else []),
temp_audio_path
]