Skip to content

compile: every segment of the same source re-hashes the full media file twice (hundreds of GB read for long videos) #155

Closed

Description

@MarcelL94

What happens

compile (and add-video / add-audio) calls copyAssetDeduped() once per segment. From the second segment of the same source on, assets/<kind>/<basename> already exists, so planAssetCopy() calls sameContent(src, dest). That function SHA-1-hashes both files completely every time (dist/factory.js, fileSha1 / sameContent, introduced with the collision fix in #38).

For a talking-head video that is cut into many segments, the cost grows with segments × file size:

  • 63 segments from two camera files (3.0 GB + 2.7 GB): about 33 × 6 GB + 28 × 5.4 GB ≈ 350 GB read, ~20 minutes per compile on an NVMe SSD (~300 MB/s sustained, CPU mostly idle). The process was observed at 145 GB read / 2.9 GB written after 8 minutes.
  • Smaller cut lists finish fast only while the file fits in the OS page cache.

Reproduction

ffmpeg -f lavfi -i testsrc2=size=1920x1080:rate=30:duration=60 -f lavfi -i sine=duration=60 \
  -c:v libx264 -preset ultrafast -b:v 50M -c:a aac -shortest video.mp4      # ~270 MB

A spec with 40 one-second items on one video track, all "path": "video.mp4" with different sourceStart, then capcut compile spec.json --out draft --template <empty template>.

time bytes read by node
0.29.0 as published 13.6 s 20.8 GB
with the cache below 1.0 s 0.8 GB

The resulting draft_content.json is identical apart from ids and timestamps. 0.30.0 has the same fileSha1 / sameContent / planAssetCopy code.

Suggested fix

Memoise the comparison per process, keyed by both paths plus size and mtime, so a file that changes is still hashed again:

const sameContentCache = new Map();
function sameContent(a, b) {
    const source = statSync(a);
    const destination = statSync(b);
    if (!destination.isFile() || source.size !== destination.size)
        return false;
    const key = [a, source.size, source.mtimeMs, b, destination.size, destination.mtimeMs].join("\0");
    let same = sameContentCache.get(key);
    if (same === undefined) {
        same = fileSha1(a) === fileSha1(b);
        sameContentCache.set(key, same);
    }
    return same;
}

Possible further steps: remember the source → destination mapping of files copied by the same run (no hash at all after the copy), or cache fileSha1(src) separately so the collision branch does not hash the source again.

Environment: Windows 11, Node 24, capcut-cli 0.29.0 (also checked 0.30.0), CapCut 9.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions