fix(transcoding): declare the duration of a piped mp3 transcode

The mp3 muxer writes the Xing frame that carries the frame count only after
rewinding to the start of the stream, which it cannot do on a pipe (-f mp3 -).
An uncached transcode therefore declares its length nowhere: no Xing frame, and
no Content-Length unless the client asks for the estimate. A decoder has to
guess the duration from the bytes it has so far, and an offline copy stays
unseekable, the same defect #6105 fixed for FLAC.

Transcode now inserts a Xing frame ahead of the first audio frame, carrying the
frame count derived from the track duration. Only the frame count is declared:
a piped encode cannot know its own byte size, and a wrong size is worse than an
absent one. Decoded audio is unchanged, since decoders skip the tag frame.

Measured on a 20s 128kbps transcode: ffprobe reports 20.010s instead of
20.036s, and Firefox reports 19.998s at loadedmetadata instead of 2.04s
climbing over nine durationchange events. This does not make an uncached
transcode resumable; that needs range support, which only the cache path has.

Reported in #6170.
This commit is contained in:
Deluan 2026-09-24 16:42:48 -04:00 • committed by Deluan Quintão
commit c05fbd0edb
4 changed files with 366 additions and 2 deletions

View file

@ -32,7 +32,7 @@ type TranscodeOptions struct {
Channels int // 0 = no constraint
BitDepth int // 0 = no constraint; valid values: 16, 24, 32
Offset int // seconds
Duration float32 // seconds; 0 = unknown. Only used to repair a piped FLAC header.
Duration float32 // seconds; 0 = unknown. Only used to repair a piped FLAC or mp3 header.
}
// AudioProbeResult contains authoritative audio stream properties from ffprobe.
@ -91,7 +91,13 @@ func (e *ffmpeg) Transcode(ctx context.Context, opts TranscodeOptions) (io.ReadC
if err != nil {
return nil, err
}
return patchFLACDuration(out, opts.Duration-float32(opts.Offset)), nil
duration := opts.Duration - float32(opts.Offset)
switch opts.Format {
case "mp3":
return patchMP3Duration(out, duration), nil
default:
return patchFLACDuration(out, duration), nil
}
}
func (e *ffmpeg) ConvertAnimatedImage(ctx context.Context, reader io.Reader, maxSize int, quality int) (io.ReadCloser, error) {