SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # 20190822 | tatze
  3.  
  4. # ------------------------------------------
  5. global_path=~/Videos/_reduced/
  6. local_path='reduced/'
  7. # ------------------------------------------
  8.  
  9. # create directory if it's non-existent
  10. function dir {
  11.   if [ ! -d "$1" ]
  12.     then
  13.     printf "${bold}creating folder $1 ${normal}\n"
  14.     mkdir -p -- "$1"
  15.     if [ ! -d "$1" ]
  16.       then
  17.       printf "${bold}creating folder $1 FAILED, aborting script.${normal}\n"
  18.       exit 1
  19.     fi
  20.   fi
  21. }
  22.  
  23. # terminal output: bold or normal?
  24. bold=$(tput bold)
  25. normal=$(tput sgr0)
  26.  
  27. # your choice ...
  28. printf "\n${bold}Where shall the reduced images be created? ${normal}
  29. 1. locally ($local_path) ${bold}[default]${normal}
  30. 2. globaly ($global_path)\n${bold}
  31. [?] : ${normal}"
  32. read RP
  33.  
  34. case $RP in
  35.   2)
  36.     printf "using $global_path ${normal}\n\n"
  37.     path=$global_path
  38.     pth=$(basename $PWD)/
  39.     path+=$pth
  40.     ;;
  41.   *)
  42.     printf "using $local_path ${normal}\n\n"
  43.     path=$local_path
  44.     ;;
  45. esac
  46.  
  47. dir "$path"
  48.  
  49. find . -iregex '.*\.mp4$' -not -iregex '.*_reduced\.mp4$' -not -path "$path/*" -print0 | sort -z |
  50. while IFS= read -rd '' file; do
  51.   # path for reduced files
  52.   rd=$path${file%/*}
  53.   dir "$rd"
  54.   # get basename
  55.   bn=${file##*/}
  56.   # create filename for reduced version
  57.   bn="${bn%.*}_reduced.${bn##*.}"
  58.   # create filename for temp version
  59.   tmp="${bn%.*}_temp.mp4"
  60.   # check if reduced file already exists
  61.   if [ ! -f "$rd/$bn" ]
  62.   # nope, go on
  63.   then
  64.     printf "compressing video $file ... "
  65.     # prepending < /dev/null to prevent characters bleeding to ffmpeg
  66.     # using temporary file to avoid faulty files when escaping the loop (next run the script will re-do $file then)
  67.     < /dev/null ffmpeg -y -i "$file" \
  68.                        -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k \
  69.                        -movflags +faststart -vf scale=-2:720,format=yuv420p \
  70.                        -v 0 \
  71.                        "$rd/$tmp"
  72.     printf "done.\ncopying metadata ..."
  73.     # copy metadata to the reduced version
  74.     < /dev/null ffmpeg -i "$file" \
  75.                        -i "$rd/$tmp" -map 1 -map_metadata 0 \
  76.                        -v 0 \
  77.                        -c copy "$rd/temp_for_metadata".mp4
  78.     # move the reduced file to its final version
  79.     mv "$rd/temp_for_metadata.mp4" "$rd/$bn"
  80.     # remove the temporary file
  81.     rm "$rd/$tmp"
  82.     # preserve modified timestamp  
  83.     touch -r "$file" "$rd/$bn"
  84.     printf "done.\nhere it is: $rd/$bn\n\n"
  85.   # file exists, skip it
  86.   else
  87.     printf "$rd/$bn already exists, skipping.\n"
  88.   fi
  89. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top