Skip to content

Instantly share code, notes, and snippets.

@olibartfast
Created July 10, 2025 15:05
Show Gist options
  • Select an option

  • Save olibartfast/3b85494a80e7dffa3ad6e29fdda00388 to your computer and use it in GitHub Desktop.

Select an option

Save olibartfast/3b85494a80e7dffa3ad6e29fdda00388 to your computer and use it in GitHub Desktop.
Extract a segment of a video from a given start time and duration.
#!/bin/bash
###############################################################################
# extract_clip.sh
#
# 🎬 Extract a segment of a video from a given start time and duration.
#
# 📥 Usage:
# ./extract_clip.sh input.mp4 start_time duration output.mp4 [--accurate]
#
# - input.mp4 : Input video file
# - start_time : Start time (e.g. 30 or 00:00:30)
# - duration : Duration in seconds (e.g. 10)
# - output.mp4 : Output file name
# - --accurate : Optional flag for frame-accurate extraction (slower)
#
# 💡 Example:
# ./extract_clip.sh input.mp4 60 5 clip.mp4
# ./extract_clip.sh input.mp4 00:01:00 5 clip.mp4 --accurate
###############################################################################
# Check minimum number of arguments
if [ "$#" -lt 4 ]; then
echo "Usage: $0 input.mp4 start_time duration output.mp4 [--accurate]"
exit 1
fi
INPUT="$1"
START="$2"
DURATION="$3"
OUTPUT="$4"
ACCURATE="$5"
# Choose mode
if [ "$ACCURATE" == "--accurate" ]; then
echo "🎯 Accurate mode: re-encoding for frame-precise cut"
ffmpeg -ss "$START" -i "$INPUT" -t "$DURATION" -c:v libx264 -c:a aac -strict experimental "$OUTPUT"
else
echo "⚡ Fast mode: copying streams (may be imprecise at keyframes)"
ffmpeg -ss "$START" -i "$INPUT" -t "$DURATION" -c copy "$OUTPUT"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment