trim

Extract specific video segments with frame-perfect precision or fast stream copying.

Overview

The trim command extracts portions of videos by specifying start/end times or duration. It offers two modes: fast mode (stream copy, instant) for quick cuts, and accurate mode (re-encoding) for frame-perfect precision. Perfect for creating highlights, removing unwanted sections, or extracting specific scenes.

Key Features:

  • ⏱️ Flexible time formats (HH:MM:SS or seconds)
  • ⚡ Fast mode: Stream copy (no quality loss, instant)
  • 🎯 Accurate mode: Frame-perfect cuts
  • 🎬 Add fade-in/fade-out effects
  • 🎵 Volume and speed adjustments
  • 📁 Batch trim multiple segments

Common Use Cases:

  • Creating video highlights or teasers
  • Removing intro/outro from videos
  • Extracting specific scenes or chapters
  • Splitting long videos into segments
  • Creating clips for social media
  • Removing mistakes or unwanted content
Info

Fast vs Accurate: Fast mode uses stream copy (instant, no re-encoding) but may be off by 1-2 seconds due to keyframe positions. Accurate mode re-encodes for frame-perfect cuts but takes longer.

Usage

Terminal
$ mediaproc video trim <input> [options]

Options

OptionAliasTypeDefaultDescription
--output-ostringAutoOutput file or directory path
--start-sstring0Start time (HH:MM:SS or seconds)
--end-estringEnd time (HH:MM:SS or seconds)
--duration-dstringDuration from start (HH:MM:SS or seconds)
--fastbooleanfalseFast mode (stream copy, may be slightly inaccurate)
--accuratebooleanfalseAccurate mode (re-encode for frame accuracy)
--codec-cstringcopyVideo codec: h264, h265, copy
--formatstringOutput format: mp4, mkv, webm
--fade-innumberAdd fade-in effect (seconds)
--fade-outnumberAdd fade-out effect (seconds)
--speednumberAdjust playback speed (0.5-2.0)
--volumenumber1.0Adjust audio volume (0.0-2.0)
--qualitynumber23CRF quality if re-encoding (default: 23)
--no-audiobooleanfalseRemove audio track from output
--dry-runbooleanfalsePreview without executing
--verbose-vbooleanfalseShow detailed FFmpeg output

Flag Details

--start / --end - Time Boundaries

Specify where to start and end the trim operation.

Time Formats:

HH:MM:SS Format:

  • 00:01:30 = 1 minute 30 seconds
  • 01:30:45 = 1 hour 30 minutes 45 seconds
  • 05:30 = 5 minutes 30 seconds (MM:SS)
  • 00:00:15 = 15 seconds

Seconds Format:

  • 90 = 1 minute 30 seconds
  • 3665 = 1 hour 1 minute 5 seconds
  • 30.5 = 30.5 seconds (decimals supported)

Examples:

# Extract from 1:30 to 3:45
mediaproc video trim video.mp4 --start 00:01:30 --end 00:03:45

# Same using seconds
mediaproc video trim video.mp4 --start 90 --end 225

# From 30s to end of video
mediaproc video trim video.mp4 --start 30

--duration - Length from Start

Specify how long the output should be (alternative to --end).

Format: HH:MM:SS or seconds

Relationship:

  • --end = --start + --duration
  • Only use --end OR --duration, not both

Examples:

# First 10 seconds
mediaproc video trim video.mp4 --duration 10

# From 30s, extract 1 minute
mediaproc video trim video.mp4 --start 30 --duration 60

# 2 minutes from 1:30 mark
mediaproc video trim video.mp4 --start 00:01:30 --duration 00:02:00

--fast - Fast Mode (Stream Copy)

Enable ultra-fast trimming by copying video/audio streams without re-encoding.

Advantages:

  • ⚡ Instant operation (real-time or faster)
  • 🎯 Zero quality loss (bit-perfect copy)
  • 💾 Minimal CPU/memory usage

Limitations:

  • ⚠️ May be off by 1-3 seconds (keyframe dependent)
  • ⚠️ Cannot add effects (fade, speed, filters)
  • ⚠️ Start point snaps to nearest keyframe

How It Works:

  • Videos are encoded with keyframes every 2-10 seconds
  • Stream copy can only cut at keyframe positions
  • Start time snaps to nearest keyframe before requested time

When to Use:

  • Quick rough cuts
  • Time precision not critical
  • Preserving exact original quality
  • Large files (4K, high bitrate)

Example:

# Fast trim from 5:00 to 10:00 (may start at 4:58)
mediaproc video trim video.mp4 -s 05:00 -e 10:00 --fast

Learn More:


--accurate - Accurate Mode (Re-encode)

Re-encode video for frame-perfect cuts (default behavior).

Advantages:

  • 🎯 Frame-accurate cuts (perfect timing)
  • ✨ Can apply effects (fade, speed, filters)
  • 📐 Can change resolution/codec simultaneously

Limitations:

  • ⏱️ Slower (depends on video length and settings)
  • 📉 Slight quality loss (minimized with high CRF)

Encoding Time:

  • 1080p 30fps: ~0.2-0.5x realtime (2 min → 4-10 min)
  • 4K 60fps: ~0.05-0.2x realtime (2 min → 10-40 min)
  • Hardware acceleration helps significantly

When to Use:

  • Professional editing (precision required)
  • Adding effects (fade, speed changes)
  • Changing codec/resolution while trimming
  • Social media clips (exact timing matters)

--codec - Video Codec

Specify output codec when re-encoding (accurate mode only).

Options:

  • copy - Stream copy, no re-encoding ← default in fast mode
  • h264 - Universal compatibility
  • h265 - Better compression, modern devices
  • vp9 - Web/YouTube

Examples:

# Trim and compress with H.265
mediaproc video trim video.mp4 -s 60 -d 30 -c h265

# Trim without re-encoding
mediaproc video trim video.mp4 -s 60 -d 30 -c copy --fast

--fade-in / --fade-out - Fade Effects

Add smooth fade-in at start and/or fade-out at end (requires re-encoding).

Format: Duration in seconds

Fade-In:

  • Gradually increases brightness from black
  • Duration counted from trim start point

Fade-Out:

  • Gradually decreases brightness to black
  • Duration counted backward from trim end point

Examples:

# 2-second fade-in, 3-second fade-out
mediaproc video trim video.mp4 -s 30 -d 60 --fade-in 2 --fade-out 3

# Just fade-out
mediaproc video trim video.mp4 -s 00:01:00 -e 00:02:00 --fade-out 2

Result Preview:

[0s=====fade-in====2s]===normal====[58s====fade-out====60s]
Tip

Professional Touch: Use 1-2 second fades for professional-looking clips. Longer fades (3-5s) work for dramatic effect.


--speed - Playback Speed

Adjust playback speed while trimming (requires re-encoding).

Range: 0.5 to 2.0

  • 0.5 = Half speed (slow motion)
  • 0.75 = 3/4 speed
  • 1.0 = Normal speed ← default
  • 1.25 = 1.25x speed
  • 1.5 = 1.5x speed
  • 2.0 = Double speed (fast forward)

Examples:

# Slow motion trim (half speed)
mediaproc video trim action.mp4 -s 30 -d 10 --speed 0.5

# Fast forward (2x speed)
mediaproc video trim video.mp4 -s 00:05:00 -d 00:10:00 --speed 2

# Slightly faster (common for time-lapse effect)
mediaproc video trim video.mp4 -s 60 -d 120 --speed 1.5

Audio Behavior:

  • Audio pitch is preserved (not chipmunk/deep voice effect)
  • Uses FFmpeg's atempo filter for natural-sounding speed changes
Info

Duration Changes: Changing speed affects output duration. 60s at 2x speed = 30s output. 60s at 0.5x speed = 120s output.


--volume - Audio Volume

Adjust audio volume level (requires re-encoding).

Range: 0.0 to 2.0

  • 0.0 = Mute (silent)
  • 0.5 = Half volume (-6 dB)
  • 1.0 = Original volume ← default
  • 1.5 = 50% louder (+3.5 dB)
  • 2.0 = Double volume (+6 dB)

Examples:

# Lower volume by half
mediaproc video trim video.mp4 -s 30 -d 60 --volume 0.5

# Increase volume
mediaproc video trim quiet-video.mp4 -s 0 -d 120 --volume 1.5

# Mute audio
mediaproc video trim video.mp4 -s 60 -d 30 --volume 0
Warning

Avoid Distortion: Values above 1.5 may cause audio clipping/distortion. Use audio normalization tools for professional volume adjustment.


--quality - Encoding Quality

Set CRF quality when re-encoding (accurate mode only).

Range: 0-51, lower = better quality

  • 18 = Near-lossless (large files)
  • 20-23 = High quality ← 23 is default
  • 24-28 = Good quality (smaller files)
  • 29+ = Lower quality (compression artifacts)

Examples:

# High quality trim
mediaproc video trim video.mp4 -s 30 -d 60 --quality 20

# Smaller file size
mediaproc video trim video.mp4 -s 30 -d 60 --quality 28

--no-audio - Remove Audio

Strip audio track from output (video only).

Use Cases:

  • Creating GIF-like clips
  • Background/B-roll footage
  • Silent video for presentations
  • Reducing file size when audio not needed

Example:

mediaproc video trim video.mp4 -s 30 -d 60 --no-audio

--format - Output Format

Specify output container format.

Options: mp4, mkv, webm, avi

Format Guide:

  • mp4 - Universal compatibility ← default
  • mkv - Advanced features, multi-audio/subtitle support
  • webm - Web streaming (requires VP9/VP8 codec)
  • avi - Legacy compatibility

Example:

mediaproc video trim video.mp4 -s 60 -d 120 --format mkv

Time can be specified in two formats:

HH:MM:SS Format

  • Hours:Minutes:Seconds: 01:30:45
  • Minutes:Seconds: 05:30
  • Seconds only: 00:00:30

Seconds Format

  • Integer seconds: 90 (= 1 minute 30 seconds)
  • Decimal seconds: 90.5 (= 1 minute 30.5 seconds)

Modes

Fast Mode (--fast)

  • Method: Stream copy (no re-encoding)
  • Speed: Very fast (real-time or faster)
  • Accuracy: May be slightly off (up to 2-3 seconds)
  • Quality: Lossless (exact copy of source frames)
  • Use case: Quick cuts where frame precision isn't critical

Accurate Mode (default)

  • Method: Re-encodes video
  • Speed: Slower (depends on video length and codec)
  • Accuracy: Frame-perfect cuts
  • Quality: Near-lossless with default CRF 23
  • Use case: Professional editing, precise timing required

Examples

Basic Trimming

Terminal
$ mediaproc video trim video.mp4 --start 00:01:00 --end 00:02:00
Input: video.mp4 • Duration: 00:05:30
Resolution: 1920x1080
Trimming from 00:01:00 to 00:02:00...
✓ Trim complete
Output: video_trimmed.mp4 • Duration: 00:01:00 • 45.2 MB
Terminal
$ mediaproc video trim video.mp4 --start 30 --duration 60
✓ Trimmed from 30s for 60s duration
Terminal
$ mediaproc video trim video.mp4 --duration 10
✓ Extracted first 10 seconds

Specify Output Location

Terminal
$ mediaproc video trim video.mp4 -s 00:01:00 -e 00:02:00 -o highlight.mp4
✓ Saved to highlight.mp4
Terminal
$ mediaproc video trim video.mp4 -s 01:00 -d 30 -o clips/
✓ Saved to clips/video_trimmed.mp4

Using Seconds

Terminal
$ mediaproc video trim video.mp4 -s 90 -e 180
✓ Trimmed from 1:30 to 3:00
$ mediaproc video trim video.mp4 -d 45
$ mediaproc video trim video.mp4 -s 30.5 -d 15.5

Fast Mode (Stream Copy)

Terminal
$ mediaproc video trim video.mp4 -s 00:01:00 -e 00:02:00 --fast
✓ Fast trim complete (no re-encoding)
Terminal
$ mediaproc video trim long-video.mp4 -s 10:00 -d 60 --fast
Input: long-video.mp4 • Duration: 01:30:00
[FAST MODE] Stream copy (no re-encoding)
Trimming from 00:10:00 for 00:01:00...
✓ Trim complete (2.3 seconds)
Output: long-video_trimmed.mp4

Extract Specific Segments

Terminal
$ mediaproc video trim video.mp4 -d 5 -o intro.mp4
✓ Extracted intro (first 5 seconds)
Terminal
$ mediaproc video trim video.mp4 -s 00:01:00 -e 00:03:00 -o middle.mp4
✓ Extracted middle section (1:00 - 3:00)
Terminal
$ mediaproc video trim video.mp4 -s 00:05:20 -o outro.mp4
✓ Extracted outro (last 10 seconds)

Dry Run Preview

# Preview trim operation
mediaproc video trim video.mp4 -s 01:00 -e 02:00 --dry-run

Output:

[DRY RUN] Would execute: ffmpeg -i video.mp4 -ss 60 -to 120 -c:v libx264 -crf 23 -c:a aac video_trimmed.mp4 Input: video.mp4 (Duration: 00:05:30) Trim: 00:01:00 to 00:02:00 (Duration: 00:01:00) Mode: Accurate (re-encode)

Duration from Start

# Trim first 30 seconds
mediaproc video trim video.mp4 --duration 30

# Trim 2 minutes from beginning
mediaproc video trim video.mp4 --duration 00:02:00

# Combined with start time
mediaproc video trim video.mp4 -s 00:01:00 --duration 45

Multiple Clips

# Extract multiple segments
mediaproc video trim video.mp4 -s 00:00:30 -e 00:01:00 -o clip1.mp4
mediaproc video trim video.mp4 -s 00:02:00 -e 00:02:30 -o clip2.mp4
mediaproc video trim video.mp4 -s 00:03:15 -e 00:04:00 -o clip3.mp4

# Then merge them
mediaproc video merge clip1.mp4 clip2.mp4 clip3.mp4 -o highlights.mp4

Use Cases

Remove Intro/Outro

Remove unwanted sections from videos:

# Remove first 10 seconds (skip intro)
mediaproc video trim video.mp4 -s 10

# Remove last 5 seconds (requires knowing video length)
# For 5:30 video, trim to 5:25
mediaproc video trim video.mp4 -e 00:05:25

# Remove both intro and outro
# For 10:00 video, keep 00:05 to 09:55
mediaproc video trim video.mp4 -s 5 -e 595

Create Highlights

Extract best moments from long videos:

# Sports highlight at 2:30
mediaproc video trim match.mp4 -s 00:02:25 -d 10 -o goal.mp4

# Presentation key point
mediaproc video trim webinar.mp4 -s 00:15:00 -d 120 -o key-point.mp4

# Funny moment
mediaproc video trim stream.mp4 -s 01:23:45 -d 30 -o funny-clip.mp4

Social Media Clips

Create short clips for social platforms:

# Instagram Reel (15 seconds)
mediaproc video trim video.mp4 -s 00:01:30 -d 15 -o reel.mp4

# TikTok video (up to 60 seconds)
mediaproc video trim video.mp4 -s 00:02:00 -d 60 -o tiktok.mp4

# Twitter video (up to 2:20)
mediaproc video trim video.mp4 -s 00:00:30 -d 140 -o twitter.mp4

Presentation Segments

Break long presentations into chapters:

# Chapter 1: Introduction
mediaproc video trim webinar.mp4 -s 00:00:00 -e 00:05:00 -o chapter1-intro.mp4

# Chapter 2: Content
mediaproc video trim webinar.mp4 -s 00:05:00 -e 00:25:00 -o chapter2-content.mp4

# Chapter 3: Q&A
mediaproc video trim webinar.mp4 -s 00:25:00 -o chapter3-qa.mp4

Tutorial Sections

Extract specific steps from tutorial videos:

# Step 1
mediaproc video trim tutorial.mp4 -s 00:01:00 -e 00:03:30 -o step1.mp4

# Step 2
mediaproc video trim tutorial.mp4 -s 00:03:30 -e 00:07:15 -o step2.mp4

# Step 3
mediaproc video trim tutorial.mp4 -s 00:07:15 -e 00:12:00 -o step3.mp4

Remove Mistakes

Cut out errors from recordings:

# Remove mistake at 2:30-2:45
# Trim two segments and merge
mediaproc video trim recording.mp4 -e 00:02:30 -o part1.mp4
mediaproc video trim recording.mp4 -s 00:02:45 -o part2.mp4
mediaproc video merge part1.mp4 part2.mp4 -o clean.mp4

Technical Details

Stream Copy (Fast Mode)

Fast mode uses stream copy (-c copy):

ffmpeg -i input.mp4 -ss 60 -to 120 -c copy output.mp4

Advantages:

  • Extremely fast (no decoding/encoding)
  • Lossless (exact frames from source)
  • Minimal CPU usage

Limitations:

  • May not cut at exact frame
  • Depends on keyframe positions
  • Can be off by 0-3 seconds
  • First frame might be black

When to use:

  • Rough cuts acceptable
  • Speed is priority
  • Very long videos
  • Multiple quick previews

Re-encoding (Accurate Mode)

Accurate mode re-encodes video:

ffmpeg -i input.mp4 -ss 60 -to 120 -c:v libx264 -crf 23 -c:a aac output.mp4

Advantages:

  • Frame-perfect accuracy
  • Clean start/end frames
  • Consistent results
  • No black frames

Limitations:

  • Slower processing
  • Quality loss (minimal with CRF 23)
  • CPU intensive

When to use:

  • Professional editing
  • Precise timing required
  • Final deliverables
  • Concatenating multiple clips

Time Calculation

End time vs Duration:

  • --end: Absolute position in video
  • --duration: Length of output clip

Examples:

# These are equivalent:
mediaproc video trim video.mp4 -s 60 -e 120
mediaproc video trim video.mp4 -s 60 -d 60

# Start=1:00, End=2:30, Duration=1:30
mediaproc video trim video.mp4 -s 00:01:00 -e 00:02:30
# Same as:
mediaproc video trim video.mp4 -s 00:01:00 -d 00:01:30

Time Validation

The command validates times against video duration:

# Error: End time exceeds video duration
mediaproc video trim short-video.mp4 -e 00:10:00
# Error: Video is only 00:05:00 long

Performance Considerations

Fast Mode Speed

Approximate processing speeds:

  • Fast mode: Real-time or faster (seconds for any length)
  • Accurate mode: Depends on trim length and codec
    • H.264: ~1-2x real-time
    • H.265: ~0.3-0.5x real-time

Disk Usage

Temporary files are not created. Output is written directly.

Memory Usage

Memory usage scales with:

  • Video resolution (4K uses more RAM)
  • Codec complexity
  • Not dependent on video length

Error Handling

Common Errors

Invalid time format:

Error: Invalid time format '1:75:00' Use HH:MM:SS or seconds (e.g., '01:15:00' or '75')

End before start:

Error: End time (00:01:00) must be after start time (00:02:00)

Time exceeds duration:

Error: End time 00:10:00 exceeds video duration 00:05:30

Missing time parameters:

Error: Must specify --end or --duration

Best Practices

  1. Use fast mode for preview: Test cuts quickly with --fast
  2. Use accurate mode for final: Produce final clips without --fast
  3. Test with --dry-run: Preview commands before execution
  4. Note keyframe positions: Fast mode cuts at keyframes
  5. Add buffer time: For fast mode, add 1-2 seconds buffer
  6. Keep originals: Never trim source files directly
  7. Verify output: Always check trimmed videos play correctly
  8. Document times: Keep notes of interesting timestamps

Troubleshooting

Black Frames at Start (Fast Mode)

Fast mode may produce black frames:

Solution: Use accurate mode (remove --fast)

# Instead of:
mediaproc video trim video.mp4 -s 60 --fast

# Use:
mediaproc video trim video.mp4 -s 60

Inaccurate Cut Points (Fast Mode)

Fast mode cuts at nearest keyframe:

Solution 1: Use accurate mode

mediaproc video trim video.mp4 -s 60 -e 120

Solution 2: Add buffer time in fast mode

# Add 2 seconds before/after desired time
mediaproc video trim video.mp4 -s 58 -e 122 --fast

Audio/Video Desync

If audio and video become desynchronized:

Solution: Use accurate mode (default)

mediaproc video trim video.mp4 -s 60 -e 120

Processing Too Slow

If accurate mode is too slow:

Solution 1: Use fast mode for rough cuts

mediaproc video trim video.mp4 -s 60 -e 120 --fast

Solution 2: Use faster codec (H.264 vs H.265)

# Fast mode automatically uses stream copy

Advanced Examples

Extract Multiple Highlights

#!/bin/bash
# Extract multiple highlight clips from a video

input="long-video.mp4"
output_dir="highlights"

# Define highlights (start_time duration output_name)
highlights=(
  "00:02:30 15 goal1"
  "00:15:45 20 goal2"
  "01:05:20 12 goal3"
)

mkdir -p "$output_dir"

for highlight in "${highlights[@]}"; do
  read -r start duration name <<< "$highlight"
  echo "Extracting $name..."
  mediaproc video trim "$input" -s "$start" -d "$duration" -o "$output_dir/$name.mp4"
done

echo "Merging highlights..."
mediaproc video merge "$output_dir"/*.mp4 -o "all-highlights.mp4"

Remove Commercials

#!/bin/bash
# Remove commercial breaks from recording

input="tv-recording.mp4"

# Keep these segments (content)
mediaproc video trim "$input" -s 00:00:00 -e 00:15:00 -o part1.mp4
mediaproc video trim "$input" -s 00:18:00 -e 00:33:00 -o part2.mp4
mediaproc video trim "$input" -s 00:36:00 -e 00:51:00 -o part3.mp4

# Merge without commercials
mediaproc video merge part1.mp4 part2.mp4 part3.mp4 -o no-commercials.mp4

# Cleanup
rm part1.mp4 part2.mp4 part3.mp4

Time-based Splitting

#!/bin/bash
# Split video into equal segments

input="lecture.mp4"
segment_duration=300  # 5 minutes

# Get video duration (requires ffprobe)
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$input")
duration=${duration%.*}  # Remove decimal

segment=1
for ((start=0; start<duration; start+=segment_duration)); do
  echo "Creating segment $segment..."
  mediaproc video trim "$input" -s "$start" -d "$segment_duration" -o "segment-$segment.mp4"
  ((segment++))
done

Precision Timing Log

#!/bin/bash
# Log exact times for review

input="video.mp4"

echo "Timestamp,Duration,Output" > trim-log.csv

# Array of clips (start, end, name)
clips=(
  "00:01:23,00:01:45,intro"
  "00:05:30,00:06:15,demo"
  "00:10:00,00:11:30,outro"
)

for clip in "${clips[@]}"; do
  IFS=',' read -r start end name <<< "$clip"

  mediaproc video trim "$input" -s "$start" -e "$end" -o "$name.mp4"

  # Calculate duration
  start_sec=$(echo "$start" | awk -F: '{print $1*3600 + $2*60 + $3}')
  end_sec=$(echo "$end" | awk -F: '{print $1*3600 + $2*60 + $3}')
  duration=$((end_sec - start_sec))

  echo "$start-$end,$duration,$name.mp4" >> trim-log.csv
done
  • merge - Join trimmed clips together
  • extract-frames - Extract specific frames instead of segments
  • resize - Change resolution of trimmed clips
  • compress - Reduce size of trimmed videos

See Also

Found an issue? Help us improve this page.

Edit on GitHub →