From e5e4128458f5695b495ea4ce84d0fcc40a8f6e9c Mon Sep 17 00:00:00 2001 From: Matheus Albino Date: Fri, 9 Aug 2024 19:21:28 -0300 Subject: [PATCH] Script updates --- scripts/shell/backup-stuff.sh | 2 + scripts/shell/ffmpeg-get-video-fps.sh | 3 + scripts/shell/ffmpeg-helper.sh | 166 ++++++++++++-------- scripts/shell/ffmpeg-mp4-to-gif.sh | 2 +- scripts/shell/images-indexer.sh | 212 +++++++++++++------------- 5 files changed, 215 insertions(+), 170 deletions(-) create mode 100644 scripts/shell/backup-stuff.sh create mode 100755 scripts/shell/ffmpeg-get-video-fps.sh diff --git a/scripts/shell/backup-stuff.sh b/scripts/shell/backup-stuff.sh new file mode 100644 index 0000000..a48c04c --- /dev/null +++ b/scripts/shell/backup-stuff.sh @@ -0,0 +1,2 @@ +# vencord stuff +cp '/mnt/c/Users/cloud/AppData/Roaming/Vencord/settings/settings.json' '/mnt/e/home/clouds/google-drive/backup' diff --git a/scripts/shell/ffmpeg-get-video-fps.sh b/scripts/shell/ffmpeg-get-video-fps.sh new file mode 100755 index 0000000..04731b7 --- /dev/null +++ b/scripts/shell/ffmpeg-get-video-fps.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +ffmpeg -i $1 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p" diff --git a/scripts/shell/ffmpeg-helper.sh b/scripts/shell/ffmpeg-helper.sh index 0468329..28a05e1 100755 --- a/scripts/shell/ffmpeg-helper.sh +++ b/scripts/shell/ffmpeg-helper.sh @@ -1,31 +1,31 @@ #!/bin/bash show_help() { - echo "Usage: `basename $0` [--input|-i input_file] [--crop|-c width:height] [--trim-start start] [--trim-end end] [--crf crf_value] [--fps new_fps] [--scale640] [--merge-audio] [--remove-audio] --output|-o output_file" - echo "Options:" - echo " --input|-i Specify the input video file (required)." - echo " --crop|-c Specify the width:height for video cropping." - echo " --trim-start Specify the start timestamp for video trimming." - echo " --trim-end Specify the end timestamp for video trimming." - echo " --crf Specify the CRF value for video compressing." - echo " --fps Specify the new frames per second for the video." - echo " --scale640 Scale the video to 640x320." - echo " --merge-audio Merge all audio tracks into the main track." - echo " --remove-audio Suppress all audio and remove audio tracks." - echo " --audio-track-no Selects a specific audio track for the output" - echo " --output|-o Specify the output filename (required)." + echo "Usage: $(basename $0) [--input|-i input_file] [--crop|-c width:height] [--trim-start start] [--trim-end end] [--crf crf_value] [--fps new_fps] [--scale new_video_resolution] [--merge-audio] [--remove-audio] --output|-o output_file" + echo "Options:" + echo " --input|-i Specify the input video file (required)." + echo " --crop|-c Specify the width:height for video cropping." + echo " --trim-start Specify the start timestamp for video trimming." + echo " --trim-end Specify the end timestamp for video trimming." + echo " --crf Specify the CRF value for video compressing." + echo " --fps Specify the new frames per second for the video." + echo " --scale Scale the video to a new resolution." + echo " --merge-audio Merge all audio tracks into the main track." + echo " --remove-audio Suppress all audio and remove audio tracks." + echo " --audio-track-no Selects a specific audio track for the output" + echo " --output|-o Specify the output filename (required)." } remove_audio() { - input_file="$1" - output_file="$2" + input_file="$1" + output_file="$2" - ffmpeg -i "$input_file" -an "$output_file" + ffmpeg -i "$input_file" -an "$output_file" } -if ! command -v ffmpeg &> /dev/null; then - echo "Error: 'ffmpeg' command not found. Please install ffmpeg." - exit 1 +if ! command -v ffmpeg &>/dev/null; then + echo "Error: 'ffmpeg' command not found. Please install ffmpeg." + exit 1 fi input_file="" @@ -34,73 +34,109 @@ trim_start="" trim_end="" crf="" new_fps="" -scale640=false +scale="" merge_audio=false remove_audio=false audio_track_no=1 output_file="" while [ "$#" -gt 0 ]; do - case "$1" in - --help|-h) show_help; exit ;; - --input|-i) input_file="$2"; shift 2 ;; - --crop|-c) crop="$2"; shift 2 ;; - --trim-start) trim_start="$2"; shift 2 ;; - --trim-end) trim_end="$2"; shift 2 ;; - --crf) crf="$2"; shift 2 ;; - --fps) new_fps="$2"; shift 2 ;; - --scale640) scale640=true; shift ;; - --merge-audio) merge_audio=true; shift ;; - --remove-audio) remove_audio=true; shift;; - --audio-track-no) audio_track_no="$2"; shift;; - --output|-o) output_file="$2"; shift 2 ;; - *) shift ;; - esac + case "$1" in + --help | -h) + show_help + exit + ;; + --input | -i) + input_file="$2" + shift 2 + ;; + --crop | -c) + crop="$2" + shift 2 + ;; + --trim-start) + trim_start="$2" + shift 2 + ;; + --trim-end) + trim_end="$2" + shift 2 + ;; + --crf) + crf="$2" + shift 2 + ;; + --fps) + new_fps="$2" + shift 2 + ;; + --scale) + scale="$2" + shift 2 + ;; + --merge-audio) + merge_audio=true + shift + ;; + --remove-audio) + remove_audio=true + shift + ;; + --audio-track-no) + audio_track_no="$2" + shift + ;; + --output | -o) + output_file="$2" + shift 2 + ;; + *) shift ;; + esac done if [ -z "$input_file" ]; then - echo "Error: Input filename (--input or -i) is required." - exit 1 + echo "Error: Input filename (--input or -i) is required." + exit 1 fi if [ -z "$output_file" ]; then - echo "Error: Output filename (--output or -o) is required." - exit 1 + echo "Error: Output filename (--output or -o) is required." + exit 1 fi -if [ -z "$crop" ] && [ -z "$trim_start" ] && [ -z "$trim_end" ] && [ -z "$crf" ] && [ -z "$new_fps" ] && [ "$merge_audio" = false ] && [ "$scale640" = false ] && [ "$remove_audio" = false ]; then - echo "Error: At least one optional parameter is required." - exit 1 +if [ -z "$crop" ] && [ -z "$trim_start" ] && [ -z "$trim_end" ] && [ -z "$crf" ] && [ -z "$new_fps" ] && [ "$merge_audio" = false ] && [ -z "$scale" ] && [ "$remove_audio" = false ]; then + echo "Error: At least one optional parameter is required." + exit 1 fi if [ -n "$trim_start" ] && [ -z "$trim_end" ]; then - echo "Error: If using --trim-start, you must also specify --trim-end." - exit 1 + echo "Error: If using --trim-start, you must also specify --trim-end." + exit 1 fi if [ -n "$trim_end" ] && [ -z "$trim_start" ]; then - echo "Error: If using --trim-end, you must also specify --trim-start." - exit 1 + echo "Error: If using --trim-end, you must also specify --trim-start." + exit 1 fi -if [ -n "$crop" ] && [ "$scale640" = true ]; then - echo "Error: Cannot use both --crop and --scale640 together." - exit 1 +if [ -n "$crop" ] && [ "$scale" = true ]; then + echo "Error: Cannot use both --crop and --scale together." + exit 1 fi if [ "$merge_audio" = true ] && [ "$remove_audio" = true ]; then - echo "Error: Cannot use both --merge-audio and --remove-audio together." - exit 1 + echo "Error: Cannot use both --merge-audio and --remove-audio together." + exit 1 fi if [ "$merge_audio" = true ] && [ "$audio_track_no" != 1 ]; then - echo "Error: Cannot use both --merge-audio and --audio-track-no together." - exit 1 + echo "Error: Cannot use both --merge-audio and --audio-track-no together." + exit 1 fi if [ "$remove_audio" = true ] && [ "$audio_track_no" != 1 ]; then - echo "Error: Cannot use both --remove-audio and --audio-track-no together." - exit 1 + echo "Error: Cannot use both --remove-audio and --audio-track-no together." + exit 1 fi ffmpeg_command="ffmpeg -i \"$input_file\"" @@ -110,36 +146,36 @@ ffmpeg_command="ffmpeg -i \"$input_file\"" # fi if [ "$remove_audio" = true ]; then - ffmpeg_command+=" -an" + ffmpeg_command+=" -an" fi if [ -n "$crop" ]; then - ffmpeg_command+=" -vf crop=$crop" + ffmpeg_command+=" -vf crop=$crop" fi if [ -n "$trim_start" ] && [ -n "$trim_end" ]; then - ffmpeg_command+=" -ss $trim_start -to $trim_end" + ffmpeg_command+=" -ss $trim_start -to $trim_end" fi if [ -n "$crf" ]; then - ffmpeg_command+=" -c:v libx264 -crf $crf" + ffmpeg_command+=" -c:v libx265 -crf $crf" fi if [ -n "$new_fps" ]; then - ffmpeg_command+=" -r $new_fps" + ffmpeg_command+=" -r $new_fps" fi -if [ "$scale640" = true ]; then - ffmpeg_command+=" -vf scale=640:320" +if [ -n "$scale" ]; then + ffmpeg_command+=" -s $scale" fi if [ "$merge_audio" = true ]; then - num_audio_streams=$(ffprobe -loglevel error -select_streams a -show_entries stream=codec_type -of csv=p=0 "$input_file" | wc -l) - ffmpeg_command+=" -filter_complex amerge=inputs=$num_audio_streams" + num_audio_streams=$(ffprobe -loglevel error -select_streams a -show_entries stream=codec_type -of csv=p=0 "$input_file" | wc -l) + ffmpeg_command+=" -filter_complex amerge=inputs=$num_audio_streams" fi if [ "$audio_track_no" != 1 ]; then - ffmpeg_command+=" -map 0:0 -map 0:$audio_track_no" + ffmpeg_command+=" -map 0:0 -map 0:$audio_track_no" fi ffmpeg_command+=" \"$output_file\"" diff --git a/scripts/shell/ffmpeg-mp4-to-gif.sh b/scripts/shell/ffmpeg-mp4-to-gif.sh index 141b005..60c0d02 100755 --- a/scripts/shell/ffmpeg-mp4-to-gif.sh +++ b/scripts/shell/ffmpeg-mp4-to-gif.sh @@ -8,4 +8,4 @@ video_res=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,heig video_framerate=$(ffmpeg -i $file_to_convert 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p") ffmpeg -y -i $file_to_convert -vf palettegen $pallete_file -ffmpeg -y -i $file_to_convert -i $pallete_file -filter_complex paletteuse "${file_to_convert%"$file_to_convert_extension"}.gif" +ffmpeg -y -i $file_to_convert -i $pallete_file -filter_complex fps=20,paletteuse "${file_to_convert%".$file_to_convert_extension"}.gif" diff --git a/scripts/shell/images-indexer.sh b/scripts/shell/images-indexer.sh index cd5995b..8e1aa8c 100755 --- a/scripts/shell/images-indexer.sh +++ b/scripts/shell/images-indexer.sh @@ -2,7 +2,7 @@ show_help() { # echo "Usage: `basename $0` [--no-abort-on-found] [--parallel] [--custom-grep-search|-c ]" - echo "Usage: `basename $0` [--help|-h] [--filter-mode|-f]" + echo "Usage: $(basename $0) [--help|-h] [--filter-mode|-f]" echo "Options:" echo " --help | -h (optional) Display script's help text" echo " --filter-mode | -f (optional) The script iterate over the files of a selected folder and store the ones that should be deleted" @@ -11,9 +11,15 @@ show_help() { filter_mode=false while [ "$#" -gt 0 ]; do case "$1" in - --help|-h) show_help; exit ;; - --filter-mode|-f) filter_mode=true; shift;; - *) shift ;; + --help | -h) + show_help + exit + ;; + --filter-mode | -f) + filter_mode=true + shift + ;; + *) shift ;; esac done @@ -21,56 +27,56 @@ commands_to_check=(gallery-dl gdl.sh feh xdotool) commands_not_found=() for command in ${commands_to_check[@]}; do - if ! command -v "$command" &> /dev/null; then - commands_not_found+=("$command") - fi + if ! command -v "$command" &>/dev/null; then + commands_not_found+=("$command") + fi done if [ ${#commands_not_found[@]} -ne 0 ]; then - echo 'The following commands are necessary in order to run the script, but were not found:' + echo 'The following commands are necessary in order to run the script, but were not found:' - for command in ${commands_not_found[@]}; do - echo " \"$command\"" - done + for command in ${commands_not_found[@]}; do + echo " \"$command\"" + done - exit 1 + exit 1 fi -cur_dir=`pwd` -furry_commission_ideas_path=/mnt/e/home/documents/data-hoarding/furry-commission-ideas +cur_dir=$(pwd) +furry_commission_ideas_path=/mnt/e/home/documents/downloads-furry furry_commission_ideas_urls_filename="urls.txt" scripts_path=/home/cloud/repos/personal-devboot/scripts/shell if [ ! -d $furry_commission_ideas_path ]; then - echo "[ERROR] The images folder was not found (\"$furry_commission_ideas_path\")" - exit 1 + echo "[ERROR] The images folder was not found (\"$furry_commission_ideas_path\")" + exit 1 fi if [ ! -d $scripts_path ]; then - echo "[ERROR] The scripts folder was not found (\"$scripts_path\")" - exit 1 + echo "[ERROR] The scripts folder was not found (\"$scripts_path\")" + exit 1 fi input_media_url() { - read -p "[INFO] Please inform the media's url: " media_url - echo "$media_url" + read -p "[INFO] Please inform the media's url: " media_url + echo "$media_url" } media_url='' if ! $filter_mode; then - media_url=`input_media_url` + media_url=$(input_media_url) while [ -z "$media_url" ]; do - media_url=`input_media_url` + media_url=$(input_media_url) done # remove trailing slashes - media_url=`sed 's:/*$::' <<< "$media_url"` + media_url=$(sed 's:/*$::' <<<"$media_url") url_regex='(https?)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]' if [[ ! $media_url =~ $url_regex ]]; then - echo "The URL informed is not valid." - exit 1 + echo "The URL informed is not valid." + exit 1 fi fi @@ -82,70 +88,70 @@ filtered_existing_folders=() # removing artists folder depth 2 only for folder in "${existing_folders[@]}"; do - if [[ ! $folder =~ ^artists/[^/]+$ ]]; then - filtered_existing_folders+=( "$folder" ) - fi; + if [[ ! $folder =~ ^artists/[^/]+$ ]]; then + filtered_existing_folders+=("$folder") + fi done existing_folders=("${filtered_existing_folders[@]}") input_create_folder() { - read -p "[INFO] Please create one: " new_foldername - echo "$new_foldername" + read -p "[INFO] Please create one: " new_foldername + echo "$new_foldername" } selected_folder='' if [ -z "${existing_folders}" ]; then - echo "[INFO] There are no folders in the 'furry-commission-ideas' folder." + echo "[INFO] There are no folders in the 'furry-commission-ideas' folder." + new_foldername=$(input_create_folder) + while [ -z "$new_foldername" ]; do new_foldername=$(input_create_folder) - while [ -z "$new_foldername" ]; do - new_foldername=$(input_create_folder) - done + done - mkdir --parents "$new_foldername" - selected_folder="$new_foldername" + mkdir --parents "$new_foldername" + selected_folder="$new_foldername" fi input_create_folder() { - read -p "[INFO] Please informe a name for the new folder: " new_foldername - echo "$new_foldername" + read -p "[INFO] Please informe a name for the new folder: " new_foldername + echo "$new_foldername" } input_select_folder() { - read -p "[INFO] Please inform the desired option: " selected_option + read -p "[INFO] Please inform the desired option: " selected_option - if [ "$selected_option" == 'n' ]; then - selected_folder=`input_create_folder` - while [ -z "$selected_folder" ]; do - selected_folder=`input_create_folder` - done + if [ "$selected_option" == 'n' ]; then + selected_folder=$(input_create_folder) + while [ -z "$selected_folder" ]; do + selected_folder=$(input_create_folder) + done - mkdir --parents "$selected_folder" - else - selected_folder=${existing_folders[$selected_option]} - fi + mkdir --parents "$selected_folder" + else + selected_folder=${existing_folders[$selected_option]} + fi - echo "$selected_folder" + echo "$selected_folder" } if [ -z "$selected_folder" ]; then - printf "\n" - echo "[INFO] Folders:" - for ((i = 0; i < ${#existing_folders[@]}; i++)); do - foldername=${existing_folders[$i]} - echo " $i) $foldername" - done + printf "\n" + echo "[INFO] Folders:" + for ((i = 0; i < ${#existing_folders[@]}; i++)); do + foldername=${existing_folders[$i]} + echo " $i) $foldername" + done - echo " n) (create a new folder)" + echo " n) (create a new folder)" - printf "\n" + printf "\n" - selected_folder=`input_select_folder` - while [ -z "$selected_folder" ]; do - selected_folder=`input_select_folder` - done + selected_folder=$(input_select_folder) + while [ -z "$selected_folder" ]; do + selected_folder=$(input_select_folder) + done fi cd $selected_folder @@ -154,11 +160,11 @@ function confirm() { while true; do read -n 1 yn case $yn in - [Yy]* ) return 0;; - [Nn]* ) return 1;; - [Uu]* ) return 2;; - [Cc]* ) exit;; - * ) echo "Please answer YES, NO, or CANCEL.";; + [Yy]*) return 0 ;; + [Nn]*) return 1 ;; + [Uu]*) return 2 ;; + [Cc]*) exit ;; + *) echo "Please answer YES, NO, or CANCEL." ;; esac done } @@ -170,14 +176,14 @@ escape_string() { # Iterate over each character in the input string while IFS= read -r -n1 char; do case "$char" in - ' '|'\'|'$'|'`'|'!'|'&'|'|'|';'|'<'|'>'|'"'|"'"|'*'|'?'|'['|']'|'{'|'}'|'('|')'|'#') - escaped="${escaped}\\${char}" - ;; - *) - escaped="${escaped}${char}" - ;; + ' ' | '\' | '$' | '`' | '!' | '&' | '|' | ';' | '<' | '>' | '"' | "'" | '*' | '?' | '[' | ']' | '{' | '}' | '(' | ')' | '#') + escaped="${escaped}\\${char}" + ;; + *) + escaped="${escaped}${char}" + ;; esac - done <<< "$input" + done <<<"$input" echo "$escaped" } @@ -200,12 +206,12 @@ if $filter_mode; then files_to_delete=() files_count=${#files[@]} - for ((i = 0; i < files_count;)); do + for ((i = 0; i < files_count; )); do file="${files[$i]}" file="${file#./}" echo "" - echo "($((i+1))/${files_count}) File: $file" + echo "($((i + 1))/${files_count}) File: $file" echo -n "Should this file be deleted? (y/n): " confirm @@ -213,26 +219,25 @@ if $filter_mode; then should_undo=false case $confirm_result in - 0) - if [[ ! ${files_to_delete[@]} =~ $file ]]; then - files_to_delete+=("$file") - fi - ;; - 1) - ;; - 2) - if (( i - 1 < 0 )); then - echo "" - echo "" - echo "Can't undo because this is the first image" - continue - else - ((i--)) - xdotool search --name feh key p - files_to_delete=("${files_to_delete[@]/$file}") - continue - fi - ;; + 0) + if [[ ! ${files_to_delete[@]} =~ $file ]]; then + files_to_delete+=("$file") + fi + ;; + 1) ;; + 2) + if ((i - 1 < 0)); then + echo "" + echo "" + echo "Can't undo because this is the first image" + continue + else + ((i--)) + xdotool search --name feh key p + files_to_delete=("${files_to_delete[@]/$file/}") + continue + fi + ;; esac xdotool search --name feh key n @@ -265,10 +270,10 @@ if $filter_mode; then done else if [ -f "$furry_commission_ideas_urls_filename" ] && grep -qE "$media_url" -i "$furry_commission_ideas_urls_filename"; then - printf "\n" - echo "[INFO] This folder already has the media informed." - echo "[INFO] Exiting..." - exit 0 + printf "\n" + echo "[INFO] This folder already has the media informed." + echo "[INFO] Exiting..." + exit 0 fi if ! command_output=$($scripts_path/gdl.sh $media_url); then @@ -278,14 +283,13 @@ else media_filenames=() for ((i = 0; i < ${#command_output[@]}; i++)); do - filename=${command_output[$i]} - media_filenames+=$(basename "$filename") + filename=${command_output[$i]} + media_filenames+=$(basename "$filename") done - for ((i = 0; i < ${#media_filenames[@]}; i++)); do - media_filename=${media_filenames[$i]} + media_filename=${media_filenames[$i]} - echo "$media_filename: $media_url" >> $furry_commission_ideas_urls_filename + echo "$media_filename: $media_url" >>$furry_commission_ideas_urls_filename done fi