Create Canon DSLR CR2 image statistics (exiftool & GNU Plot) and convert ML dual ISO CR2 files painlessly for post-processing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
6.7 KiB

5 years ago
  1. #!/bin/bash
  2. # Batch convert multiple Magic Lantern dual ISO image files on Linux
  3. # Copyright (C) 2017 Pekka Helenius
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. #
  19. ###############################################
  20. # NOTE: This script REQUIRES a patched cr2hdr tool with '--dry-run' parameter support!!
  21. # Required patch file is provided in the same repository with this script
  22. #Brackets and quotation marks in variables prevent errors occuring if file paths with spaces is used.
  23. ################################################################################################
  24. # File system check
  25. # We don't allow writing to SD card.
  26. #Sed is here to remove any trailing spaces and crap like blank lines
  27. INPUT_FILESYSTEM=$(df -h "${1}" | awk -F ' ' 'FNR> 1 {print $1}' | grep -i -E "/dev/sd?|/dev/hd?" | sed '/^\s*$/d' | wc -l)
  28. if [[ "${INPUT_FILESYSTEM}" -eq 0 ]]; then #if input file (first file printed in bash) filesystem does not start with /dev/sdX
  29. kdialog --error "Image(s) are in a SD Card. Please move them your local or external storage and try again."
  30. exit
  31. fi
  32. ################################################################################################
  33. #We get the directory just from the first filename. Pwd should be easier, but bugged, so...
  34. INPUT_DIR=$(dirname "${1}")
  35. DIR_BASENAME=$(echo "${INPUT_DIR}" | rev | cut -d/ -f 1 | rev)
  36. echo "DEBUG: Input dir is $INPUT_DIR"
  37. mkdir -p "${INPUT_DIR}"/converted_dual_iso
  38. echo "DEBUG: 'converted_dual_iso' directory created"
  39. ############################################################################################
  40. ####PROGRESSBAR STUFF - BEGIN
  41. LABELTEXT='Processing RAW images...'
  42. numargs=$# # Number of all files
  43. tics=100 # Percentage tics
  44. inc=0 # Current file number
  45. mltp=1000 # Percentage multiplier for bash
  46. dbusRef=$(kdialog --title "Dual ISO (folder: ${DIR_BASENAME})" --progressbar "$LABELTEXT" $tics)
  47. qdbus $dbusRef showCancelButton true
  48. ####PROGRESSBAR STUFF - END
  49. while [[ $# -gt 0 ]] && [[ $(qdbus $dbusRef wasCancelled) == "false" ]]; do
  50. INPUT="${1}" #Input file path, full path like: /home/myhome/IMG_4021.CR2
  51. OLDFILE_CR2=$(basename "${INPUT}") #Output as IMG_4021.CR2 (for example)
  52. # Once we do this, it's very clear which kind of CR2 file we're talking about here.
  53. NEWFILE_CR2=$(basename "${INPUT}" | sed 's/\.\w*$/_dualiso.CR2/')
  54. # Converted Dual ISO file.
  55. NEWFILE_DNG=$(basename "${INPUT}" | sed 's/\.\w*$/_dualiso.DNG/')
  56. #If converted Dual ISO exists already, we skip the conversion process. This passes only if the file doesn't exist.
  57. if [[ ! -e "${INPUT_DIR}"/converted_dual_iso/"${NEWFILE_DNG}" ]]; then
  58. if [[ $(cr2hdr --dry-run "${INPUT}" | grep "Interlaced ISO detected" | wc -l) == 1 ]]; then # Test an input file for Dual ISO.
  59. echo "Interlaced ISO detected: ${OLDFILE_CR2}"
  60. #Rename detected dual ISO CR2 file with a proper prefix (so that we can distinguish Dual ISO images from "normal" CR2 files)
  61. mv "${INPUT_DIR}"/"${OLDFILE_CR2}" "${INPUT_DIR}"/"${NEWFILE_CR2}"
  62. #Input we will use from this point is the renamed file, so we set INPUT variable to point to the renamed file.
  63. INPUT="${INPUT_DIR}"/"${NEWFILE_CR2}"
  64. cr2hdr --process "${INPUT}" # Process a valid input file.
  65. mv "${INPUT_DIR}"/"${NEWFILE_DNG}" "${INPUT_DIR}"/converted_dual_iso/ # Move converted Dual ISO.
  66. # Add Subject=Dual-ISO tag for every Dual ISO CR2 file.
  67. echo "Writing new EXIF/XMP tag Subject: Dual ISO CR2"
  68. exiftool -xmp:subject='Dual ISO CR2' "${INPUT_DIR}"/"${NEWFILE_CR2}" -overwrite_original
  69. fi
  70. fi
  71. ##############################################
  72. ####PROGRESSBAR STUFF - BEGIN
  73. let inc++
  74. #Percentage needs to be calculated like this due to bash rounding limitations...
  75. PERCENT_VALUE=$((($mltp*$tics)/(200*$numargs/$inc % 2 + $mltp*$numargs/$inc)))
  76. #Output: 20, 40, 59, 80, 100 etc.
  77. qdbus $dbusRef Set "" "value" $PERCENT_VALUE;
  78. qdbus $dbusRef setLabelText "$LABELTEXT ($inc/$numargs)";
  79. ####PROGRESSBAR STUFF - END
  80. shift #Process next CR2/DNG file...
  81. done
  82. ##############################################
  83. #Close processing window if cancelled event has been triggered.
  84. ####PROGRESSBAR STUFF - BEGIN
  85. # If the process was cancelled, remove tmp file and exit the script.
  86. if [[ ! $(qdbus $dbusRef wasCancelled) == "false" ]]; then
  87. exit
  88. fi
  89. ##############################################
  90. #Close processing window if not cancelled and processing finished.
  91. qdbus $dbusRef close
  92. ####PROGRESSBAR STUFF - END
  93. ##############################################
  94. if [ $(pgrep -x 'cr2hdr' | wc -l) == 0 ]; then
  95. notify-send 'Dual ISO' -i image-x-krita 'Conversion done!'
  96. fi
  97. ############################################################################################
  98. QUESTCOUNT=0 #Ask this question only once
  99. #Dual ISO (Subject only defined in converted Dual ISO DNG images)
  100. for i in $(find "${INPUT_DIR}" -maxdepth 1 -type f -iname "*.DNG"); do
  101. if [[ ! -z $(echo -n $i) ]] && [[ $(exiftool $i |grep --max-count=1 "Subject" | sed -e 's/.*: //g') == *"Dual-ISO"* ]] ; then
  102. if [[ $QUESTCOUNT == 0 ]]; then
  103. QUESTION=$(kdialog --yesno "More Dual ISO files detected in '$(echo ${INPUT_DIR} | rev | cut -d/ -f1 | rev)' main folder. Do you want to move these files into 'converted_dual_iso' folder?";)
  104. echo $QUESTION
  105. MOVEALL=true
  106. let QUESTCOUNT++
  107. else
  108. MOVEALL=false
  109. fi
  110. if [[ $MOVEALL == true ]]; then
  111. mv $i "${INPUT_DIR}"/converted_dual_iso/
  112. echo "DEBUG: all detected Dual ISO images moved to 'converted_dual_iso' folder"
  113. fi
  114. fi
  115. done
  116. ############################################################################################
  117. #If there are no files converted, we delete converted_dual_iso folder
  118. if [[ $(ls "${INPUT_DIR}"/converted_dual_iso | wc -l) == 0 ]]; then
  119. rm -Rf "${INPUT_DIR}"/converted_dual_iso
  120. echo "DEBUG: 'converted_dual_iso' empty, so deleted"
  121. fi
  122. exit