Open source CapCut alternative.
TypeScript
feat: media skill subtitle pipeline and bundled libass burn-in
David Le979349c7e0f7
diff --git a/THIRD-PARTY-LICENSES.md b/THIRD-PARTY-LICENSES.mdindex 0c0af304..4a1a1400 100644--- a/THIRD-PARTY-LICENSES.md+++ b/THIRD-PARTY-LICENSES.md@@ -4,7 +4,7 @@ Bundled in `donkey-tools/` (built by `scripts/fetch-bundled-tools.sh`).| Tool | License | Notes ||---|---|---|-| ffmpeg | LGPL-2.1 | built `--disable-gpl`, no x264/x265; H.264/HEVC encode via VideoToolbox |+| ffmpeg | LGPL-2.1 | built `--disable-gpl`, no x264/x265; H.264/HEVC encode via VideoToolbox; subtitle burn-in links libass (ISC), freetype (FTL), fribidi (LGPL-2.1+) || liteparse (`lit`) | Apache-2.0 | PDF extraction; bundles Tesseract (Apache-2.0) for OCR || yt-dlp | Unlicense | || qpdf | Apache-2.0 | |diff --git a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/media/SKILL.md b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/media/SKILL.mdindex 344d68e1..633038ba 100644--- a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/media/SKILL.md+++ b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/media/SKILL.md@@ -1,10 +1,10 @@# Mediaid: media-description: Expert audio and video — download from a URL with yt-dlp, then transcode, trim, extract audio, or grab frames with ffmpeg.-tags: video, audio, media, download, convert, youtube-keywords: video, audio, download, youtube, vimeo, mp4, mp3, convert, trim, clip, extract audio, transcode, frame-tools: shell_exec+description: Expert audio and video — download from a URL with yt-dlp, transcode/trim/extract/grab frames with ffmpeg, and transcribe, subtitle, or translate with the model.+tags: video, audio, media, download, convert, youtube, subtitles, transcribe+keywords: video, audio, download, youtube, vimeo, mp4, mp3, convert, trim, clip, extract audio, transcode, frame, subtitle, subtitles, caption, captions, transcribe, transcript, translate, srt, vtt+tools: shell_exec, llm.generate`yt-dlp` and `ffmpeg`/`ffprobe` are bundled and on the PATH, so use them by barename. Downloading and writing files is reversible — propose the command and let@@ -24,6 +24,24 @@ the consent gate handle approval; do not fall back to a browser.- One frame / thumbnail: `ffmpeg -ss 5 -i in.mp4 -frames:v 1 thumb.png`. Frames every second: `ffmpeg -i in.mp4 -vf fps=1 frame_%03d.png`.- ffmpeg refuses to overwrite by default and prompts; pass `-y` to overwrite or choose a new output name.+## Subtitles & transcription+Get the subtitle text, then apply it. Prefer the cheapest source that gives correct timing.++1. **Reuse the video's own captions first (free — no model call).** Many videos already ship captions; pull them as a sidecar `.srt`:+ `yt-dlp --write-subs --write-auto-subs --sub-langs 'en.*' --convert-subs srt --skip-download -P ~/Downloads 'URL'` (drop `--skip-download` to grab the video too).+2. **Otherwise transcribe with the model.** Timing matters: **clip first, then transcribe the clip** so timestamps start at zero and line up. Extract compact mono audio, then call `llm.generate` with that file:+ - `ffmpeg -i clip.mp4 -vn -ac 1 -c:a libmp3lame -b:a 64k clip.mp3`+ - `llm.generate filePath=clip.mp3 prompt="Transcribe this audio to SRT with timestamps, one cue per sentence" toFile=true` → the returned file holds your SRT text; save/rename it to `subs.srt`.+3. **Translate** by asking in the same call: `prompt="Transcribe this audio and translate the text to Spanish; output SRT, keep the timestamps"`.+4. **Long media — chunk it.** Inline audio is size-limited (`llm.generate` rejects a file that is too large or a transcript it had to truncate — both mean "use smaller chunks"). Split, transcribe each zero-based, then shift by each segment's *real* start:+ - `ffmpeg -i audio.mp3 -f segment -segment_time 600 -reset_timestamps 1 -c copy seg_%03d.mp3` splits into ~10-min pieces.+ - `-c copy` cuts on frame boundaries, so a segment's real length is not exactly 600s. Read each one's true duration with `ffprobe -v error -show_entries format=duration -of csv=p=0 seg_000.mp3` and accumulate; that running sum is the next segment's start offset — do not assume `index × 600`.+ - Transcribe each segment with a plain zero-based prompt (`"Transcribe to SRT with timestamps"`), then add the accumulated offset to every cue in that segment's SRT and concatenate in order, renumbering cue indices. Sanity-check that timestamps only increase.+5. **Apply to the video (ffmpeg):**+ - Burn-in (default for a shareable clip — always-visible, what social players expect): `ffmpeg -i clip.mp4 -vf "subtitles=subs.srt" -c:a copy out.mp4`. The bundled ffmpeg ships with libass, so this works out of the box. If it ever fails with `No such filter: 'subtitles'` (a stripped ffmpeg without libass), fall back to the soft track below and tell the user why.+ - Soft, toggleable track (offer for an archive copy): `ffmpeg -i clip.mp4 -i subs.srt -c copy -c:s mov_text out.mp4`.+- Model timestamps are approximate (good enough for subtitles); always extract audio before transcribing, and chunk long files.+## Verify- Confirm the output exists and is real media: `ffprobe -v error -show_entries format=duration:format_name -of default=nw=1 out.mp4`.- A zero-byte file or an ffprobe error means the operation failed — report it, don't claim success.\ No newline at end of filediff --git a/scripts/fetch-bundled-tools.sh b/scripts/fetch-bundled-tools.shindex 1ede6977..9d899b11 100755--- a/scripts/fetch-bundled-tools.sh+++ b/scripts/fetch-bundled-tools.sh@@ -47,17 +47,58 @@ bundle() {local search=(--search-path "$BREW_LIB")local dirfor dir in "${EXTRA_SEARCH[@]:-}"; do [ -n "$dir" ] && search+=(--search-path "$dir"); done- dylibbundler --overwrite-files --bundle-deps --fix-file "$VENDOR_DIR/$name" \+ if dylibbundler --overwrite-files --bundle-deps --fix-file "$VENDOR_DIR/$name" \--dest-dir "$VENDOR_DIR" --install-path "@loader_path/" "${search[@]}" \- >/dev/null 2>&1- ok "$name"+ >/dev/null 2>&1; then+ ok "$name"+ else+ fail "$name" "dylibbundler failed; binary would have unresolved libraries"+ return 1+ fi+}++# Install prefix of the from-source libass, set by build_libass for build_lgpl_ffmpeg.+ASS_PREFIX=""++# Build libass from source so ffmpeg's `subtitles` filter (subtitle burn-in) works in the bundled+# binary. We build it with the CoreText font provider (--disable-fontconfig): CoreText uses macOS's+# own installed fonts, so there are NO fontconfig config files or font cache to relocate into the+# bundle — the relocation trap that keeps fontconfig-based tools from shipping. harfbuzz is left off+# (--disable-harfbuzz) to avoid pulling in the glib dependency tree; Latin/CJK subtitles render fine,+# only complex-script (Arabic/Indic) shaping is given up. libass (ISC), freetype (FTL), and fribidi+# (LGPL) are all LGPL-compatible, so enabling it keeps ffmpeg --disable-gpl.+build_libass() {+ local ver="0.17.3"+ local work="/tmp/libass-build"+ local brew_prefix; brew_prefix="$(brew --prefix)"+ local dep+ for dep in freetype fribidi; do+ brew list "$dep" >/dev/null 2>&1 || brew install "$dep" >/dev/null 2>&1+ done+ rm -rf "$work"; mkdir -p "$work"+ curl -fsSL -o "$work/libass.tar.xz" \+ "https://github.com/libass/libass/releases/download/${ver}/libass-${ver}.tar.xz" \+ || { echo "FATAL: libass source download failed" >&2; exit 1; }+ tar -xJf "$work/libass.tar.xz" -C "$work" --strip-components=1+ ( cd "$work" \+ && PKG_CONFIG_PATH="$brew_prefix/lib/pkgconfig:$brew_prefix/share/pkgconfig" \+ ./configure --prefix="$work/out" --disable-static --enable-shared \+ --disable-fontconfig --disable-harfbuzz \+ && make -j"$(sysctl -n hw.ncpu)" \+ && make install ) >"$work/build.log" 2>&1+ if [ ! -f "$work/out/lib/libass.dylib" ]; then+ echo "FATAL: libass build failed; tail of $work/build.log:" >&2+ tail -25 "$work/build.log" >&2+ exit 1+ fi+ ASS_PREFIX="$work/out"}# Build a TRUE LGPL ffmpeg from source — no --enable-gpl, no x264/x265. H.264/HEVC# encode comes from Apple VideoToolbox; decode is built in; mp3 via LGPL libmp3lame,-# opus via BSD libopus, AV1 decode via BSD dav1d. Built shared, then dylibbundled.-# ffmpeg is MANDATORY: any failure here aborts the whole vendoring run, because a-# bundle without ffmpeg is not shippable.+# opus via BSD libopus, AV1 decode via BSD dav1d, subtitle burn-in via libass (built+# above). Built shared, then dylibbundled. ffmpeg is MANDATORY: any failure here aborts+# the whole vendoring run, because a bundle without ffmpeg is not shippable.build_lgpl_ffmpeg() {local ver="7.1.1"local work="/tmp/ffmpeg-lgpl-build"@@ -68,17 +109,19 @@ build_lgpl_ffmpeg() {for dep in lame opus dav1d; dobrew list "$dep" >/dev/null 2>&1 || brew install "$dep" >/dev/null 2>&1done+ build_libassrm -rf "$work"; mkdir -p "$work"curl -fsSL -o "$work/ffmpeg.tar.xz" "https://ffmpeg.org/releases/ffmpeg-${ver}.tar.xz" \|| { echo "FATAL: ffmpeg source download failed" >&2; exit 1; }tar -xJf "$work/ffmpeg.tar.xz" -C "$work" --strip-components=1( cd "$work" \- && PKG_CONFIG_PATH="$brew_prefix/lib/pkgconfig:$brew_prefix/share/pkgconfig" \+ && PKG_CONFIG_PATH="$ASS_PREFIX/lib/pkgconfig:$brew_prefix/lib/pkgconfig:$brew_prefix/share/pkgconfig" \./configure --prefix="$work/out" \--disable-gpl --disable-nonfree --disable-doc --disable-static --enable-shared \--enable-videotoolbox --enable-audiotoolbox \- --enable-libmp3lame --enable-libopus --enable-libdav1d \- --extra-cflags="-I$brew_prefix/include" --extra-ldflags="-L$brew_prefix/lib" \+ --enable-libmp3lame --enable-libopus --enable-libdav1d --enable-libass \+ --extra-cflags="-I$brew_prefix/include -I$ASS_PREFIX/include" \+ --extra-ldflags="-L$brew_prefix/lib -L$ASS_PREFIX/lib" \&& make -j"$(sysctl -n hw.ncpu)" \&& make install ) >"$work/build.log" 2>&1if [ ! -x "$work/out/bin/ffmpeg" ] || [ ! -x "$work/out/bin/ffprobe" ]; then@@ -86,7 +129,9 @@ build_lgpl_ffmpeg() {tail -25 "$work/build.log" >&2exit 1fi- EXTRA_SEARCH=("$work/out/lib")+ # Search both ffmpeg's own out/lib and libass's out/lib so dylibbundler vendors libass (and, via+ # the Homebrew lib default search, its freetype/fribidi/libpng deps) flat with @loader_path.+ EXTRA_SEARCH=("$work/out/lib" "$ASS_PREFIX/lib")bundle "$work/out/bin/ffmpeg" ffmpegbundle "$work/out/bin/ffprobe" ffprobeEXTRA_SEARCH=()