#!/bin/sh
# Super Simple Video Editor
# Copyright (c) 2025 stemsee
# Copyright (C) 2025 stemsee <cou645@gmail.com/>
# gpl 3 license applies for personal unpaid uses,
# https://www.gnu.org/licenses/gpl-3.0.txt
# else a Unique Arbitrary License 
# must be obtained from the Copyright holder.
# Use at own risk. No liability accepted.
#

if [[ $(id -u) -ne 0 ]]; then
	[[ "$DISPLAY" ]] && exec gtksu "yvedit.sh" "$0" "$@" || exec su -c "$0 $*"
fi

( for i in xrectsel yad yadu ffmpeg xdotool MiniBrowser
do
[ -z "$(type -p $i)" ] && echo "$i"
done ) | yad --title="Missing Dependencies" --text-info --back=yellow --fore=red --fontname="sans bold 28" --center --width=200 --height=200 --timeout=1

if [[ -z "$(type -p ffmpeg)" ]]||[[ -z "$(type -p yad)" ]];then
	echo "Make sure both ffmpeg and yad are installed!"
	exit 1
fi

function exportvidfn {
	DATE=$(date +%Y%m%d%H%M%S)
	filename=$(cat /tmp/yve.file)
	
		case "$6" in
		addmultiaudio) audioi=$(yad --file --multiple --width=200 --height=200 --show-preview \
		--file-filter "Audio Files | *.au *.ogg *.wav *.mp3 *.aac" --separator='\n')
		echo -e "#!/bin/sh\nffmpeg -y -i $filename " >/tmp/op
		cnt=1
		echo "$audioi" | while read line
		do
		num=$(yad --title="$line" --text="Insert at timestamp" --entry "00:00:00")
		 echo " -itsoffset $num -i $line " >>/tmp/op
		 export cnt
		 cnt=$((cnt + 1))
		done
		echo -e " -filter_complex amix=inputs=$cnt[a] -map 0:v -map [a] -c:v copy -async 1 -c:a aac -strict experimental $3.$4\nffplay -i $3.$4" >>/tmp/op
		chmod 755 /tmp/op
		eval exec /tmp/op;;
		
	 replaceaudio) audioi=$(yad --file --width=200 --height=200 --show-preview \
	 --file-filter "Audio Files | *.au *.ogg *.wav *.mp3 *.aac" --separator='\n')
		ffmpeg -i -an "$filename" -i "$audioi" -c:v copy -map 0:v -map 1:a -y "$3.$4";;
		
	 addaudiotrack) audioi=$(yad --file --width=200 --height=200 --show-preview \
	 --file-filter "Audio Files | *.au *.ogg *.wav *.mp3 *.aac" --separator='\n')
		ffmpeg -i "$filename" -i "$audioi" -c:v copy -map 0:v -map 1:a -y "$3.$4";;
	
	 cut) if [[ ! -z "$(echo "$1" | grep ':')" ]]; then
			IFS=':' read -r hrs mins secs <<<"$1"
			startsecs=$(((($hrs*60*60) + $mins*60) + $secs))
		  else
			startsecs="$1"
		  fi
		  if [[ ! -z "$(echo "$2" | grep ':')" ]]; then
		   	IFS=':' read -r hours minutes seconds <<<"$2"
			endsecs=$(((($hours*60*60) + $minutes*60) + $seconds))
		else
			endsecs="$2"
		fi
		fps="$(ffprobe -v quiet -show_streams -select_streams v:0 $filename | grep "r_frame_rate"  | sed -E 's~(.*[=]{1})([0-9]+)([/]{1}[1]{1})~\2~')"
		frames="$((($endsecs - $startsecs) * $fps))"
		ffmpeg -y -i "$filename" -ss "$1" -to "$2" -frames:v "$frames" -acodec copy -vcodec copy "$3"."$4";;
	
	 convert) ffmpeg -y -i "$filename" -acodec copy -vcodec copy "$3"."$4";;
	
	 scale) if [[ ! -z "$(echo "$1" | grep ':')" ]]||[[ ! -z "$(echo "$2" | grep ':')" ]]; then
				echo "Width or Height format is incorrect; should be without ':'" | yad --text-info --width=200 --height=200 --timeout=39
				exit
			else
				ffmpeg -i "$filename" -vf scale="$1":"$2" "$3.$4"
			fi;;
	
	 gif) if [[ ! -z "$(echo "$1" | grep ':')" ]]||[[ ! -z "$(echo "$2" | grep ':')" ]]; then
		echo "Width or Height format is incorrect; should be without ':'" | yad --text-info --width=200 --height=200 --timeout=39
		else
		option="fps=$1, scale=$2:-1"
		echo -e "#!/bin/sh\nffmpeg -y -i $filename -loop 0 -filter_complex \"$option\" -preset fast $3.gif\nffplay $3.gif" > /tmp/op
		chmod 755 /tmp/op
		eval exec /tmp/op
		fi;;
		
	 crop) case "$5" in 
		mpv) coord=$(xwininfo -name "$filename - mpv" | grep 'geometry'  | cut -f2 -d'y');;
		vlc) coord=$(xwininfo -name "$(basename $filename) - VLC media player" | grep 'geometry'  | cut -f2 -d'y');;
		ffplay|yadu) coord=$(xwininfo -name "$filename" | grep 'geometry'  | cut -f2 -d'y');;
		esac
		IFS='+|x' read -r x y a b <<<"$coord"
		IFS='+|x' read -r w z c d <<<"$(xrectsel)"
		u=$((${c} - ${a}))
		v=$((${d} - ${b}))
		option="crop=$w:$z:$u:$v"
		echo -e "#!/bin/sh\nffmpeg -y -i $filename -vf \"$option\" -c:a copy crop-$3.$4\nffplay -i crop-$3.$4 &" > /tmp/op
		chmod 755 /tmp/op
		eval exec /tmp/op;;
	
	 concatenate) parts="$(yad --file --file-filter "Video Files | *.mp4 *.3gp *.mov *.webv *.mkv" --multiple --separator='\n')"
				rm -f /tmp/input.txt; echo -e "$parts" | while read line; do echo -e "file '$line'" >> /tmp/input.txt;done
				yadu --text="Arrange Order Then\nRight Click and save as /tmp/input.txt" --text-info --save --filename=/tmp/input.txt --editable --file-op
				ffmpeg -f concat -safe 0 -i /tmp/input.txt -c copy "$3.$4";;

	esac
	
	sleep 1
	
	case "$5" in
		mpv) "$5" --keep-open=yes "$3.$4";;
		vlc) "$5" "$3.$4";;
		ffplay) "$5" -i "$3.$4";;
		defaultbrowser) "$5" "$3.$4";;
		yadu) yadu --title="$3.$4" --html --browser --uri="$3.$4" --on-top;;
		internal) ln -f -s "$3.$4" /tmp/placeholder;;
	esac
}; export -f exportvidfn

function get_vidfn {
	export filename=$(yad --title="Select Video" --file --mouse --width=200 --height=200)
	echo "$filename" > /tmp/yve.file
	export length=$(ffprobe -i "$filename" -show_entries format=duration -v quiet -of csv="p=0")
	case "$1" in
		mpv) "$1" --keep-open=yes "$filename";;
		vlc) "$1" "$filename";;
		ffplay) "$1" -i "$filename";;
		defaultbrowser) "$1" "$filename";;
		yadu) yadu --title="$filename" --html --browser --uri="$filename" --on-top;;
		internal) ln -f -s "$filename" /tmp/placeholder;;
	esac
};export -f get_vidfn

case "$1" in

1) yadu --plug=$$ --tabnum=1 --html --file-op --browser &
	yadu --plug=$$ --tabnum=2 --form --listen --columns=5 --item-separator='~' --on-top \
	--no-buttons --undecorated --geometry=909x98+442-54 --field="Start / Width":icon "00:00:00" --field="End / Height":icon "00:00:00" \
	--field="Output Name":icon "output" --field="Output Format":cbe "mkv~mp4~mov~3gp~gif" \
	--field="Player":cbe "ffplay~vlc~yadu~mpv~internal~defaultbrowser" \
	--field="Operation":cbe "cut~convert~crop~scale~concatenate~replaceaudio~addmultiaudio~addaudiotrack~gif" --field="Video":fbtn "bash -c \"get_vidfn "%5" \"" \
	--field="Export:fbtn" "bash -c \"exportvidfn "%1" "%2" "%3" "%4" "%5" "%6" \""  &
	yadu --title="Yad Video Editor" --paned --no-buttons --key=$$ --tab=player --tab=control --splitter=440 --height=600 --width=600 --orient=vertical &
	;;

2|*) yad --form --listen --columns=5 --item-separator='~' --on-top \
	--no-buttons --undecorated --geometry=909x98+442-54 --field="Start / Width":icon "00:00:00" --field="End / Height":icon "00:00:00" \
	--field="Output Name":icon "output" --field="Output Format":cbe "mkv~mp4~mov~3gp" \
	--field="Player":cbe "ffplay~vlc~yadu~mpv~internal~defaultbrowser" \
	--field="Operation":cbe "cut~convert~crop~scale~concatenate~replaceaudio~addmultiaudio~addaudiotrack~gif" --field="Video":fbtn "bash -c \"get_vidfn "%5" \"" \
	--field="Export:fbtn" "bash -c \"exportvidfn "%1" "%2" "%3" "%4" "%5" "%6" \""  &
	;;

esac

wait $!

case $? in
0|1|252) rm -f /tmp/vidpipe
rm -f /tmp/yve.file;;
esac
exit 0
