Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
if: runner.os == 'Linux'
run: make wasm-smoke

- name: Test (WebAssembly, Node)
if: runner.os == 'Linux'
run: GOOS=js GOARCH=wasm go test -count=1 -exec "$(go env GOROOT)/lib/wasm/go_js_wasm_exec" ./cmd/mkvgo-wasm/

- name: Build (32-bit)
shell: bash
run: GOOS=linux GOARCH=arm CGO_ENABLED=0 go build ./...
Expand Down
39 changes: 28 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,35 @@ its series' file in [changelog/](changelog/) when a newer one ships.

## [Unreleased]

## [0.66.1] - 2026-11-08
## [0.66.2] - 2026-11-10

### Fixed
### Security

- HLS plans (`serve`, `Open`): a chmod, a chown or a hard link of the source
no longer counts as a changed source. 0.66.0 had added the inode change
time to the source stamp, so any of them made every later segment of a
running serve fail with `ErrSourceChanged` (HTTP 502) until the process was
restarted, and a player gave up after its retries. The stamp is the size,
the mtime and the file identity again; a file rewritten in place at the
same size with its mtime put back is, as before 0.66.0, not detected.
- `serve`, `PlanHLS`: a Matroska whose Segment declares more bytes than the
file holds (a forged size, a file cut short) no longer has a subtitle
request ask the kernel to read ahead over the declared size, one call per
chunk, with the rendition locked meanwhile (since 0.66.0).
- `edit-inplace`: a Tags, Chapters or Attachments element after the clusters
that declares more bytes than the file holds is voided up to the end of the
file; the edit wrote its declared size, from one allocation of that size.
- `demux`: an empty ALAC packet is refused by name (`--raw` keeps the stored
payloads); a few bytes of laced empty packets held 2 KiB of packet table
each (since 0.66.0).

## Older versions
### Fixed

Archived in [changelog/](changelog/): [0.60-0.69](changelog/0.60-0.69.md), [0.56-0.59](changelog/0.56-0.59.md), [0.50-0.55](changelog/0.50-0.55.md), [0.40-0.49](changelog/0.40-0.49.md), [0.30-0.39](changelog/0.30-0.39.md), [0.20-0.29](changelog/0.20-0.29.md), [0.10-0.19](changelog/0.10-0.19.md), [0.3-0.9](changelog/0.3-0.9.md).
- `edit-inplace`: a second edit of a file whose Info or Tracks an earlier
edit had moved to the end of the Segment replaces them. It appended a
second copy behind the first, which a full read kept returning: the edit
reported success and was not seen (since 0.66.0). A file edited twice by
0.66.0 or 0.66.1 is repaired by `mkvgo edit` (full rewrite).
- `to-hls`, `serve` and the other HLS commands: an encrypted text subtitle
track is refused by name, or dropped with its reason under
`--skip-unsupported`, as `to-mp4` does; since 0.66.0 it was listed as a
WebVTT rendition built from its undecrypted bytes.
- `serve` on a remote source with a subtitle index: the scan asks for each
block it reads again, not for the whole minute of media ahead in one request
(since 0.66.0).
- Every command names `--strict`, `--skip-unsupported`, `--synthesize-index`
and `--no-iframes` in its refusals; all but `demux`, `to-mp4`, `from-mp4`
and `to-hls` printed the library's `Options` field names.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ preflight:
GOOS=$$os CGO_ENABLED=0 go build ./... || exit 1; \
GOOS=$$os CGO_ENABLED=0 go vet ./... || exit 1; \
done
@echo "== wasm build =="; GOOS=js GOARCH=wasm CGO_ENABLED=0 go build ./cmd/mkvgo-wasm/
@echo "== wasm build+vet =="; GOOS=js GOARCH=wasm CGO_ENABLED=0 go build ./cmd/mkvgo-wasm/ && GOOS=js GOARCH=wasm CGO_ENABLED=0 go vet ./cmd/mkvgo-wasm/
@echo "== GOARCH=arm (32-bit) build =="; GOOS=linux GOARCH=arm CGO_ENABLED=0 go build ./...
@echo "== lint =="; \
lint="$$(command -v golangci-lint || echo "$$(go env GOPATH)/bin/golangci-lint")"; \
Expand Down
16 changes: 16 additions & 0 deletions changelog/0.60-0.69.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

Archived entries of mkvgo 0.60.x to 0.69.x, as they were published; a version moves here once a newer one ships. The current versions are in [CHANGELOG.md](../CHANGELOG.md).

## [0.66.1] - 2026-11-08

### Fixed

- HLS plans (`serve`, `Open`): a chmod, a chown or a hard link of the source
no longer counts as a changed source. 0.66.0 had added the inode change
time to the source stamp, so any of them made every later segment of a
running serve fail with `ErrSourceChanged` (HTTP 502) until the process was
restarted, and a player gave up after its retries. The stamp is the size,
the mtime and the file identity again; a file rewritten in place at the
same size with its mtime put back is, as before 0.66.0, not detected.

## Older versions

Archived in [changelog/](changelog/): [0.60-0.69](changelog/0.60-0.69.md), [0.56-0.59](changelog/0.56-0.59.md), [0.50-0.55](changelog/0.50-0.55.md), [0.40-0.49](changelog/0.40-0.49.md), [0.30-0.39](changelog/0.30-0.39.md), [0.20-0.29](changelog/0.20-0.29.md), [0.10-0.19](changelog/0.10-0.19.md), [0.3-0.9](changelog/0.3-0.9.md).

## [0.66.0] - 2026-11-08

### Breaking
Expand Down
4 changes: 3 additions & 1 deletion cmd/mkvgo/commands/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func FatalErr(err error) {
dieOfInterrupt()
return
}
msg := err.Error()
msg := flagPhrases.Replace(err.Error()) // every command exits here: the library's option names read as its flags
if errors.Is(err, matroska.ErrDamagedRegion) {
msg += "\nrepair it with `mkvgo reindex --resync`"
}
Expand All @@ -227,6 +227,8 @@ var flagPhrases = strings.NewReplacer(
"which DemuxOptions.Strict refuses", "which --strict refuses",
"which Options.Strict refuses", "which --strict refuses",
"set Options.FlattenStyledSubs", "pass --flatten-subs",
"plan with Options.SynthesizeIndex", "pass --synthesize-index",
"Options.NoIFrames is set", "--no-iframes is set",
)

func (f flagWords) Error() string { return flagPhrases.Replace(f.error.Error()) }
Expand Down
22 changes: 22 additions & 0 deletions cmd/mkvgo/commands/strict_split_cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package commands_test

import (
"path/filepath"
"strings"
"testing"

"github.com/gravity-zero/mkvgo/cmd/mkvgo/commands"
)

// --strict is global: a command other than the remuxes that refuses a dropped track names the flag too, not the
// library's Options field (split printed "which Options.Strict refuses").
func TestStrictRefusalNamesTheFlagOnEveryCommand(t *testing.T) {
commands.Strict = true
t.Cleanup(func() { commands.Strict = false })
msg := fatalMessage(t, 1, func() {
commands.CmdSplit([]string{"../../../internal/testdata/tmcd.mov", "-o", filepath.Join(t.TempDir(), "parts"), "-every", "1000"})
})
if !strings.Contains(msg, "--strict") || strings.Contains(msg, "Options.") {
t.Errorf("split --strict: %q, want the track refused by --strict", msg)
}
}
23 changes: 23 additions & 0 deletions mkv/ops/demux_caf_empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ops

import (
"errors"
"io"
"testing"

"github.com/gravity-zero/mkvgo/mkv"
)

// An empty ALAC packet is refused: a laced block of 7 bytes holds 256 of them, each one more entry of the packet
// table kept until the end of the stream, 2 KiB of memory for 7 bytes read.
func TestDemuxCAFRefusesAnEmptyPacket(t *testing.T) {
f := cafALACFormat(mkv.Track{CodecPrivate: make([]byte, 24)})
if f == nil {
t.Fatal("no CAF format for a 24-byte cookie")
}
err := f.frame(io.Discard, mkv.Block{}, nil)
var fe *frameError
if !errors.As(err, &fe) {
t.Errorf("an empty packet: err = %v, want a frame the file cannot hold", err)
}
}
18 changes: 9 additions & 9 deletions mkv/ops/demux_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ func cafALACFormat(t mkv.Track) *demuxFormat {
if rate == 0 && t.SampleRate != nil {
rate = *t.SampleRate
}
var sizes []int64
var dataSizePos, total int64
var table []byte // the packet table: each packet's size as a varint, 1 or 2 bytes a packet
var dataSizePos, total, packets int64
return &demuxFormat{ext: "caf",
head: func(w io.Writer) error {
var b []byte
Expand All @@ -227,8 +227,12 @@ func cafALACFormat(t mkv.Track) *demuxFormat {
return err
},
frame: func(w io.Writer, _ mkv.Block, data []byte) error {
sizes = append(sizes, int64(len(data)))
if len(data) == 0 {
return cannotHold("empty ALAC packet")
}
table = cafVarint(table, uint64(len(data)))
total += int64(len(data))
packets++
_, err := w.Write(data)
return err
},
Expand All @@ -244,14 +248,10 @@ func cafALACFormat(t mkv.Track) *demuxFormat {
if _, err := f.Seek(0, io.SeekEnd); err != nil {
return err
}
var table []byte
for _, n := range sizes {
table = cafVarint(table, uint64(n))
}
var pakt []byte
pakt = cafChunk(pakt, "pakt", int64(24+len(table)))
pakt = binary.BigEndian.AppendUint64(pakt, uint64(len(sizes)))
pakt = binary.BigEndian.AppendUint64(pakt, uint64(len(sizes))*uint64(framesPerPacket))
pakt = binary.BigEndian.AppendUint64(pakt, uint64(packets))
pakt = binary.BigEndian.AppendUint64(pakt, uint64(packets)*uint64(framesPerPacket))
pakt = binary.BigEndian.AppendUint32(pakt, 0) // priming frames
pakt = binary.BigEndian.AppendUint32(pakt, 0) // remainder frames
pakt = append(pakt, table...)
Expand Down
17 changes: 13 additions & 4 deletions mkv/ops/inplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,13 +765,21 @@ func scanTail(f mkv.ReadSeekCloser, pos, segEnd int64, region *metadataRegion) e
region.tailUnknown = true // more than one: not one element to keep or rewrite
return nil
}
region.tail[eh.ID] = tailElement{pos: pos, size: int64(hdrLen) + eh.Size}
region.tail[eh.ID] = tailElement{pos: pos, size: heldSize(f, pos, int64(hdrLen)+eh.Size)}
}
pos += int64(hdrLen) + eh.Size
}
return nil
}

// heldSize is how much of an element declaring size bytes at pos the file holds: a file cut short ends it early.
func heldSize(f io.Seeker, pos, size int64) int64 {
if end, err := f.Seek(0, io.SeekEnd); err == nil && pos+size > end {
return end - pos
}
return size
}

// classifyTailEntries re-reads the old SeekHead and sorts its entries: those
// pointing outside the head region are preserved for the rebuilt SeekHead -
// except a post-cluster Tags, Chapters or Attachments element, recorded with its
Expand All @@ -786,7 +794,8 @@ func classifyTailEntries(f mkv.ReadSeekCloser, path string, fs *mkv.FS, region *
if abs >= region.start && abs < region.end {
continue // head element: rewritten, gets a fresh entry
}
if isTailID(e.ID) {
movedOut := (e.ID == mkv.IDInfo || e.ID == mkv.IDTracks) && !region.inHead[e.ID] // an earlier edit moved it to the Segment's end
if isTailID(e.ID) || movedOut {
if _, err := f.Seek(abs, io.SeekStart); err != nil {
return err
}
Expand All @@ -798,10 +807,10 @@ func classifyTailEntries(f mkv.ReadSeekCloser, path string, fs *mkv.FS, region *
region.tailUnknown = true
continue
}
region.tail[e.ID] = tailElement{pos: abs, size: int64(hdrLen) + th.Size, entry: e}
region.tail[e.ID] = tailElement{pos: abs, size: heldSize(f, abs, int64(hdrLen)+th.Size), entry: e}
continue
}
if (e.ID == mkv.IDInfo || e.ID == mkv.IDTracks) && region.inHead[e.ID] {
if e.ID == mkv.IDInfo || e.ID == mkv.IDTracks {
continue // stale: the head holds the element, which gets a fresh entry or moves with one
}
region.keptSeekEntries = append(region.keptSeekEntries, e)
Expand Down
50 changes: 50 additions & 0 deletions mkv/ops/inplace_append_twice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ops

import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/gravity-zero/mkvgo/mkv"
"github.com/gravity-zero/mkvgo/mkv/reader"
)

// A second edit of a file whose Info an earlier edit moved to the Segment's end is the one every reader sees, and
// leaves one Info in the file: it appended a second copy behind the first, which the readers kept returning.
func TestEditInPlaceTwiceOnATightHeadKeepsTheLastEdit(t *testing.T) {
path := filepath.Join(t.TempDir(), "tight.mkv")
tightHeadMKV(t, path, nil)
ctx := context.Background()
first, second := "A"+strings.Repeat("a", 300), "B"+strings.Repeat("b", 600)
mustNil(t, EditInPlace(ctx, path, func(c *mkv.Container) { c.Info.Title = first }))
mustNil(t, EditInPlace(ctx, path, func(c *mkv.Container) { c.Info.Title = second }))
full, err := reader.Open(ctx, path)
mustNil(t, err)
meta, err := reader.OpenMeta(ctx, path)
mustNil(t, err)
if full.Info.Title != second || meta.Info.Title != second {
t.Errorf("titles after the second edit: full read %.1q..., head read %.1q..., want B", full.Info.Title, meta.Info.Title)
}
for _, name := range []string{"N" + strings.Repeat("n", 900), "M" + strings.Repeat("m", 1800)} {
mustNil(t, EditInPlace(ctx, path, func(c *mkv.Container) { c.Tracks[0].Name = name }))
full, err = reader.Open(ctx, path)
mustNil(t, err)
meta, err = reader.OpenMeta(ctx, path)
mustNil(t, err)
if full.Tracks[0].Name != name || meta.Tracks[0].Name != name || full.Info.Title != second || meta.Info.Title != second {
t.Errorf("after naming the track %.1q...: full read %.1q... title %.1q..., head read %.1q... title %.1q...", name,
full.Tracks[0].Name, full.Info.Title, meta.Tracks[0].Name, meta.Info.Title)
}
}
data, err := os.ReadFile(path)
mustNil(t, err)
if n := bytes.Count(data, []byte(first)); n != 0 {
t.Errorf("the first edit's title is still in the file %d time(s)", n)
}
if n := bytes.Count(data, []byte("N"+strings.Repeat("n", 900))); n != 0 {
t.Errorf("the first track name is still in the file %d time(s)", n)
}
}
28 changes: 28 additions & 0 deletions mkv/ops/inplace_tail_past_eof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ops

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/gravity-zero/mkvgo/mkv"
)

// A tail Tags element declaring more bytes than the file holds (a file cut short, a forged size) is not voided over
// its declared size: the edit wrote that many bytes, past the end of the file, from one allocation of the same size.
func TestEditInPlaceVoidsNoTailElementPastTheFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "cut.mkv")
tailTagsMKV(t, path, 2)
whole, err := os.Stat(path)
mustNil(t, err)
mustNil(t, os.Truncate(path, whole.Size()-100))
err = EditInPlace(context.Background(), path, func(c *mkv.Container) {
c.Tags = []mkv.Tag{{TargetType: "MOVIE", SimpleTags: []mkv.SimpleTag{{Name: "A", Value: "b"}}}}
})
after, serr := os.Stat(path)
mustNil(t, serr)
if after.Size() > whole.Size()-100 {
t.Errorf("the edit (err %v) grew a file of %d bytes to %d", err, whole.Size()-100, after.Size())
}
}
5 changes: 4 additions & 1 deletion mkv/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,10 @@ func WriteVoid(w io.Writer, totalSize int) error {
if _, err := w.Write(hdr); err != nil {
return err
}
_, err = w.Write(make([]byte, totalSize-len(hdr)))
zeros := make([]byte, min(totalSize-len(hdr), 64<<10))
for left := totalSize - len(hdr); left > 0 && err == nil; left -= len(zeros) {
_, err = w.Write(zeros[:min(left, len(zeros))])
}
return err
}

Expand Down
10 changes: 10 additions & 0 deletions mp4/hls.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,16 @@ func planSubTracks(c *mkv.Container, o Options) ([]hlsSubTrack, error) {
if t.Type != mkv.SubtitleTrack {
continue
}
if t.Encryption != "" { // its bytes are not its cues: refused or dropped as towards MP4, never served as text
reason := "encrypted (" + t.Encryption + "): mkvgo does not decrypt"
if !o.SkipUnsupported {
return nil, errf("track %d (%s) is %s (set Options.SkipUnsupported to drop it)", t.ID, t.Codec, reason)
}
if err := o.drop(DroppedTrack{ID: t.ID, Type: t.Type, Codec: t.Codec, Reason: reason}); err != nil {
return nil, err
}
continue
}
var reason string
switch canonicalSubCodec(t.Codec) {
case "srt", "webvtt", "ass", "ssa":
Expand Down
25 changes: 25 additions & 0 deletions mp4/hls_encrypted_subtitle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mp4

import (
"strings"
"testing"

"github.com/gravity-zero/mkvgo/mkv"
)

// An encrypted text subtitle is no WebVTT rendition: refused by name, or dropped with its reason under
// SkipUnsupported, as towards MP4. The HLS producers listed it and built its cues from the undecrypted bytes.
func TestHLSCarriesNoEncryptedSubtitle(t *testing.T) {
c := &mkv.Container{Tracks: []mkv.Track{{ID: 3, Type: mkv.SubtitleTrack, Codec: "srt", Encryption: "AES-CTR"}}}
if subs, err := planSubTracks(c, Options{}); err == nil || !strings.Contains(err.Error(), "track 3 (srt) is encrypted") {
t.Errorf("%d renditions, err = %v; want the track refused as encrypted", len(subs), err)
}
var dropped []DroppedTrack
subs, err := planSubTracks(c, Options{SkipUnsupported: true, OnDrop: func(d DroppedTrack) { dropped = append(dropped, d) }})
if err != nil || len(subs) != 0 || len(dropped) != 1 || dropped[0].ID != 3 {
t.Errorf("under SkipUnsupported: %d renditions, dropped %+v, err %v; want none, track 3 dropped", len(subs), dropped, err)
}
if _, err := planSubTracks(c, Options{SkipUnsupported: true, Strict: true}); err == nil {
t.Error("Strict let an encrypted subtitle be left out")
}
}
2 changes: 1 addition & 1 deletion mp4/hlsplan_ahead.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (p *HLSPlan) hintRange(src io.Reader, start, end int64) {
// first window at or past ms, plus the look into it, else the Segment's end.
func (p *HLSPlan) clusterEndOff(ms int64) int64 {
end := p.segEnd
if end <= 0 && p.srcInfo != nil { // a Segment of unknown size ends with the file
if p.srcInfo != nil && (end <= 0 || end > p.srcInfo.Size()) { // a Segment of unknown size ends with the file, and none runs past it
end = p.srcInfo.Size()
}
if ms == math.MaxInt64 {
Expand Down
Loading
Loading