← 返回 Skills 市场
wu-uk

video-frame-extraction

作者 wu-uk · GitHub ↗ · v0.1.0 · MIT-0
cross-platform ✓ 安全检测通过
74
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install jpg-ocr-stat-video-frame-extraction
功能描述
Extract frames from video files and save them as images using OpenCV
使用说明 (SKILL.md)

Video Frame Extraction Skill

Purpose

This skill enables extraction of individual frames from video files (MP4, AVI, MOV, etc.) using OpenCV. Extracted frames are saved as image files in a specified output directory. It is suitable for video analysis, creating training datasets, thumbnail generation, and preprocessing video content for further processing.

When to Use

  • Extracting frames for machine learning training data
  • Creating image sequences from video content
  • Generating video thumbnails or preview images
  • Preprocessing videos for object detection or tracking
  • Converting video segments to image collections for analysis
  • Sampling frames at specific intervals for time-lapse effects

Required Libraries

The following Python libraries are required:

import cv2
import os
import json
from pathlib import Path

Input Requirements

  • File formats: MP4, AVI, MOV, MKV, WMV, FLV, WEBM
  • Video codec: Must be readable by OpenCV (most common codecs supported)
  • File access: Read permissions on source video
  • Output directory: Write permissions on destination folder
  • Disk space: Ensure sufficient space for extracted frames (uncompressed images)

Output Schema

All extraction results must be returned as valid JSON conforming to this schema:

{
  "success": true,
  "source_video": "sample.mp4",
  "output_directory": "/path/to/frames",
  "frames_extracted": 150,
  "extraction_params": {
    "interval": 1,
    "start_frame": 0,
    "end_frame": null,
    "output_format": "jpg"
  },
  "video_metadata": {
    "total_frames": 300,
    "fps": 30.0,
    "duration_seconds": 10.0,
    "resolution": [1920, 1080]
  },
  "output_files": [
    "frame_000001.jpg",
    "frame_000002.jpg"
  ],
  "warnings": []
}

Field Descriptions

  • success: Boolean indicating whether frame extraction completed
  • source_video: Original video filename
  • output_directory: Path where frames were saved
  • frames_extracted: Total number of frames successfully saved
  • extraction_params.interval: Frame sampling interval (1 = every frame, 2 = every other frame, etc.)
  • extraction_params.start_frame: First frame index extracted
  • extraction_params.end_frame: Last frame index extracted (null if extracted to end)
  • extraction_params.output_format: Image format used for saving frames
  • video_metadata.total_frames: Total frame count in source video
  • video_metadata.fps: Frames per second of source video
  • video_metadata.duration_seconds: Video duration in seconds
  • video_metadata.resolution: Video dimensions as [width, height]
  • output_files: List of generated frame filenames
  • warnings: Array of issues encountered during extraction

Code Examples

Basic Frame Extraction

import cv2
import os

def extract_all_frames(video_path, output_dir):
    """Extract all frames from a video file."""
    os.makedirs(output_dir, exist_ok=True)
    
    cap = cv2.VideoCapture(video_path)
    frame_count = 0
    
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        filename = os.path.join(output_dir, f"frame_{frame_count:06d}.jpg")
        cv2.imwrite(filename, frame)
        frame_count += 1
    
    cap.release()
    return frame_count

Interval-Based Frame Extraction

import cv2
import os

def extract_frames_at_interval(video_path, output_dir, interval=1):
    """Extract frames at specified intervals."""
    os.makedirs(output_dir, exist_ok=True)
    
    cap = cv2.VideoCapture(video_path)
    frame_index = 0
    saved_count = 0
    
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        if frame_index % interval == 0:
            filename = os.path.join(output_dir, f"frame_{saved_count:06d}.jpg")
            cv2.imwrite(filename, frame)
            saved_count += 1
        
        frame_index += 1
    
    cap.release()
    return saved_count

Full Extraction with JSON Output

import cv2
import os
import json
from pathlib import Path

def extract_frames_to_json(video_path, output_dir, interval=1, 
                           start_frame=0, end_frame=None, output_format="jpg"):
    """Extract frames and return results as JSON."""
    video_name = os.path.basename(video_path)
    warnings = []
    output_files = []
    
    try:
        os.makedirs(output_dir, exist_ok=True)
        cap = cv2.VideoCapture(video_path)
        
        if not cap.isOpened():
            raise ValueError(f"Cannot open video: {video_path}")
        
        # Get video metadata
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        fps = cap.get(cv2.CAP_PROP_FPS)
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = total_frames / fps if fps > 0 else 0
        
        # Set end frame if not specified
        if end_frame is None:
            end_frame = total_frames
        
        # Seek to start frame
        if start_frame > 0:
            cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
        
        frame_index = start_frame
        saved_count = 0
        
        while frame_index \x3C end_frame:
            ret, frame = cap.read()
            if not ret:
                if frame_index \x3C end_frame:
                    warnings.append(f"Video ended early at frame {frame_index}")
                break
            
            if (frame_index - start_frame) % interval == 0:
                filename = f"frame_{saved_count:06d}.{output_format}"
                filepath = os.path.join(output_dir, filename)
                cv2.imwrite(filepath, frame)
                output_files.append(filename)
                saved_count += 1
            
            frame_index += 1
        
        cap.release()
        
        result = {
            "success": True,
            "source_video": video_name,
            "output_directory": str(output_dir),
            "frames_extracted": saved_count,
            "extraction_params": {
                "interval": interval,
                "start_frame": start_frame,
                "end_frame": end_frame,
                "output_format": output_format
            },
            "video_metadata": {
                "total_frames": total_frames,
                "fps": fps,
                "duration_seconds": round(duration, 2),
                "resolution": [width, height]
            },
            "output_files": output_files,
            "warnings": warnings
        }
        
    except Exception as e:
        result = {
            "success": False,
            "source_video": video_name,
            "output_directory": str(output_dir),
            "frames_extracted": 0,
            "extraction_params": {
                "interval": interval,
                "start_frame": start_frame,
                "end_frame": end_frame,
                "output_format": output_format
            },
            "video_metadata": {
                "total_frames": 0,
                "fps": 0,
                "duration_seconds": 0,
                "resolution": [0, 0]
            },
            "output_files": [],
            "warnings": [f"Extraction failed: {str(e)}"]
        }
    
    return result

# Usage
result = extract_frames_to_json("video.mp4", "./frames", interval=10)
print(json.dumps(result, indent=2))

Time-Based Frame Extraction

import cv2
import os

def extract_frames_by_seconds(video_path, output_dir, seconds_interval=1.0):
    """Extract frames at specific time intervals (in seconds)."""
    os.makedirs(output_dir, exist_ok=True)
    
    cap = cv2.VideoCapture(video_path)
    fps = cap.get(cv2.CAP_PROP_FPS)
    frame_interval = int(fps * seconds_interval)
    
    if frame_interval \x3C 1:
        frame_interval = 1
    
    frame_index = 0
    saved_count = 0
    
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        if frame_index % frame_interval == 0:
            filename = os.path.join(output_dir, f"frame_{saved_count:06d}.jpg")
            cv2.imwrite(filename, frame)
            saved_count += 1
        
        frame_index += 1
    
    cap.release()
    return saved_count

Batch Processing Multiple Videos

import cv2
import os
import json
from pathlib import Path

def process_video_directory(video_dir, output_base_dir, interval=1):
    """Process all videos in a directory and extract frames."""
    video_extensions = {'.mp4', '.avi', '.mov', '.mkv', '.wmv', '.flv', '.webm'}
    results = []
    
    for video_file in sorted(Path(video_dir).iterdir()):
        if video_file.suffix.lower() in video_extensions:
            video_output_dir = os.path.join(
                output_base_dir, 
                video_file.stem
            )
            result = extract_frames_to_json(
                str(video_file), 
                video_output_dir, 
                interval=interval
            )
            results.append(result)
            print(f"Processed: {video_file.name} -> {result['frames_extracted']} frames")
    
    return results

Extraction Configuration Options

Output Image Formats

# JPEG format (default, good balance of quality and size)
cv2.imwrite("frame.jpg", frame)

# PNG format (lossless, larger files)
cv2.imwrite("frame.png", frame)

# JPEG with custom quality (0-100)
cv2.imwrite("frame.jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, 95])

# PNG with compression level (0-9)
cv2.imwrite("frame.png", frame, [cv2.IMWRITE_PNG_COMPRESSION, 3])

Frame Seeking Methods

# Seek by frame number
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)

# Seek by milliseconds
cap.set(cv2.CAP_PROP_POS_MSEC, milliseconds)

# Seek by ratio (0.0 to 1.0)
cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 0.5)  # Middle of video

Frame Resizing

def extract_resized_frames(video_path, output_dir, target_size=(640, 480)):
    """Extract and resize frames to specified dimensions."""
    os.makedirs(output_dir, exist_ok=True)
    
    cap = cv2.VideoCapture(video_path)
    frame_count = 0
    
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        resized = cv2.resize(frame, target_size)
        filename = os.path.join(output_dir, f"frame_{frame_count:06d}.jpg")
        cv2.imwrite(filename, resized)
        frame_count += 1
    
    cap.release()
    return frame_count

Video Metadata Retrieval

Extract video properties before processing:

def get_video_info(video_path):
    """Retrieve video metadata."""
    cap = cv2.VideoCapture(video_path)
    
    if not cap.isOpened():
        return None
    
    info = {
        "total_frames": int(cap.get(cv2.CAP_PROP_FRAME_COUNT)),
        "fps": cap.get(cv2.CAP_PROP_FPS),
        "width": int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        "height": int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
        "codec": int(cap.get(cv2.CAP_PROP_FOURCC)),
        "duration_seconds": cap.get(cv2.CAP_PROP_FRAME_COUNT) / cap.get(cv2.CAP_PROP_FPS)
    }
    
    cap.release()
    return info

Specific Frame Extraction

For extracting frames at exact positions:

def extract_specific_frames(video_path, output_dir, frame_numbers):
    """Extract specific frames by their indices."""
    os.makedirs(output_dir, exist_ok=True)
    
    cap = cv2.VideoCapture(video_path)
    extracted = []
    
    for frame_num in sorted(frame_numbers):
        cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
        ret, frame = cap.read()
        
        if ret:
            filename = os.path.join(output_dir, f"frame_{frame_num:06d}.jpg")
            cv2.imwrite(filename, frame)
            extracted.append(frame_num)
    
    cap.release()
    return extracted

Error Handling

Common Issues and Solutions

Issue: Video file cannot be opened

cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
    print(f"Error: Cannot open video file: {video_path}")
    print("Check file path, permissions, and codec support")

Issue: Frames read as None

ret, frame = cap.read()
if not ret or frame is None:
    print("Failed to read frame - video may be corrupted or ended")

Issue: Codec not supported

# Check if video has valid properties
fps = cap.get(cv2.CAP_PROP_FPS)
if fps == 0:
    print("Warning: Could not detect FPS - codec may be unsupported")

Issue: Disk space exhausted

import shutil

def check_disk_space(output_dir, required_mb=100):
    """Check available disk space before extraction."""
    stat = shutil.disk_usage(output_dir)
    available_mb = stat.free / (1024 * 1024)
    return available_mb >= required_mb

Quality Self-Check

Before returning results, verify:

  • Output is valid JSON (use json.loads() to validate)
  • All required fields are present (success, source_video, frames_extracted, video_metadata)
  • Output directory was created successfully
  • Extracted frame count matches expected value based on interval
  • Warnings array includes all detected issues
  • Video was properly released with cap.release()
  • Frame filenames follow consistent zero-padded numbering

Limitations

  • OpenCV may not support all video codecs; install additional codecs if needed
  • Seeking in variable frame rate videos may be inaccurate
  • Large videos with high frame counts require significant disk space
  • Memory usage increases with video resolution
  • Some container formats (MKV with certain codecs) may have seeking issues
  • Encrypted or DRM-protected videos cannot be processed
  • Damaged or partially corrupted videos may extract partial results

Version History

  • 1.0.0 (2026-01-21): Initial release with OpenCV video frame extraction
安全使用建议
This skill is coherent for extracting frames, but consider these operational cautions before use: (1) Ensure the runtime environment has Python and a trusted build of OpenCV (cv2) installed — SKILL.md does not install dependencies. (2) Provide explicit, sandboxed output directories to avoid accidental overwrites and ensure sufficient disk space (frame extraction can produce many large files). (3) Treat untrusted video files cautiously: media libraries and codecs have historically had security vulnerabilities, so run extraction in a confined environment if inputs are not trusted. (4) The SKILL.md contains only examples (some content is truncated); if you plan to run code derived from it, review the full implementation for error handling, path validation, and resource limits.
功能分析
Type: OpenClaw Skill Name: jpg-ocr-stat-video-frame-extraction Version: 0.1.0 The skill bundle provides standard utility functions for extracting frames from video files using the OpenCV library. The code examples in SKILL.md follow best practices, including proper resource management (cap.release), error handling, and disk space checks, with no evidence of malicious intent, data exfiltration, or prompt injection.
能力评估
Purpose & Capability
Name and description (video frame extraction) align with the instructions and examples: they use OpenCV (cv2) to open video files and write image files. No unrelated credentials, binaries, or config paths are requested.
Instruction Scope
SKILL.md stays on-topic: examples show reading a provided video file, extracting frames, and writing images to a specified output directory. It does not instruct reading unrelated system files, contacting external endpoints, or exfiltrating data. It does require read/write access to user-specified paths (expected for this task).
Install Mechanism
Instruction-only skill with no install spec. This is low-risk from an installation perspective, but it implicitly requires an environment with Python and OpenCV installed; the SKILL.md does not attempt to download or execute remote code.
Credentials
The skill declares no environment variables, secrets, or extra credentials. File system read/write access to the specified video and output directory is required and proportional to the stated purpose.
Persistence & Privilege
Skill is not always-on and is user-invocable; it does not request persistent agent-level privileges or modify other skills. Autonomous invocation is allowed but is the platform default and not combined with other concerning capabilities.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install jpg-ocr-stat-video-frame-extraction
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /jpg-ocr-stat-video-frame-extraction 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v0.1.0
Bulk publish from all-task-skills-dedup
元数据
Slug jpg-ocr-stat-video-frame-extraction
版本 0.1.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

video-frame-extraction 是什么?

Extract frames from video files and save them as images using OpenCV. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 74 次。

如何安装 video-frame-extraction?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install jpg-ocr-stat-video-frame-extraction」即可一键安装,无需额外配置。

video-frame-extraction 是免费的吗?

是的,video-frame-extraction 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

video-frame-extraction 支持哪些平台?

video-frame-extraction 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 video-frame-extraction?

由 wu-uk(@wu-uk)开发并维护,当前版本 v0.1.0。

💬 留言讨论