Files
tool__scripts/manage-video/video-clipper/video-clipper.sh
2026-07-09 01:05:56 +02:00

255 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
CLIP_SCRIPT="./clip.sh"
FRAME_SCRIPT="./frames.sh"
QUEUE_FILE="/tmp/clip_queue.txt"
> "$QUEUE_FILE"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
CYAN="\033[36m"
NC="\033[0m"
video=""
declare -a queue
pick_video() {
#video=$(ls *.mp4 2>/dev/null | fzf --prompt="Select video > ")
video=$(find . -maxdepth 1 -type f \( -iname "*.mp4" -o -iname "*.mkv" \) | fzf --prompt="Select video > ")
if [[ -n "$video" ]]; then
echo "Selected: $video"
else
echo "No selection."
fi
}
preview_timestamp() {
if [[ -z "$video" ]]; then
echo "No video selected"
return
fi
ts="$1"
mpv --quiet --hr-seek=yes --start="$ts" "$video"
}
scrub_video() {
if [[ -z "$video" ]]; then
echo "No video selected"
return
fi
export SCRUB_QUEUE="$QUEUE_FILE"
mpv --keep-open=yes \
--pause \
"$video"
echo ""
echo "Importing marks..."
while read -r line; do
queue+=("$line")
echo "queued $line"
done < "$QUEUE_FILE"
}
show_queue() {
if [ ${#queue[@]} -eq 0 ]; then
echo -e "${YELLOW}(queue empty)${NC}"
return
fi
i=1
for job in "${queue[@]}"; do
type=$(echo "$job" | awk '{print $1}')
start=$(echo "$job" | awk '{print $2}')
end=$(echo "$job" | awk '{print $3}')
if [[ "$type" == "c" ]]; then
label="CLIP"
color=$BLUE
else
label="FRAMES"
color=$CYAN
fi
echo -e " $i. ${color}$label${NC} $start → $end"
((i++))
done
}
save_config() {
if [[ -z "$video" ]]; then
echo "No video selected"
return
fi
name=$(basename "$video")
name="${name%.*}"
dir="./target/$name"
mkdir -p "$dir"
conf="$dir/video-clipper.conf"
{
echo "video=$video"
echo ""
echo "[jobs]"
for job in "${queue[@]}"; do
echo "$job"
done
} > "$conf"
echo -e "${GREEN}Saved config:${NC} $conf"
}
load_config() {
conf="$1"
if [[ ! -f "$conf" ]]; then
echo "Config not found: $conf"
return
fi
queue=()
video=""
mode=""
> "$QUEUE_FILE"
while IFS= read -r line; do
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
if [[ "$line" == "[jobs]" ]]; then
mode="jobs"
continue
fi
if [[ "$line" == video=* ]]; then
video="${line#video=}"
continue
fi
if [[ "$mode" == "jobs" ]]; then
queue+=("$line")
echo "$line" >> "$QUEUE_FILE"
fi
done < "$conf"
echo -e "${GREEN}Loaded config:${NC} $conf"
echo "video: $video"
echo "jobs: ${#queue[@]}"
}
run_queue() {
if [ -z "$video" ]; then
echo -e "${RED}No video selected.${NC}"
return
fi
save_config
for job in "${queue[@]}"; do
type=$(echo "$job" | awk '{print $1}')
start=$(echo "$job" | awk '{print $2}')
end=$(echo "$job" | awk '{print $3}')
echo -e "${GREEN}Running ${type} ${start} -> ${end}${NC}"
if [[ "$type" == "c" ]]; then
$CLIP_SCRIPT -no-source-mv "$start" "$end"
else
$FRAME_SCRIPT -no-source-mv "$start" "$end"
fi
done
queue=()
> "$QUEUE_FILE"
echo -e "${GREEN}Queue finished.${NC}"
}
echo -e "${CYAN}FFmpeg Clip Console${NC}"
echo "commands: video, c, f, list, undo, go, quit"
while true; do
read -e -p "> " cmd a b
case "$cmd" in
video)
pick_video
;;
p)
preview_timestamp "$a"
;;
c)
queue+=("c $a $b")
echo -e "${BLUE}queued clip${NC} $a -> $b"
;;
f)
queue+=("f $a $b")
echo -e "${CYAN}queued frames${NC} $a -> $b"
;;
list)
show_queue
;;
del)
idx=$a
unset 'queue[$((idx-1))]'
queue=("${queue[@]}")
;;
scrub)
scrub_video
;;
undo)
unset 'queue[-1]'
echo "removed last job"
;;
go)
run_queue
;;
quit|exit)
break
;;
load)
conf=$(find ./target -type f -name "video-clipper.conf" \
| fzf --prompt="Select config > " \
--preview 'grep "^video=" {}')
if [[ -n "$conf" ]]; then
load_config "$conf"
else
echo "No selection."
fi
;;
*)
echo -e "${YELLOW}commands: video | scrub | p start | c start end | f start end | list | undo | go | quit${NC}"
;;
esac
done