ugh zenity

This commit is contained in:
Lexi Deville 2023-10-26 02:50:26 -05:00 committed by GitHub
parent db64f2ceac
commit 5230e82170
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,14 +4,15 @@ input_file="$1"
output_extension="$2"
filename=$(basename -- "$input_file")
filename_noext="${filename%.*}"
if [ "$output_extension" == "alac" ]; then # alac is a special babyboy
# alac is a special babyboy
if [ "$output_extension" == "alac" ]; then
output_file="${filename_noext}.m4a"
else
output_file="${filename_noext}.${output_extension}"
fi
lossy=("mp3" "aac" "ogg" "wma")
lossy=("mp3" "aac" "ogg" "wma" "m4a")
lossless=("flac" "alac" "wav" "aiff")
# dont overwrite a file
@ -20,53 +21,56 @@ if [ -e "$output_file" ]; then
exit 1
fi
# init var for ffmpeg stderrrrr
error_output=""
# warn if lossy to lossless
# Check for conversion between lossy and lossless and show a warning
input_extension="${filename##*.}"
if [[ " ${lossy[@]} " =~ " ${input_extension} " ]] && [[ " ${lossless[@]} " =~ " ${output_extension} " ]]; then
if ! zenity --question --text="WARNING: Converting from a lossy to a lossless format.\n\nQuality will NOT improve.\n\nCompression artifacts will be present.\n\nThis betrays the point of lossless formats.\n\nPRESERVATIONISTS WILL WEEP.\n\nContinue?"; then
zenity --question --text="WARNING: Converting from a lossy to a lossless format.\n\nQuality will NOT improve.\n\nCompression artifacts will be present.\n\nThis betrays the point of lossless formats.\n\nPRESERVATIONISTS WILL WEEP.\n\nContinue?" \
--ok-label="Continue" --cancel-label="Cancel" --default-cancel --icon-name="warning"
if [ $? -ne 0 ]; then
kill $ZENITY_PID
exit 1
fi
fi
# warn if lossless to lossy
input_extension="${filename##*.}"
if [[ " ${lossless[@]} " =~ " ${input_extension} " ]] && [[ " ${lossy[@]} " =~ " ${output_extension} " ]]; then
if ! zenity --question --text="WARNING: Converting from a lossless to a lossy format.\n\nQuality will NOT be preserved.\n\nCompression artifacts will be PERMANENTLY introduced.\n\nCONVERTING BACK TO LOSSLESS WILL NOT UNDO THIS.\n\nContinue?"; then
zenity --question --text="WARNING: Converting from a lossless to a lossy format.\n\nQuality will NOT be preserved.\n\nCompression artifacts will be PERMANENTLY introduced.\n\nCONVERTING BACK TO LOSSLESS WILL NOT UNDO THIS.\n\nContinue?" \
--ok-label="Continue" --cancel-label="Cancel" --default-cancel --icon-name="warning"
if [ $? -ne 0 ]; then
kill $ZENITY_PID
exit 1
fi
fi
# ffmpeg cmd changes based on ext
(
if [[ " ${lossy[@]} " =~ " ${output_extension} " ]] || [[ " ${lossless[@]} " =~ " ${output_extension} " ]]; then
if [ "$output_extension" == "alac" ]; then # alac is still a special babyboy
ffmpeg -i "$input_file" -acodec alac "$output_file" 2>&1 &
else
error_output=$(ffmpeg -i "$input_file" -q:a 0 -map a "$output_file" 2>&1) &
ffmpeg -i "$input_file" -q:a 1 -map a "$output_file" 2>&1 &
fi
else
error_output=$(ffmpeg -i "$input_file" -q:a 3 "$output_file" 2>&1) &
ffmpeg -i "$input_file" -q:a 1 "$output_file" 2>&1 &
fi
) | zenity --progress --auto-close --auto-kill --text="Converting..."
FFMPEG_PID=$!
# check status
if [ $? -eq 0 ]; then
# zenity --info --text="Done"
else
# i hate zenity tbh
( while kill -0 $FFMPEG_PID 2> /dev/null; do
echo "# Converting\n$filename\nto\n$output_file"
sleep 1
done
) | zenity --progress --title="Converting Media" --text="Initializing..." --auto-close
# check if cancelled
if [ $? -eq 1 ]; then
kill $FFMPEG_PID
rm -f "$output_file"
zenity --error --text="Conversion cancelled or failed"
zenity --error --text="Conversion canceled. Target file deleted."
exit 1
fi
# check exit statuses
if [ $error_output -ne 0 ]; then
# y u fail, ffmpeg????
if echo "$error_output" | grep -q "Invalid data found when processing input"; then
kdialog --error "FFmpeg does not support source file type."
else
kdialog --error "An unknown error occurred:\n$error_output"
fi
wait $FFMPEG_PID
if [ $? -ne 0 ]; then
zenity --error --text="An error occurred during conversion."
exit 1
fi