Skip to content

garroshub/MixCut-Skill

mixcut

Verified

Create short mixed-cut videos from YouTube clips with automated analysis, clipping, and professional subtitle processing.

FileKindSize
SKILL.mdskill md4.3 KB
scripts/check_dependencies.pyscript966 B
scripts/process_mixcut.pyscript16.8 KB
scripts/skill_loader.pyscript1.9 KB
scripts/utils.pyscript7.9 KB

scripts/process_mixcut.py

View on GitHub
import os
import sys
import json
import subprocess
import shutil
import re
from pathlib import Path

if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8")

# Add local scripts to path to import skill_loader
sys.path.append(str(Path(__file__).resolve().parent))
import skill_loader
import check_dependencies

# Check dependencies first
if not check_dependencies.check_dependencies():
    sys.exit(1)

# Load Skills
try:
    download_video_module = skill_loader.load_skill_module(
        "Youtube-clipper-skill", "download_video", "scripts"
    )
    clip_video_module = skill_loader.load_skill_module(
        "Youtube-clipper-skill", "clip_video", "scripts"
    )
    analyze_subtitles_module = skill_loader.load_skill_module(
        "Youtube-clipper-skill", "analyze_subtitles", "scripts"
    )

    # Import utils from Youtube-clipper-skill (path added by load_skill_module)
    from utils import time_to_seconds, seconds_to_time, sanitize_filename

except Exception as e:
    print(f"Error loading skills: {e}")
    sys.exit(1)

# Dynamic resolution of directories
BASE_DIR = Path(__file__).resolve().parent.parent
REMOTION_DIR = BASE_DIR / "remotion"

# Initialize with defaults
WORK_DIR = Path.cwd() / "work"
ASSETS_DIR = WORK_DIR / "assets"
INTERMEDIATE_DIR = WORK_DIR / "intermediate"
OUTPUT_DIR = WORK_DIR / "outputs"
FINAL_OUTPUT_DIR = None


def set_work_dir(work_dir):
    global WORK_DIR, ASSETS_DIR, INTERMEDIATE_DIR, OUTPUT_DIR

    if work_dir:
        WORK_DIR = Path(work_dir)
    else:
        WORK_DIR = Path.cwd() / "work"

    ASSETS_DIR = WORK_DIR / "assets"
    INTERMEDIATE_DIR = WORK_DIR / "intermediate"
    OUTPUT_DIR = WORK_DIR / "outputs"


# Set initial work dir from environment if available
set_work_dir(os.environ.get("MIXCUT_WORK_DIR"))


def ensure_dirs():
    for d in [ASSETS_DIR, INTERMEDIATE_DIR, OUTPUT_DIR]:
        d.mkdir(parents=True, exist_ok=True)
    (ASSETS_DIR / "youtube").mkdir(exist_ok=True)


def run_cmd(cmd, cwd=None):
    print(f"Running: {' '.join(cmd)}")
    subprocess.check_call(cmd, cwd=cwd, shell=True)


def get_subtitles_for_range(subtitles, start, end):
    res = []
    for sub in subtitles:
        # Check if subtitle overlaps with [start, end]
        if sub["end"] > start and sub["start"] < end:
            # Shift and clamp
            s = max(0, sub["start"] - start)
            e = min(end - start, sub["end"] - start)
            res.append({"start": s, "end": e, "text": sub["text"]})
    return res


def download_video_robust(url, output_dir):
    """Download video and subtitles using Youtube-clipper-skill."""
    print(f"--- Robust Download: {url} ---")

    video_path = None
    subtitle_path = None

    try:
        # download_video returns a dict with paths
        result = download_video_module.download_video(url, str(output_dir))
        if result.get("subtitle_path"):
            subtitle_path = Path(result["subtitle_path"])

        if result.get("video_path"):
            video_path = Path(result["video_path"])

    except Exception as e:
        print(f"Warning: Youtube-clipper-skill failed: {e}")

    # Fallback search if paths are still None (e.g. if files existed but weren't returned)
    if not video_path:
        for ext in ["*.mp4", "*.mkv", "*.webm"]:
            found = list(output_dir.glob(ext))
            if found:
                video_path = found[0]
                break

    if not subtitle_path:
        for ext in ["*.srt", "*.vtt"]:
            found = list(output_dir.glob(ext))
            if found:
                subtitle_path = found[0]
                break

    return video_path, subtitle_path


def clip_youtube_segments(sources):
    print("--- Clipping YouTube Segments ---")
    clips = []

    # Cache for downloaded videos to avoid redundant downloads for multiple segments of same URL
    download_cache = {}

    for src in sources:
        if src["type"] != "youtube":
            continue

        url = src.get("url")
        video_path_str = src.get("path")
        video_path = Path(video_path_str) if video_path_str else None
        original_subtitle = src.get("subtitle_path")

        # Robust download if needed (path missing or explicit URL provided)
        if url:
            if url in download_cache:
                video_path, original_subtitle = download_cache[url]
            elif not video_path or not video_path.exists():
                download_dir = ASSETS_DIR / "youtube" / sanitize_filename(url)
                download_dir.mkdir(parents=True, exist_ok=True)
                v_path, s_path = download_video_robust(url, download_dir)
                if v_path:
                    video_path = v_path
                if s_path:
                    original_subtitle = s_path
                download_cache[url] = (video_path, original_subtitle)

        if not video_path or not video_path.exists():
            print(
                f"Warning: Video file for {url or video_path_str} not found. Skipping."
            )
            continue

        all_subs = []
        if original_subtitle:
            try:
                # Use analyze_subtitles_module to parse VTT
                all_subs = analyze_subtitles_module.parse_vtt(str(original_subtitle))
            except Exception as e:
                print(f"Warning: Failed to parse subtitles {original_subtitle}: {e}")

        for segment in src["clips"]:
            start = segment["start"]
            end = segment["end"]
            # Output name: video_stem_start_end.mp4
            out_name = f"{video_path.stem}_{int(start)}_{int(end)}.mp4"
            out_path = INTERMEDIATE_DIR / out_name

            if not out_path.exists():
                try:
                    clip_video_module.clip_video(
                        str(video_path), start, end, str(out_path)
                    )
                except Exception as e:
                    print(f"Error clipping video: {e}")
                    continue

            # Extract subtitles for this segment
            subs = get_subtitles_for_range(all_subs, start, end)

            clips.append(
                {
                    "type": "youtube",
                    "path": str(out_path),
                    "duration": end - start,
                    "subtitles": subs,
                    "title": segment.get("title"),
                }
            )
    return clips


def normalize_clips(clips, target_fps=30, target_height=1080):
    print("--- Normalizing Clips ---")
    normalized = []
    for i, clip in enumerate(clips):
        in_path = Path(clip["path"])
        out_path = INTERMEDIATE_DIR / f"norm_{i}_{in_path.name}"

        if not out_path.exists():
            cmd = [
                "ffmpeg",
                "-i",
                str(in_path),
                "-c:v",
                "libx264",
                "-preset",
                "fast",
                "-c:a",
                "copy",
                "-r",
                str(target_fps),
                "-vf",
                f"scale=-2:{target_height}",
                "-y",
                str(out_path),
            ]
            try:
                # Keep using subprocess for ffmpeg as it's an external tool
                subprocess.check_call(cmd, shell=True)
                clip["path"] = str(out_path)
                normalized.append(clip)
            except Exception as e:
                print(f"Failed to normalize {in_path}: {e}")
        else:
            clip["path"] = str(out_path)
            normalized.append(clip)

    return normalized


def generate_timeline(clips, settings):
    print("--- Generating Timeline ---")

    if not isinstance(settings, dict):
        settings = {}

    settings.setdefault("fps", 30)
    settings.setdefault("titleBehavior", "card")
    settings.setdefault("titleCardDurationInFrames", 60)

    # Transition logic
    transition_val = settings.get("transition", "fade")
    if isinstance(transition_val, dict):
        transition_type = transition_val.get("type", "fade")
        transition_frames = int(transition_val.get("durationInFrames", 20))
    else:
        transition_type = transition_val
        transition_frames = 20

    title_behavior = settings.get("titleBehavior", "card")
    title_card_frames = int(settings.get("titleCardDurationInFrames", 60))

    timeline = {"settings": settings, "tracks": [{"id": "main", "clips": []}]}
    current_frame = 0
    fps = int(settings.get("fps", 30))

    # Add Intro Title Sequence
    video_title = settings.get("video_title")
    if video_title:
        print(f"Adding Intro: {video_title}")
        intro_duration = 90
        timeline["tracks"][0]["clips"].append(
            {
                "type": "title",
                "text": video_title,
                "startAt": 0,
                "durationInFrames": intro_duration,
            }
        )
        current_frame = intro_duration

    for i, clip in enumerate(clips):
        # 1. Segment Title Card
        if title_behavior == "card" and title_card_frames > 0 and clip.get("title"):
            start_at = current_frame
            # Apply transition overlap with previous clip
            if transition_type != "none" and current_frame > 0:
                start_at = max(0, current_frame - transition_frames)

            timeline["tracks"][0]["clips"].append(
                {
                    "type": "title",
                    "text": clip.get("title"),
                    "startAt": start_at,
                    "durationInFrames": title_card_frames,
                }
            )
            current_frame = start_at + title_card_frames

        # 2. Video Clip
        duration_sec = clip.get("duration", 5.0)
        duration_frames = int(duration_sec * fps)
        start_at = current_frame

        # Apply transition overlap
        if transition_type != "none" and current_frame > 0:
            start_at = max(0, current_frame - transition_frames)

        # Use relative path for Remotion (assuming assets are copied to public/assets)
        # We will handle the copy in main()
        filename = Path(clip["path"]).name

        timeline["tracks"][0]["clips"].append(
            {
                "type": "video",
                "src": f"assets/{filename}",
                "startAt": start_at,
                "durationInFrames": duration_frames,
                "subtitles": clip.get("subtitles", []),
                "title": clip.get("title") if title_behavior == "overlay" else None,
            }
        )
        current_frame = start_at + duration_frames

    timeline["durationInFrames"] = current_frame

    with open(WORK_DIR / "timeline.json", "w", encoding="utf-8") as f:
        json.dump(timeline, f, indent=2, ensure_ascii=False)

    return timeline


def generate_final_srt(timeline, output_path):
    print("--- Generating Final SRT ---")
    fps = 30
    all_final_subs = []

    for clip in timeline["tracks"][0]["clips"]:
        if clip.get("type") != "video":
            continue
        start_offset = clip["startAt"] / fps
        for sub in clip.get("subtitles", []):
            clean_text = _clean_subtitle_text(sub["text"])
            if not clean_text:
                continue

            all_final_subs.append(
                {
                    "start": sub["start"] + start_offset,
                    "end": sub["end"] + start_offset,
                    "text": clean_text,
                }
            )

    if not all_final_subs:
        raise ValueError("No subtitles found to generate SRT")

    all_final_subs.sort(key=lambda x: x["start"])
    all_final_subs = _dedupe_overlapping_subtitles(all_final_subs)

    with open(output_path, "w", encoding="utf-8") as f:
        for i, sub in enumerate(all_final_subs):
            f.write(f"{i + 1}\n")
            start_str = seconds_to_time(sub["start"], use_comma=True)
            end_str = seconds_to_time(sub["end"], use_comma=True)
            f.write(f"{start_str} --> {end_str}\n")
            f.write(f"{sub['text']}\n\n")

    _deliver_final_srt(output_path)


def _clean_subtitle_text(text):
    """Collapse repeated words and remove filler words."""
    if not text:
        return ""

    # Collapse repeated words (e.g., "the the" -> "the")
    # Case insensitive, but keep original case if possible (here we just use lower for comparison)
    words = text.split()
    if not words:
        return ""

    cleaned_words = [words[0]]
    for i in range(1, len(words)):
        if words[i].lower() != words[i - 1].lower():
            cleaned_words.append(words[i])

    text = " ".join(cleaned_words)

    # Remove filler words (simple list)
    fillers = ["uh", "um", "ah", "er", "like", "you know"]
    pattern = r"\b(" + "|".join(fillers) + r")\b"
    text = re.sub(pattern, "", text, flags=re.IGNORECASE)

    # Normalize whitespace again after filler removal
    text = " ".join(text.split())
    return text


def _dedupe_overlapping_subtitles(subtitles):
    """Merge consecutive segments with identical text."""
    if not subtitles:
        return []

    deduped = []
    if not subtitles:
        return deduped

    current = subtitles[0].copy()

    for i in range(1, len(subtitles)):
        nxt = subtitles[i]
        # If text is same and they are close enough (or overlap)
        if (
            nxt["text"].strip().lower() == current["text"].strip().lower()
            and nxt["start"] <= current["end"] + 0.5
        ):
            current["end"] = max(current["end"], nxt["end"])
        else:
            deduped.append(current)
            current = nxt.copy()

    deduped.append(current)
    return deduped


def _deliver_final_srt(srt_path):
    """Copy SRT to final destination and verify."""
    if not FINAL_OUTPUT_DIR:
        print("No FINAL_OUTPUT_DIR set, skipping delivery.")
        return

    FINAL_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    dest_path = FINAL_OUTPUT_DIR / "final.srt"

    # Avoid SameFileError if src and dst are the same
    if srt_path.resolve() != dest_path.resolve():
        shutil.copy2(srt_path, dest_path)

    if not dest_path.exists() or dest_path.stat().st_size == 0:
        raise RuntimeError(f"Delivered SRT is empty or missing: {dest_path}")

    print(f"✅ SRT delivered to: {dest_path}")


def prepare_remotion_assets(clips):
    print("--- Preparing Remotion Assets ---")
    # Copy normalized clips to remotion/public/assets
    public_assets_dir = REMOTION_DIR / "public" / "assets"
    public_assets_dir.mkdir(parents=True, exist_ok=True)

    for clip in clips:
        src = Path(clip["path"])
        dst = public_assets_dir / src.name
        if not dst.exists():
            shutil.copy2(src, dst)

    print(f"Assets copied to {public_assets_dir}")


def main():
    import argparse

    parser = argparse.ArgumentParser(description="MixCut Processor")
    parser.add_argument("config_json", help="Path to configuration JSON")
    parser.add_argument(
        "--action",
        choices=["all", "generate_srt"],
        default="all",
        help="Action to perform",
    )
    args = parser.parse_args()

    config_path = Path(args.config_json)
    with open(config_path, "r", encoding="utf-8") as f:
        config = json.load(f)

    workspace_dir = config.get("workspace_dir") or config.get("work_dir")
    if workspace_dir:
        set_work_dir(workspace_dir)

    # Handle output_dir from config
    final_output_dir_config = config.get("final_output_dir") or config.get("output_dir")
    if final_output_dir_config:
        global OUTPUT_DIR, FINAL_OUTPUT_DIR
        # If output_dir is provided, it's used for workspace outputs
        if config.get("output_dir"):
            OUTPUT_DIR = Path(config.get("output_dir"))

        # FINAL_OUTPUT_DIR is the delivery destination
        FINAL_OUTPUT_DIR = Path(final_output_dir_config)

    ensure_dirs()

    if args.action == "generate_srt":
        print("--- Action: Generate SRT from Timeline ---")
        timeline_path = WORK_DIR / "timeline.json"
        if not timeline_path.exists():
            print(f"Error: Timeline file not found at {timeline_path}")
            sys.exit(1)

        with open(timeline_path, "r", encoding="utf-8") as f:
            timeline = json.load(f)

        final_srt_path = OUTPUT_DIR / "final.srt"
        generate_final_srt(timeline, final_srt_path)
        print(f"Final SRT generated at: {final_srt_path}")

        print("SRT regeneration complete.")
        return

    # Default Action: ALL
    # 1. Clip YouTube
    yt_clips = clip_youtube_segments(config.get("sources", []))

    # 2. Combine & Normalize
    all_clips = yt_clips
    norm_clips = normalize_clips(all_clips)

    # 3. Prepare Assets for Remotion
    prepare_remotion_assets(norm_clips)

    # 4. Timeline
    timeline = generate_timeline(norm_clips, config.get("global_settings", {}))

    # 5. SRT
    final_srt_path = OUTPUT_DIR / "final.srt"
    generate_final_srt(timeline, final_srt_path)
    print(f"Final SRT generated at: {final_srt_path}")

    print("Processing complete. Ready for rendering.")
    print(f"Timeline saved to: {WORK_DIR / 'timeline.json'}")


if __name__ == "__main__":
    main()
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall