summaryrefslogtreecommitdiff
path: root/mac/.local/bin/cutvideo
blob: 322000871f267821c525c25b2bc4d3831899f910 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh

usage() {
  echo "Crop a video file using ffmpeg."
  echo ""
  echo "Usage: cutvideo [file_name] [position] [duration]"
  echo ""
  echo "Arguments:"
  echo "  file_name:   The name of the video file (e.g., video.mp4)."
  echo "  position:    The start position in the format HH:MM:SS (e.g., 00:00:00)."
  echo "  duration:    The duration in seconds from the start position (e.g., 10)."
  echo ""
  echo "Example:"
  echo "  cutvideo ~/Video/video.mp4 00:01:00 10"
  echo "  This will create a 10-second cut starting at 00:01:00 in the video.mp4 file."
  exit 1
}

[ -z "$1" ] && echo "Target file missing" && usage
[ -z "$2" ] && echo "Target position missing" && usage
[ -z "$3" ] && echo "Target duration missing" && usage

file="$1"
filename="${file%%.*}"
ext="${file##*.}"
num=1

# Find a unique filename by incrementing num
if [ -f "${filename}_cut.${ext}" ]; then
  while [ -f "${filename}_cut_$(printf "%02d" "$num").${ext}" ]; do
    num=$((num + 1))
  done
  new_filename="${filename}_cut_$(printf "%02d" "$num").${ext}"
else
  new_filename="${filename}_cut.${ext}"
fi

# Perform the cut using ffmpeg
ffmpeg -hide_banner -ss "$2" -to "$3" -i "$file" -c copy "$new_filename"

echo "Created file: $new_filename"