home / skills / benchflow-ai / skillsbench / ffmpeg-video-editing

This skill helps you edit videos with ffmpeg, cutting, trimming, and recombining clips efficiently for various formats.

npx playbooks add skill benchflow-ai/skillsbench --skill ffmpeg-video-editing

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.7 KB
---
name: ffmpeg-video-editing
description: "Video editing with ffmpeg including cutting, trimming, concatenating segments, and re-encoding. Use when working with video files (.mp4, .mkv, .avi) for: removing segments, joining clips, extracting portions, or any video manipulation task."
---

# FFmpeg Video Editing

## Cutting Video Segments

### Extract a portion (keep segment)

```bash
# Extract from start_time to end_time
ffmpeg -i input.mp4 -ss START -to END -c copy output.mp4

# With re-encoding for frame-accurate cuts
ffmpeg -i input.mp4 -ss START -to END -c:v libx264 -c:a aac output.mp4
```

### Remove a segment (cut out middle)

To remove a segment, split into parts and concatenate:

```bash
# 1. Extract before the cut
ffmpeg -i input.mp4 -to CUT_START -c copy part1.mp4

# 2. Extract after the cut  
ffmpeg -i input.mp4 -ss CUT_END -c copy part2.mp4

# 3. Concatenate
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
```

## Concatenating Multiple Segments

### Using concat demuxer (recommended for same-codec files)

Create a file list (`segments.txt`):
```
file 'segment1.mp4'
file 'segment2.mp4'
file 'segment3.mp4'
```

Then concatenate:
```bash
ffmpeg -f concat -safe 0 -i segments.txt -c copy output.mp4
```

### Using filter_complex (for re-encoding)

```bash
ffmpeg -i seg1.mp4 -i seg2.mp4 -i seg3.mp4 \
  -filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[v][a]" \
  -map "[v]" -map "[a]" output.mp4
```

## Removing Multiple Segments (Batch)

For removing many short segments (like filler words), the efficient approach:

1. Calculate the "keep" segments (inverse of remove segments)
2. Extract each keep segment
3. Concatenate all keep segments

```python
import subprocess
import os

def remove_segments(input_file, segments_to_remove, output_file):
    """
    segments_to_remove: list of (start, end) tuples in seconds
    """
    # Get video duration
    result = subprocess.run([
        'ffprobe', '-v', 'error', '-show_entries', 'format=duration',
        '-of', 'default=noprint_wrappers=1:nokey=1', input_file
    ], capture_output=True, text=True)
    duration = float(result.stdout.strip())

    # Sort segments and merge overlapping
    segments = sorted(segments_to_remove)

    # Calculate keep segments (gaps between remove segments)
    keep_segments = []
    current_pos = 0.0

    for start, end in segments:
        if start > current_pos:
            keep_segments.append((current_pos, start))
        current_pos = max(current_pos, end)

    if current_pos < duration:
        keep_segments.append((current_pos, duration))

    # Extract each keep segment
    temp_files = []
    for i, (start, end) in enumerate(keep_segments):
        temp_file = f'/tmp/seg_{i:04d}.mp4'
        subprocess.run([
            'ffmpeg', '-y', '-i', input_file,
            '-ss', str(start), '-to', str(end),
            '-c', 'copy', temp_file
        ], check=True)
        temp_files.append(temp_file)

    # Create concat list
    list_file = '/tmp/concat_list.txt'
    with open(list_file, 'w') as f:
        for temp_file in temp_files:
            f.write(f"file '{temp_file}'\n")

    # Concatenate
    subprocess.run([
        'ffmpeg', '-y', '-f', 'concat', '-safe', '0',
        '-i', list_file, '-c', 'copy', output_file
    ], check=True)

    # Cleanup
    for f in temp_files:
        os.remove(f)
    os.remove(list_file)
```

## Common Issues

### Audio/Video sync problems
- Use `-c copy` only when cutting at keyframes
- For precise cuts, re-encode: `-c:v libx264 -c:a aac`

### Gaps or glitches at cut points
- Ensure segments don't overlap
- Small buffer (0.01s) between segments can help

### Getting video duration

```bash
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4
```

Overview

This skill provides practical ffmpeg-based video editing capabilities for cutting, trimming, concatenating, and re-encoding common video formats (.mp4, .mkv, .avi). It helps remove unwanted segments, join clips, extract portions, and automate batch edits while preserving sync and quality. The instructions and snippets prioritize reliable, repeatable workflows for both quick edits and scripted batch processing.

How this skill works

The skill shows how to extract or remove time ranges using ffmpeg commands, either with stream copy for fast keyframe-aligned cuts or with re-encoding for frame-accurate results. It explains concatenation using the concat demuxer for same-codec files and filter_complex for re-encoding. A Python example demonstrates computing keep segments from remove lists, extracting temporary segments, and concatenating them into a final output.

When to use it

  • Trim start or end portions from a recording quickly
  • Remove one or many unwanted segments (e.g., filler words, mistakes)
  • Join multiple clips recorded with the same codec into one file
  • Produce frame-accurate cuts that require re-encoding
  • Automate batch edits in scripts or pipelines

Best practices

  • Prefer -c copy and the concat demuxer when all segments share codecs and keyframe alignment to avoid re-encoding overhead
  • Use re-encoding (e.g., -c:v libx264 -c:a aac) for frame-accurate cuts or when codecs differ
  • Calculate keep segments (inverse of remove ranges) and extract them before concatenation for many small removals
  • Use ffprobe to get precise duration and validate timestamps before editing
  • Add a tiny buffer (e.g., 0.01s) if you see glitches at cut points and ensure segments do not overlap

Example use cases

  • Extract a 30s highlight from a longer .mp4 for social sharing
  • Remove a middle section from a webinar recording and stitch the remaining parts together
  • Batch-remove multiple filler-word ranges from an interview using a Python script that runs ffmpeg
  • Concatenate several same-codec clips from a multi-camera shoot with the concat demuxer
  • Re-encode and trim footage to produce a standardized delivery format (H.264/AAC .mp4)

FAQ

When should I use -c copy vs re-encoding?

Use -c copy for fast cuts if you are cutting at keyframes and all files use compatible codecs. Re-encode when you need frame-accurate cuts or when codecs/parameters differ between segments.

Why do I see A/V sync issues after cutting?

Sync problems usually occur if cuts are not on keyframes or if stream copy was used improperly. Re-encode the affected segments or adjust cut points to keyframes to fix sync.

How do I concatenate files with different codecs?

Either re-encode all segments to a common codec and parameters, then use the concat demuxer, or use filter_complex concat which re-encodes during the process.