optimize_data.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/bash
  2. #
  3. # (C) 2013 Lauri Kasanen, under the GPLv3
  4. # (C) 2014-2015 Odd0002, under the GPLv3
  5. #
  6. # Script to optimize the data, currently PNG, JPEG, B3DZ.
  7. # experimental B3D to B3DZ compression added, not enabled by default
  8. # Run it before making a release, and after adding new data.
  9. #Takes 30+ minutes, depending on your cpu or compression options.
  10. # Spaces in filenames are supported.
  11. #define number of threads
  12. threads=$(nproc)
  13. # Start checks
  14. #if you do not want to use a program, set the variable for it to false here
  15. jpegtran=true
  16. advdef=true
  17. advzip=true
  18. optipng=true
  19. convert=true
  20. #WARNING! SETTING TO TRUE MAY POSSIBLY INCREASE LOAD TIMES ON A SLOW CPU (UNTESTED) OR LEAD TO FILE NOT FOUND ERRORS WHEN RUNNING SUPERTUXKART!
  21. compress_b3d=false
  22. #---------------------------------------------------------
  23. # Begin main code
  24. #Check for reqired programs; if a program is not available, disable it or quit the program
  25. #TODO: make awk optional
  26. if [ ! $(which awk) ]
  27. then
  28. echo "Please install awk. It is required by this program. \nQuitting..."
  29. exit 1
  30. fi
  31. #check for jpegtran
  32. if [ ! $(which jpegtran) ]
  33. then
  34. echo "jpegtran not installed, therefore it will not be used. It is included in the package \"libjpeg-progs\"."; jpegtran=false
  35. sleep 2
  36. fi
  37. #check for advdef
  38. if [ ! $(which advdef) ]
  39. then
  40. echo "advdef is not installed, therefore it will not be used. It is included in the package \"advancecomp\"."; advdef=false
  41. sleep 2
  42. fi
  43. #check for advzip
  44. if [ ! $(which advzip) ]
  45. then
  46. echo "advzip is not installed, therefore it will not be used. It is included in the package \"advancecomp\"."; advzip=false
  47. sleep 2
  48. fi
  49. #check for convert
  50. if [ ! $(which convert) ]
  51. then
  52. echo "convert is not installed, therefore it will not be used."; convert=false
  53. sleep 2
  54. fi
  55. #check for optipng
  56. if [ ! $(which optipng) ]
  57. then
  58. echo "optipng is not installed, therefore it will not be used."; optipng=false
  59. sleep 2
  60. fi
  61. # Defines
  62. #Internal Field Seperator
  63. IFS="
  64. "
  65. export LANG=C
  66. # Go
  67. #store disk usage of files beforehand
  68. BEFORE=`du -sk | awk '{print $1}'`
  69. #functions for xargs multithreading, used instead of GNU parallel for cross-compatibility
  70. #TODO: let next set of optimization scripts run if one set is stuck on a single file at the end to decrease total runtime
  71. #strip ICC information off PNG's
  72. strippng () {
  73. for arg; do
  74. convert "$arg" -strip "$arg"
  75. done
  76. }
  77. export -f strippng
  78. #optimize PNG's
  79. optimpng () {
  80. for arg; do
  81. optipng -quiet -o3 "$arg"
  82. #level 3 = 16 trials, which according to http://optipng.sourceforge.net/pngtech/optipng.html (retrieved October 2014) should be satisfactory for all users
  83. done
  84. }
  85. export -f optimpng
  86. #compress PNG in-stream data
  87. comprpng () {
  88. for arg; do
  89. advdef -z4 "$arg"
  90. done
  91. }
  92. export -f comprpng
  93. #optimize and recompress jpeg files (losslessly)
  94. optimjpg () {
  95. for arg; do
  96. jpegtran -optimize -copy none -outfile "$arg".$$ "$arg"
  97. mv "$arg".$$ "$arg"
  98. done
  99. }
  100. export -f optimjpg
  101. #recompress b3dz files
  102. recomprb3dz () {
  103. for arg; do
  104. advzip -z4 "$arg"
  105. done
  106. }
  107. export -f recomprb3dz
  108. #END MULTITHREADING FUNCTIONS
  109. #strip png icc information
  110. if [ "$convert" = true ]; then
  111. find . -path .svn -prune -o -name "*.png" -print0 | xargs -0 -n 1 -P "$threads" bash -c 'strippng "$@"' -- #multithread the png stripping
  112. else echo "convert not installed. Ignoring commands using convert..."; sleep 1
  113. fi
  114. #lossless png image optimization
  115. if [ "$optipng" = true ]; then
  116. find . -path .svn -prune -o -name "*.png" -print0 | xargs -0 -n 1 -P "$threads" bash -c 'optimpng "$@"' -- #multithread the png optimization
  117. else echo "optipng not installed. Ignoring commands using optipng..."; sleep 1
  118. fi
  119. #in-stream data/png compression
  120. if [ "$advdef" = true ]; then
  121. find . -path .svn -prune -o -name "*.png" -print0 | xargs -0 -n 1 -P "$threads" bash -c 'comprpng "$@"' -- #multithread image compression
  122. else echo "advdef is not installed. Ignoring commands using advdef..."; sleep 1
  123. fi
  124. #lossless jpeg optimization/recompression
  125. if [ "$jpegtran" = true ]; then
  126. find . -path .svn -prune -o -name "*.jpg" -print0 | xargs -0 -n 1 -P "$threads" bash -c 'optimjpg "$@"' -- #multithread jpg compression and optimization
  127. else echo "jpegtran not installed. Ignoring commands using jpegtran..."; sleep 1
  128. fi
  129. #b3dz to b3dz compression
  130. #WARNING: BETA, MAY CAUSE MISSING FILE WARNINGS!
  131. if [ "$compress_b3d" = true ]; then
  132. for xmls in $(find . -name "*.xml"); do
  133. sed 's/b3d/b3dz/g' "$xmls" > "$xmls".$$
  134. mv "$xmls".$$ "$xmls"
  135. #echo "$xmls"
  136. sed 's/b3dzz/b3dz/g' "$xmls" > "$xmls".$$
  137. mv "$xmls".$$ "$xmls"
  138. sed 's/b3dzz/b3dz/g' "$xmls" > "$xmls".$$
  139. mv "$xmls".$$ "$xmls"
  140. done
  141. find . -name "*.b3d" -execdir zip '{}.zip' '{}' \;
  142. for b3dzip in $(find -name "*.b3d.zip"); do
  143. b3dz="${b3dzip%.zip}"
  144. #echo "$b3dz"
  145. mv "$b3dzip" "${b3dz}z"
  146. done
  147. find . -type d -name "models" -prune -o -name "*.b3d" -print0 | xargs -0 rm
  148. else echo "b3d to b3dz compression disabled. Ignoring actions..."; sleep 1
  149. fi
  150. #b3dz file stream compression
  151. if [ "$advzip" = true ]; then
  152. find . -path .svn -prune -o -name "*.b3dz" -print0 | xargs -0 -n 1 -P "$threads" bash -c 'recomprb3dz "$@"' -- #multithread b3dz recompression
  153. else echo "advzip not installed. Ignoring commands using advzip..."; sleep 1
  154. fi
  155. # Add optimizations for other types if necessary
  156. # get and store new disk usage info
  157. AFTER=`du -sk | awk '{print $1}'`
  158. # Print stats out
  159. echo $BEFORE $AFTER | awk '{print "Size before: " $1/1024 "mb; Size after: " $2/1024 "mb" }'
  160. echo $BEFORE $AFTER | awk '{print "Saved " (1-($2/$1)) * 100 "%" }'