Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # 20190822 | tatze
- # ------------------------------------------
- global_path=~/Videos/_reduced/
- local_path='reduced/'
- # ------------------------------------------
- # create directory if it's non-existent
- function dir {
- if [ ! -d "$1" ]
- then
- printf "${bold}creating folder $1 ${normal}\n"
- mkdir -p -- "$1"
- if [ ! -d "$1" ]
- then
- printf "${bold}creating folder $1 FAILED, aborting script.${normal}\n"
- exit 1
- fi
- fi
- }
- # terminal output: bold or normal?
- bold=$(tput bold)
- normal=$(tput sgr0)
- # your choice ...
- printf "\n${bold}Where shall the reduced images be created? ${normal}
- 1. locally ($local_path) ${bold}[default]${normal}
- 2. globaly ($global_path)\n${bold}
- [?] : ${normal}"
- read RP
- case $RP in
- 2)
- printf "using $global_path ${normal}\n\n"
- path=$global_path
- pth=$(basename $PWD)/
- path+=$pth
- ;;
- *)
- printf "using $local_path ${normal}\n\n"
- path=$local_path
- ;;
- esac
- dir "$path"
- find . -iregex '.*\.mp4$' -not -iregex '.*_reduced\.mp4$' -not -path "$path/*" -print0 | sort -z |
- while IFS= read -rd '' file; do
- # path for reduced files
- rd=$path${file%/*}
- dir "$rd"
- # get basename
- bn=${file##*/}
- # create filename for reduced version
- bn="${bn%.*}_reduced.${bn##*.}"
- # create filename for temp version
- tmp="${bn%.*}_temp.mp4"
- # check if reduced file already exists
- if [ ! -f "$rd/$bn" ]
- # nope, go on
- then
- printf "compressing video $file ... "
- # prepending < /dev/null to prevent characters bleeding to ffmpeg
- # using temporary file to avoid faulty files when escaping the loop (next run the script will re-do $file then)
- < /dev/null ffmpeg -y -i "$file" \
- -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k \
- -movflags +faststart -vf scale=-2:720,format=yuv420p \
- -v 0 \
- "$rd/$tmp"
- printf "done.\ncopying metadata ..."
- # copy metadata to the reduced version
- < /dev/null ffmpeg -i "$file" \
- -i "$rd/$tmp" -map 1 -map_metadata 0 \
- -v 0 \
- -c copy "$rd/temp_for_metadata".mp4
- # move the reduced file to its final version
- mv "$rd/temp_for_metadata.mp4" "$rd/$bn"
- # remove the temporary file
- rm "$rd/$tmp"
- # preserve modified timestamp
- touch -r "$file" "$rd/$bn"
- printf "done.\nhere it is: $rd/$bn\n\n"
- # file exists, skip it
- else
- printf "$rd/$bn already exists, skipping.\n"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement