-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdftomp4
executable file
·533 lines (450 loc) · 12.3 KB
/
pdftomp4
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/bin/bash
start=1 # start page (from)
end=1000 # end page (to)
win_height=400 # the height of sliding window (view port)
stride=32 # the number of pixels to slide
cut_left=100 # left margin to crop
cut_top=100 # top margin to crop
cols=0 # number of paper columns
direction="ltr"
default_delay=0.16
start_point=0
remove="none"
del_all="no"
negate=""
trim="none"
scale_h=100
cuts=""
dups="none"
linger=1
linger_frame=20
top_trim="auto"
skip=0
frame_rate=24
crf=17
split=500
from=0
last_page=0
base_folder="date"
update_rate=0
mode="1x"
support_lines="yes"
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare $param="$2"
# echo $1 $2 // Optional to see the parameter:value result
fi
shift
done
if [ ! -f "$input" ]; then
echo "$input doesn't exist"
echo "Please provide the input file using --input option"
exit 1
fi
if [ -z ${reset+x} ]; then
echo "reset is unset";
else
remove="all"
echo "reset is set to '$reset'";
fi
if [ -z ${delay+x} ]; then
delay=$default_delay
fi
start_page=$start
end_page=$end
echo "input = $input, remove = $remove, start=$start_page end = $end_page"
#echo "./conv.sh " $@ >> "$input".sh
cut_pages=()
cut_locs=()
for elem in $cuts; do
arr=(${elem//:/ })
p=${arr[0]}
loc=${arr[1]}
cut_pages=( "${cut_pages[@]}" "$p" )
cut_locs=( "${cut_locs[@]}" "$loc" )
done
function get_index() {
value="$1"
shift
local index=0
for i in "$@"; do
if [[ "$i" = "${value}" ]]; then
echo "${index}";
return
fi
((index++))
done
return 1
}
fullpath=$input
filename="${fullpath##*/}" # Strip longest match of */ from start
dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
base="${filename%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
ext="${filename:${#base} + 1}" # Substring from len of base thru end
pages_dir="$dir""$base"_pages
echo "directory is $pages_dir"
if [[ $remove = "pages" ]] || [[ $remove = "all" ]]; then
echo "Removing $pages_dir"
rm -r "$pages_dir"
fi
echo "delay:$delay win_hegith:$win_height stride:$stride"
dt=$(date '+%d-%m %H');
if [[ ! -d "$pages_dir" ]]; then
echo "Generating pages.. "
mkdir "$pages_dir"
if [[ $end -ne 1000 ]]; then
pdftoppm "$fullpath" -f $start -l $end -png "$pages_dir"/page
else
pdftoppm "$fullpath" -png "$pages_dir"/page
fi
if [[ $trim != "none" ]]; then
for img in "$pages_dir"/*.png; do
convert "$img" "$img"
done
fi
image_files=( "$pages_dir"/*.png )
IFS=$'\n' image_files=($(sort -V <<<"${image_files[*]}"))
unset IFS
all_images_num=${#image_files[@]}
current="${image_files[0]}"
page_width=$(identify -format "%w" $current)
page_height=$(identify -format "%h" $current)
pad_x=$((44-(page_width%4)))
echo "page_width:$page_width pad_x:$pad_x "
page_width=$((page_width + pad_x))
middle=$((page_width /2))
trim=0
if [[ $cols -eq 0 ]]; then
convert "$current" -crop +0+600 +repage -alpha off -colorspace gray -negate -scale x1\! -threshold 2% txt: > "$pages_dir"/col_detect.txt
filled_seen=0
x_begin=0
x_end=0
counter=0
while IFS= read -r line; do
line_arr=($line)
pos=${line_arr[0]}
color=${line_arr[3]}
if [[ $color = "white" ]] || [[ $color = "gray(255)" ]] ; then # filled space
pos_arr=(${pos//,/ })
xpos=${pos_arr[0]}
if [[ $x_begin -gt 0 ]]; then
x_end=$xpos
echo "x_end: $x_end"
break;
else
if [[ $trim = 0 ]]; then
trim=$((xpos - 20))
echo "Trim 1: $trim"
fi
fi
filled_seen=1
counter=0
else # empty space
if [[ $filled_seen -eq 1 ]] && [[ $x_begin -eq 0 ]]; then
pos_arr=(${pos//,/ })
xpos=${pos_arr[0]}
counter=$((counter + 1))
if [[ $counter -gt 10 ]]; then
if [[ $xpos -gt $((page_width / 3)) ]] && [[ $xpos -lt $((2*(page_width / 3))) ]]; then
x_begin=$xpos
echo "x_begin: $x_begin"
else
if [[ $xpos -lt $((page_width / 3)) ]]; then
trim=0
echo "Trim 2: $trim, xpos:$xpos"
fi
fi
fi
fi
fi
done < "$pages_dir"/col_detect.txt
if [[ $x_end -gt 0 ]]; then
middle=$(((x_begin + x_end) / 2))
middle=$((middle + (pad_x/2)))
cols=2
echo "Two columns detected ... middle: $middle"
fi
fi
if [[ $cols != 2 ]]; then
scale_h=200
else
# if [[ $negate = "" ]]; then
# negate="all"
# fi
convert "$current" -alpha off -colorspace gray -negate -scale 1x\! -threshold 2% txt: > "$pages_dir"/vert.txt
filled_seen=0
counter=0
v_cut=0
while IFS= read -r line; do
line_arr=($line)
pos=${line_arr[0]}
color=${line_arr[3]}
pos_arr=(${pos//,/ })
ypos=${pos_arr[1]}
ypos=${ypos%?}
if [[ $color = "white" ]] || [[ $color = "gray(255)" ]] ; then # filled space
filled_seen=1
counter=0
else # empty space
if [[ $filled_seen -eq 1 ]]; then #empty space after filled space
counter=$((counter + 1))
if [[ $counter -gt 30 ]]; then
if [[ $ypos -lt $((page_height / 3)) ]]; then
v_cut=$((ypos - 10))
fi
fi
fi
fi
done < "$pages_dir"/vert.txt
if [[ $v_cut -gt 0 ]]; then
echo "V_cut is $v_cut "
p=1
loc=$v_cut
cut_pages=( "${cut_pages[@]}" "$p" )
cut_locs=( "${cut_locs[@]}" "$loc" )
fi
fi
ff="%02d"
if [[ $all_images_num -gt 99 ]]; then
ff="%03d"
fi
do_negate=""
bgcolor="white"
if [[ $negate = "all" ]]; then
echo "Nagation is for all pages"
do_negate="-negate"
bgcolor="black"
fi
do_trim=""
if [[ $top_trim = "auto" ]]; then
do_trim="-trim +repage -bordercolor $bgcolor -border 10x20"
fi
shave=""
win_half=$((winh / 2))
if [[ $trim != "none" ]]; then
shave="$trim"x0
shave="-shave $shave +repage"
echo "Shaving at $trim"
fi
for ((ii=0; ii<${#image_files[@]}; ii++)); do
img=${image_files[$ii]}
iii=$((ii+1))
pnum=$(printf $ff $iii)
convert "$img" $shave $do_negate -adaptive-resize "$page_width"x +repage "$img"
if [[ ii -eq 0 ]]; then
# convert "$img" -pointsize 24 -fill white \
# -undercolor '#0004' -gravity "North" \
# -annotate +10+20 "$dt | $all_images_num | $stride" \
# "$img"
echo "first page"
convert \
-background '#0004' \
-gravity Center \
-fill white \
-size 500x100 \
caption:"$dt | $all_images_num | $stride" \
"${img}" \
+swap \
-gravity North \
-composite \
"${img}"
fi
if [[ $support_lines = "yes" ]]; then
convert "$img" \
\( -size 256x250 xc:"#0502" -size 256x20 xc:none -append -write mpr:tile +delete \) \
\( -clone 0 -tile mpr:tile -draw "color 0,0 reset" \) \
-compose over -composite \
"$img"
fi
num=${pnum#0}
if [[ $cols -eq 2 ]]; then
cut=0
if page_index=$(get_index $num "${cut_pages[@]}"); then
echo "Cutting page $pnum ..."
cut="${cut_locs[$page_index]}"
fi
if [[ $cut -gt 0 ]]; then
convert "$img" -crop x"$cut"+0+0 +repage $do_trim -adaptive-resize "$page_width"x "$pages_dir"/page-"$pnum"-cut-0.png
fi
safe_margin=0
m1=$((middle + $safe_margin))
m2=$((middle - $safe_margin))
col1="$m1"x"0"+0+"$cut"
col2="$m1"x"0"+"$m2"+"$cut"
if [[ $direction = "rtl" ]]; then
col1="$middle"x"0"+"$middle"+"$cut"
col2="$middle"x"0"+0+"$cut"
fi
convert "$img" -crop $col1 +repage $do_trim -background black -adaptive-resize "$page_width"x "$pages_dir"/page-"$pnum"-cut-1.png
convert "$img" -crop $col2 +repage $do_trim -background black -adaptive-resize "$page_width"x "$pages_dir"/page-"$pnum"-cut-2.png
rm "$img"
else
convert "$img" $do_trim +repage -adaptive-resize "$page_width"x "$img"
fi
if [[ $dups != "none" ]]; then
if [[ $dups = "all" ]]; then
cp "$img" "$pages_dir"/page-"$pnum"-2.png
mv "$img" "$pages_dir"/page-"$pnum"-1.png
else
if [[ " ${dups[@]} " =~ " ${num} " ]]; then
echo "Duplicating page $pnum ..."
cp "$img" "$pages_dir"/page-"$pnum"-2.png
mv "$img" "$pages_dir"/page-"$pnum"-1.png
fi
fi
fi
done
fi
image_files=( "$pages_dir"/*.png )
all_images_num=${#image_files[@]}
if [[ $end_page -eq 1000 ]]; then
end_page=$all_images_num
if [[ $cols = 2 ]]; then
end_page=$((all_images_num/2))
fi
fi
factor=$((win_height/stride))
factor=5
first_delay=$(echo $delay+5 | bc)
beg=$((0 + win_height))
frames_dir="$dir""$base"_frames_"$stride"
if [[ $remove = "frames" ]] || [[ $remove = "all" ]]; then
echo "Removing $frames_dir"
rm -r "$frames_dir"
fi
shopt -s nullglob
folders=("$dir""$base"_frames_*)
echo "$dir""$base"_frames_*
exist=1
if [ ${#folders[*]} -ge 1 ]; then
frames_dir=${folders[0]}
echo "Frames ($frames_dir) aleardy exist, it adds new frames to it"
frames=($(ls -a "$frames_dir"/*.png | head -2))
echo "frames : ${frames[@]}"
first_frame=$(basename ${frames[0]})
first_frame="${first_frame%.*}"
sec_frame=$(basename ${frames[1]})
sec_frame="${sec_frame%.*}"
echo "first_frame: $first_frame, sec_frame:$sec_frame"
arr=(${first_frame//_/ })
num1=${arr[3]}
num1=$((10#$num1))
arr=(${sec_frame//_/ })
num2=${arr[3]}
num2=$((10#$num2))
dist=$((num2 - num1))
start_point=$((dist /2))
stride=$dist
delay=$((dist / 100)) | bc -l
rm "$frames_dir"/*.txt
mv "$frames_dir" "$dir""$base"_frames_"$start_point"
frames_dir="$dir""$base"_frames_"$start_point"
echo "stride: $stride, start_point: $start_point, delay:$delay"
else
exist=0
mkdir "$frames_dir"
fi
dt=$(date '+%d-%m');
echo "$dt"
page_num=-1
echo "Generating images for $start_page to $end_page"
first=-1
image_files=( "$pages_dir"/*.png )
echo "${#image_files[@]} images in $pages_dir"
page_height=0
col_num=1
frame=-1
mdelay=$delay
last_frame=""
for ((ii=0; ii<${#image_files[@]}; ii++)); do
page=${image_files[$ii]}
filename="${page##*/}"
name=$(echo "$filename" | cut -f 1 -d '.')
arr=(${name//-/ })
num=${arr[1]}
new_page=0
num=$((10#$num))
if [[ $num != ${page_num#0} ]]; then
new_page=1
frame=-1
fi
page_num=$num
echo "page number : $page_num, $start_page, $end_page"
if [[ $num -lt $start_page ]]; then
continue
fi
if [[ $num -gt $end_page ]]; then
break
fi
echo "Processing page $page_num of $end_page"
prev_page_height=$page_height
page_height=$(identify -format "%h" $page)
page_width=$(identify -format "%w" $page)
#read page_width page_height < <(identify -format "%w %h" $page)
if [[ $first -lt 0 ]] && [[ $ii -eq 0 ]]; then
cp "${image_files[$ii]}" "$base"_temp.png
current="$base"_temp.png
first=1
limit=$page_height
top=$start_point
mdelay=$first_delay
else
# echo "combining..."
t1="${image_files[$ii-1]}"
#t1="$last_frame"
t2="${image_files[$ii]}"
if [[ $negate = "even" ]]; then
if [ $((ii%2)) -eq 0 ]; then
convert -negate "$t2" t2.png
t1="${image_files[$ii-1]}"
t2="t2.png"
else
convert -negate "$t1" t1.png
t1="t1.png"
t2="${image_files[$ii]}"
fi
fi
convert "$t1" "$t2" -append "$base"_temp.png
current="$base"_temp.png
limit=$((prev_page_height + page_height))
top=$((limit-page_height-win_height+stride+start_point))
#top=$((stride+start_point))
# echo "top :$top"
fi
#echo "current image: $current top:$top"
bottom=$((top+win_height))
left=0
win_width=$page_width
pnum=$(printf "%03d" $num)
iii=$(printf "%03d" $ii)
# echo "$name"
list_file="$frames_dir"/list-"$start_point"-"$pnum"-"$iii".txt
if [[ -f $list_file ]]; then
continue
fi
# echo "limit: $limit"
while [ $bottom -lt $limit ]; do
frame=$((frame+1))
i=$((frame+1000))
ttt=$(printf "%04d" $top)
fname="page_"$pnum"_"$iii"_"$ttt".png"
# if [ -f "$frames_dir"/$fname ]; then
# top=$((top+stride))
# bottom=$((top+win_height))
# continue
# fi
crop="$win_width"x"$win_height"+"$left"+"$top"
scaledH=$((win_height+scale_h))
# -adaptive-resize x"$scaledH"\!
convert $current -crop "$crop" +repage -quality 99 -adaptive-resize x"$scaledH"\! +repage -pointsize 24 -fill white -undercolor '#56f' -gravity "NorthEast" -annotate +10+20 "$page_num" "$frames_dir"/$fname
# last_frame="$frames_dir"/$fname
top=$((top+stride))
bottom=$((top+win_height))
#echo "$bottom of $limit"
done #while
echo "done" >> $list_file
done
rm "$base"_temp.png
echo "$stride $start_point $delay" >> "$base"_"$dt".txt
./frames2mp4 --frames "$frames_dir" --start_page $start_page --end_page $end_page --bg