Jafner.net/dotfiles/utility-scripts/ffmpeg/clip
2024-07-15 15:35:16 -07:00

70 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# This script should be runnable either from the CLI, or from KDE's Dolphin file manager, or GNOME's Nautilus file manager.
# Each file manager handles input file differently.
# Set the $FILE variable to act on. Varies with file manager, but Dolphin's servicemenus and CLI both use $1
if ! [ -z $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS ]; then
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" > ~/.nautilus_script_selected_file_paths
if (( $(grep -c . <<<"$(cat ~/.nautilus_script_selected_file_paths)") > 1 )); then notify-send -u critical "Script error" "Selecting more than 1 file is not supported."; exit 1; fi
rm ~/.nautilus_script_selected_file_paths
INPUT_FILE="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
elif ! [ -z "$1" ]; then
INPUT_FILE="$1"
else
echo "No file selected."
exit 1
fi
while [ -z $START_TIMESTAMP ]; do
START_TIMESTAMP=$(kdialog --title "Clip: Start timestamp" --inputbox "Timestamp for start of clip \nE.g. 1:23:04 \n(Remember to align to video keyframe)")
done
while [ -z $END_TIMESTAMP ]; do
END_TIMESTAMP=$(kdialog --title "Clip: End timestamp" --inputbox "Timestamp for end of clip \nE.g. 1:24:44")
done
while [ -z $CLIP_NAME ]; do
CLIP_NAME=$(kdialog --title "Clip: Name" --inputbox "Name of the clip \nE.g. \"Forecast calls for rain\" will end up like:\n\"[2024-02-09] Forecast calls for rain.mp4\"")
done
# TODO: This variable does not seem to resolve to 0 or 1 as expected, but always returns empty
while [ -z $KEEP_CLIP_RANGE ]; do
KEEP_CLIP_RANGE=$(kdialog --title "Clip: Keep clip range in filename" --yesno "Do you want to keep the clip range in the resulting filename?\nE.g. \"[2024-02-09] Forecast calls for rain (1:23:04-1:24:44).mp4\"")
done
# We know our raw recordings are named like "[YYYY-MM-DD] HH:mm:ss.mkv"
# We want to turn "[YYYY-MM-DD] HH:mm:ss.mkv" into
# "[YYYY-MM-DD] Clip Name.mp4"
# We want to preserve the date, but we don't need the timestamp.
# Break up the input file's parts so we know the:
# - absolute path (e.g. /home/user/Videos/MyVideo.mkv)
# - file name (with extension) (e.g. MyVideo.mkv)
# - file extension (e.g. mkv)
# - file name (without extension) (e.g. MyVideo)
# - composed output file absolute path (e.g. /home/user/Videos/MyVideo-clip.mp4)
INPUT_FILE=$(realpath "$INPUT_FILE")
FILE_PATH=$(dirname "$INPUT_FILE")
FILE_NAME=$(basename "$INPUT_FILE")
FILE_EXT="${FILE_NAME##*.}"
FILE_NAME="${FILE_NAME%.*}"
FILE_DATESTAMP=$(echo $FILE_NAME | cut -d' ' -f1)
FILE_TIMESTAMP=$(echo $FILE_NAME | cut -d' ' -f2)
if ! [ -z $CLIP_NAME ]; then
FILE_NAME="$FILE_DATESTAMP $CLIP_NAME"
else
FILE_NAME="$FILE_DATESTAMP $FILE_TIMESTAMP"
fi
# TODO: See #31
if ! [ $KEEP_CLIP_RANGE ]; then
CLIPPED_FILE=$(echo "/tmp/$FILE_NAME.$FILE_EXT")
else
CLIPPED_FILE=$(echo "/tmp/$FILE_NAME ($START_TIMESTAMP-$END_TIMESTAMP).$FILE_EXT")
fi
CLIPPED_FILE=$(echo "$FILE_PATH/$FILE_NAME.$FILE_EXT")
notify-send -t 2000 "Clip starting" "$FILE_FULL_PATH"
ffmpeg -hide_banner -i "$INPUT_FILE" -ss "$START_TIMESTAMP" -to "$END_TIMESTAMP" -map 0 -c:v copy -c:a copy "$OUTFILE"
notify-send -t 4000 "Clip complete" "$FILE_FULL_PATH"