#!/bin/bash

fileinput=$1

justname=`basename "$fileinput"`
justpath=${fileinput%/*}

varW=`kdialog --inputbox "What width do you want?"`
varH=`kdialog --inputbox "What height do you want?"`
varLM=`kdialog --inputbox "How many pixels to crop from left"`
varTM=`kdialog --inputbox "How many pixels to crop from top?"`

if [ "$varW" == "" ] || [ "$varH" == "" ] ||[ "$varLM" == "" ] ||[ "$varTM" == "" ]; then
    kdialog --sorry "You must enter a value for all dimensions\nEnter 0 for no value" &
    exit
fi

kdialog --yesno "Do you want to play video to check size?"
if [ "$?" = 0 ]; then
    ffplay -i "${fileinput}" -vf "crop=$varW:$varH:$varLM:$varTM"

    kdialog --yesno "Do you want to convert that to a file?"
    if [ "$?" = 0 ]; then
        continue
    else
        kdialog --msgbox "Please try again.\nSettings used this time were:\nWidth = $varW\nHeight = $varH\nCrop from left = $varLM\nCrop from top = $varTM" &
        exit
    fi
elif [ "$?" = 1 ];  then
    kdialog --msgbox "Skipping preview..."
else
    kdialog --error "ERROR";
fi;

kdialog --passivepopup "Converting video
Please be patient" &
ffmpeg -i "$fileinput" -filter:v "crop=$varW:$varH:$varLM:$varTM" -c:a copy -q:v 2 "${justpath}/cropped_${justname}"
kdialog --msgbox "Created $varW x $varH video\ncropped $varLM pixels from left and $varTM pixels from the top.\nUsing file $fileinput To ${justpath}/cropped_${justname}"
