From 84844715a681c41dd1644718b1b8f2087ea4cc09 Mon Sep 17 00:00:00 2001 From: Chad Date: Tue, 4 Jul 2023 16:07:54 +0000 Subject: [PATCH] Compress Video Updates, Attempt To Customize Cinnamon (#42) * Using $1 does not work well with options. * Add timestamp to compressed file. Format code. * Add a wildcard catch for getopts. * Add a usage line to the usage. * Add m, v, and x options. Refactor a few areas. * Add missing options to usage. * Change verbose to be V so that video-bitrate stays v. * Set the same values as GNOME for Cinnamon DE. * Add note on what the change really did to Cinnamon. * Enhancements to multiple options. * Process the files/folders in a recognizable order. * Update comment, no longer using a hard-coded codec. --- files/scripts/compress-video.sh | 116 +++++++++++++++----- tasks/workstation/shared/settings/gnome.yml | 12 +- 2 files changed, 96 insertions(+), 32 deletions(-) diff --git a/files/scripts/compress-video.sh b/files/scripts/compress-video.sh index 31f7ae3..c2ef567 100755 --- a/files/scripts/compress-video.sh +++ b/files/scripts/compress-video.sh @@ -9,78 +9,123 @@ if [[ $DIR == '.' ]]; then fi echo "Running $DIR/$PROG" -## Functions +## Functions ## + function usage { + echo "Usage: $PROG [-i file/folder] [-v bitrate] [-a bitrate] [-c vcodec] [-r] [-f] [-m] [-V] [-x] [-h]" cat <<- EOF - Reduce the filesize of a video file to make it stream well. It also + Reduce the filesize of a video file to make it stream well. It also helps with the file size for placing the file into a backup system. - Currently only set up for libopenh264 and mp4 files. + Currently only set up for mp4 files. Parameters: -i input : The input file or folder with which to search for video files. If nothing is provided, current directory (.) is assumed. -v bitrate : The video bitrate to convert to, defaults to 2000k. -a bitrate : The audio bitrate to convert to, defaults to 128k. + -c vcodec : The video codec you'd like to use, such as libopenh264. -r : Recurse the entire directory structure, compressing all video files. -f : Force recompressing any files by deleting it if it already exists. + -m : Measure the time it takes to compress each video and do the loop. + -V : Add verbosity, such as printing all the variable values. + -x : Set the shell's x flag to display every action which is taken. -h : Display this help messaging. EOF exit $1 } -## Parse Input -while getopts ":i:v:a:rfh" opt; do +## Parameters ## + +while getopts ":i:v:a:c:rfmVxh" opt; do case $opt in i) input="$OPTARG" - echo "input='$input'" ;; v) video_bitrate="$OPTARG" - echo "video_bitrate='$video_bitrate'" ;; a) audio_bitrate="$OPTARG" - echo "audio_bitrate='$audio_bitrate'" ;; - r) recurse="Y" - search_command=find - echo "recurse='$recurse', search_command='$search_command'" + c) codec="-vcodec $OPTARG" + ;; + r) search_command="find" ;; f) force="Y" - echo "force='$force'" + ;; + m) time_command="time -p" + ;; + V) verbose="Y" + ;; + x) set_x="Y" ;; h) usage 0 ;; + *) echo "ERROR: Option '$OPTARG' not recognized." >&2 + usage 1 + ;; esac done +if [[ $set_x == "Y" ]]; then + set -x +fi + if [[ -z "$input" ]]; then - if [[ ! -z "$1" ]]; then - echo "WARNING: Program was not passed a file. Using input $1." - input=$1 - else - echo "WARNING: Program was not passed a file. Using current directory." - input='.' - fi + echo "WARNING: Program was not passed an input. Using current directory." + input="." fi if [[ -z $video_bitrate ]]; then - video_bitrate='2000k' + video_bitrate="2000k" fi if [[ -z $audio_bitrate ]]; then - audio_bitrate='128k' + audio_bitrate="128k" fi -if [[ -z $recurse ]]; then - search_command=ls +if [[ -z $codec ]]; then + codec="" fi -## Other Variables -filename_flag='compressed.' +if [[ -z $search_command ]]; then + search_command="ls" +fi -## Main Loop -$search_command $input | while read file; do +if [[ -z $time_command ]]; then + time_command="" +fi + +## Other Variables ## + +filename_flag='compressed' +date_YYYYMMDD="`date "+%Y%m%d"`" + +## Main ## + +if [[ $verbose == "Y" ]]; then + cat <<- EOF + VERBOSE: Full list of variables. + input='$input' + video_bitrate='$video_bitrate' + audio_bitrate='$audio_bitrate' + codec='$codec' + search_command='$search_command' + force='$force' + time_command='$time_command' + verbose='$verbose' + set_x='$set_x' + filename_flag='$filename_flag' + date_YYYYMMDD='$date_YYYYMMDD' + SECONDS='$SECONDS' + EOF +fi + +SECONDS=0 +$search_command $input | sort | while read file; do echo -e "\n$file" + if [[ -n $time_command ]]; then + date + fi + # Exception checks for the existing file. if [[ $file != *'.mp4' ]]; then echo "SKIP: Not an MP4." @@ -93,7 +138,7 @@ $search_command $input | while read file; do # Build the new filename to signify it is different thn the original. extension=${file##*.} - newfile=${file//$extension/$filename_flag$extension} + newfile=${file//$extension/$filename_flag-$date_YYYYMMDD.$extension} # More exception checks based on the new file. if [[ -e $newfile ]]; then @@ -108,9 +153,20 @@ $search_command $input | while read file; do # Convert the file. echo "Converting to $newfile." - ffmpeg -nostdin -hide_banner -loglevel quiet \ + $time_command bash -c "ffmpeg -nostdin -hide_banner -loglevel quiet \ -i $file -b:v $video_bitrate -b:a $audio_bitrate \ - -vcodec libopenh264 -movflags +faststart $newfile + $vcodec -movflags +faststart $newfile" done +echo "\nDone!" + +# Display elapsed time +if [[ -n $time_command ]]; then + typeset -i hours minutes seconds + hours=$(( SECONDS / 3600 )) + minutes=$(( (SECONDS % 3600) / 60 )) + seconds=$(( SECONDS % 60 )) + echo "Loop Performance: ${hours}h ${minutes}m ${seconds}s" +fi + exit 0 diff --git a/tasks/workstation/shared/settings/gnome.yml b/tasks/workstation/shared/settings/gnome.yml index 07a82df..2deb1d8 100644 --- a/tasks/workstation/shared/settings/gnome.yml +++ b/tasks/workstation/shared/settings/gnome.yml @@ -164,9 +164,9 @@ ignore_errors: yes -- name: Workstation | Account Management | GNOME | Favorites (Linux) +- name: Workstation | Account Management | GNOME + Cinnamon | Favorites (Linux) dconf: - key: /org/gnome/shell/favorite-apps + key: "{{ item }}" value: "[ 'org.gnome.Terminal.desktop', 'gnome-system-monitor.desktop' , 'org.gnome.Nautilus.desktop' , 'io.gitlab.librewolf-community.desktop', '{{ browser }}' @@ -180,6 +180,14 @@ state: present become_user: "{{ user }}" when: ansible_system == "Linux" + loop: + - /org/gnome/shell/favorite-apps + - /org/cinnamon/favorite-apps + # As of 2023-07-01 this only sets the Menu Favorites on Cinnamon, not the + # Panel Pins. Cannot find any details online of where the pinned application + # data lives. Cloned and searched the linuxmint/cinnamon project too and + # couldn't find which function handles it. Leaving the loop for it but it's + # not what was hoped for and is sort of a TBD/TODO. - name: Workstation | Account Management | GNOME | Favorites (FreeBSD) dconf: