-
Notifications
You must be signed in to change notification settings - Fork 2
/
heic_sips_tif.sh
160 lines (125 loc) · 3.37 KB
/
heic_sips_tif.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/zsh
#set -x
#setopt interactivecomments
setopt +o nomatch
unsetopt CASE_GLOB #case insensitive *.
echoo () {
echo $'\e[0;103m' $1 $'\e[0m'
}
start=`date +%s`;
cd $1;
remove=0
subfolders=0
path_wildcard=""
output_folder="_heic_conversion_tmp"
files_not_converted=()
if [[ "$2" = "remove" || "$3" = "remove" ]];
then
remove=1
fi
if [[ "$2" = "subfolders" || "$3" = "subfolders" ]];
then
subfolders=1
path_wildcard="**/*/"
fi
exts=("tif")
echoo "\tCONVERTING FILES WITH EXTENSIONS: $exts";
if [[ $subfolders -eq 1 ]];
then
echoo "\tSUBFOLDERS INCLUDED";
fi
if [[ $remove -eq 1 ]];
then
echoo "\tORIGINAL FILES WILL BE REMOVED AFTER CONVERSION";
fi
glob_total=0;
glob_success=0;
glob_deleted=0;
mkdir $output_folder;
for ext in $exts; do
echoo "\t";
echoo "\tPROCESSING $ext";
total_ext=0;
success_ext=0;
#echo $~path_wildcard;
for dir in $~path_wildcard ./; do
success_dir=0
if [[ "$dir" = "$output_folder/" ]]; #ignore output folder
then
continue
fi
src_files=($dir*.$ext);
if [[ "${src_files[@]:0:1}" = "$dir*.$ext" ]] #no files in directory
then
continue
fi
total_dir=${#src_files[@]};
((total_ext=total_ext+total_dir))
echoo "\t"
echoo "\tCURRENT DIR: $dir, TOTAL FILES: $total_dir, RUNNING SIPS..."
echo ""
sips -s format heic $src_files --out $output_folder
echo ""
dst_files=($output_folder/*.heic)
#echoo \tDst files: $dst_files
#echoo "${dst_files[@]:0:1}"
#echoo "$output_folder/*.heic"
if [[ "${dst_files[@]:0:1}" != "$output_folder/*.heic" ]] #there're files in directory
then
success_dir=${#dst_files[@]};
((success_ext=success_ext+success_dir));
#
# MOVE CONVERTED FILES
#
echoo "\tCOPYING CONVERTED FILES.."
for dst_file in $dst_files; do
dst=$(basename -- "$dst_file")
#echoo $dst
trg_file=$dst
until
[[ ! -f $dir/$trg_file ]]
do
trg_file="$(echo "$dst" | sed -e 's/\.[^.]*$//')-dup.heic"
done
#echoo $trg_file
#echoo $output_folder
#echoo $dir
mv $output_folder/$dst $dir/$trg_file
done
#
# DELETE ORIGINALS
#
if [[ $remove -eq 1 ]]; then
echoo "\tDELETING ORIGINALS.."
for dst_file in $dst_files; do
orig_file=$(basename -- "$(echo "$dst_file" | sed -e 's/\.[^.]*$//').$ext");
#echoo $dst_file
#echoo $orig_file
file_exists=0
if [[ -f $dir/$orig_file ]]; then
file_exists=1
fi;
#echoo "\tOriginal file $orig_file ---- exists $file_exists"
if [[ $file_exists -eq 1 ]]; then
rm $dir/$orig_file;
if [[ $? -eq 0 ]]; then
((glob_deleted++))
fi
fi
done;
fi
fi
echoo "\tTOTAL FILES SUCCESSFULY CONVERTED: $success_dir";
#mv $dst_files $dir;
#dst="$(echo "$src" | sed -e 's/\.[^.]*$//').heic"; # remove original extension and replace it with heic
done;
((glob_total=total_ext+glob_total));
((glob_success=success_ext+glob_success));
echoo "\tFILES PROCESSED SO FAR: $glob_total, SUCCESSFULY: $glob_success, ORIGINALS DELETED: $glob_deleted";
echoo "\t";
done;
rm -rf $output_folder;
#echoo "\tFILES NOT CONVERTED: ${#files_not_converted[@]}"
#echoo "$files_not_converted"
end=`date +%s`;
echoo "\tTIME: `expr $end - $start` seconds.";