Skip to content

Commit 8248ad4

Browse files
committed
extra fixes and cleanup
1 parent fd9427d commit 8248ad4

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

image_stacker.sh

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/bin/bash
22

33
# requires ffmpeg, ffprobe, imagemagick's convert and vidstab python module , python3
4+
# credit to https://inphotos.org/2015/11/28/more-fun-with-long-exposure-stacking-of-photos/
5+
46

57
# print usage if no params
68
if [ -z $1 ]; then
7-
echo "usage: $0 -f <video file> -F <convert evaluate filter > -r <factor to reduce fps captures> -s <start mm:sec.fraction> -S <stabilize video first 0/1>"
9+
echo "usage: $0 -f <video file> -F <convert evaluate filter > -r <factor to reduce fps captures> -s <start mm:sec.fraction> -S <stabilize video first 0/1> -d <duration of video to capture>"
810
echo "for a list of all convert filters, run convert -list evaluate "
911
exit
1012
fi
@@ -16,19 +18,24 @@ for i in ffmpeg ffprobe convert; do
1618
exit;
1719
fi
1820
done
19-
21+
pid=$$
2022
until [[ -z $1 ]]; do
2123
case $1 in
22-
-f) filename="$2"; shift ;; # quote this in case video contains spaces
23-
-F) filter=$2; shift ;;
24-
-r) reduce_factor=$2; shift ;;
25-
-s) start_time=$2; shift ;;
26-
-S) stabilize=$2; shift ;;
24+
-f) filename="$2"; shift ;; # quote this in case video contains spaces
25+
-F) filter=$2; shift ;;
26+
-r) reduce_factor=$2; shift ;;
27+
-s) start_time=$2; shift ;;
28+
-d) duration_secs=$2; shift ;;
29+
-S) stabilize=$2; shift ;;
2730
esac
2831
shift
2932
done
3033

3134
# need to check the basic requires params of $filename and $filter are all there
35+
if [ -z $filename ] || [ -z $filter ]; then
36+
echo "At least a filename and filter are required"
37+
exit;
38+
fi
3239

3340
newvid=new.mp4
3441
if [ -z $stabilize ] || [ $stabilize == 0 ]; then
@@ -51,11 +58,32 @@ else
5158
python3 -m vidstab -i "$filename" -o $newvid
5259
fi
5360

61+
# optional -s option to start slicing at a certain point in the video - e.g. instable video for the first 10 seconds, pass 11 to start slicing at 11 seconds in.
62+
if [ -z $start_time ]; then
63+
start_time="-ss 0"
64+
else
65+
start_time="-ss $start_time"
66+
fi
67+
68+
# duration - need to
69+
if [ ! -z $duration_secs ]; then
70+
duration_secs="-t $duration_secs"
71+
fi
72+
73+
74+
# if we get start_time and duration_secs, run those through ffmpeg to create a new video for proper calculations below
75+
if [ "$start_time" != "-ss 0" ] || [ "$duration_secs" != "" ]; then
76+
mv $newvid tmp_$newvid
77+
echo "Generating new video based on start time of $start_time and duration : $duration_secs"
78+
ffmpeg -loglevel quiet $start_time $duration_secs -i tmp_$newvid -q:v 1 $newvid
79+
fi
80+
81+
# main image processing
5482
TMPDIR="tmp_$$"
5583

5684
if [ -e $TMPDIR ]; then
57-
rm -rf $TMPDIR
58-
mkdir $TMPDIR
85+
rm -rf $TMPDIR
86+
mkdir $TMPDIR
5987
else
6088
mkdir $TMPDIR
6189
fi
@@ -70,37 +98,35 @@ fps_string=$(grep Stream $TMPDIR/ff_output);
7098

7199
# probably won't have a hour or minute long video for this, so we just strip out the last digits
72100
duration=$(echo $duration_string| sed 's/.*Duration.*[0-9][0-9]:[0-9][0-9]:\([0-9][0-9]\.[0-9][0-9]\),.*/\1/');
73-
fps=$(echo $fps_string |sed 's/.*\([0-9][0-9]\) fps.*/\1/');
101+
fps=$(echo $fps_string |sed 's/.*, \(.*\) fps.*/\1/');
102+
fps=$(echo "scale=0; $fps/1"|bc); # drop the decimal if there is one
74103

75104
# optional $reduce_factor - to decrease the number of slices - e.g. for 30 fps and 2 $reduce_factor, will capture 15 slices per second
76105
if [ ! -z $reduce_factor ]; then
77-
images_per_sec=$(echo "$fps / $reduce_factor" | bc);
78-
else
79-
images_per_sec=$fps
80-
fi
81-
82-
# optional -s option to start slicing at a certain point in the video - e.g. instable video for the first 10 seconds, pass 11 to start slicing at 11 seconds in.
83-
if [ -z $start_time ]; then
84-
start_time="-ss 0"
106+
images_per_sec=$(echo "$fps / $reduce_factor" | bc);
85107
else
86-
start_time="-ss $start_time"
108+
images_per_sec=$fps
87109
fi
88110

89111
frames=$(echo "scale=2;$duration * $images_per_sec" | bc);
90112
echo "Video is $duration seconds long at $fps fps - - will capture $images_per_sec images per second - which should generate $frames images - start at $start_time"
91113
# note that the -r $fps option placement is important. If places BEFORE the -i it affects the input file rate
92-
ffmpeg $start_time -loglevel quiet -stats -hide_banner -i "$newvid" -q:v 1 -r $images_per_sec $TMPDIR/slice_%04d.png
114+
ffmpeg -loglevel quiet -stats -hide_banner -i "$newvid" -q:v 1 -r $images_per_sec $TMPDIR/slice_%04d.png
93115

94116
# cd into $TMPDIR so all the output is generated in the $TMPDIR
95117
cd $TMPDIR
96118

97-
newname="${filename%.*}_stacked.png"
119+
newname="${filename%.*}_stacked_$filter_$pid.png"
98120
#nice convert -monitor -flatten slice* -evaluate-sequence $i output_$i.png
99-
time convert slice* -evaluate-sequence $filter ../$newname
100-
exit;
121+
if [ $filter == "average" ]; then
122+
time convert slice* -average ../$newname
123+
else
124+
time convert slice* -evaluate-sequence $filter ../$newname
125+
fi
101126
if [ "$?" == "0" ]; then
102127
cd ..
103128
rm -r $TMPDIR
104129
rm $newvid
105130
fi
106131
ls -la $newname
132+
feh $newname

0 commit comments

Comments
 (0)