1#!/usr/local/bin/bash
2#    gallery1-to-slideshow
3#    was gallery2slideshow
4#    Copyright 2003 Scott Dylewski  <scott at dylewski.com>
5#
6#    This program is free software; you can redistribute it and/or modify
7#    it under the terms of the GNU General Public License as published by
8#    the Free Software Foundation; either version 2 of the License, or
9#    (at your option) any later version.
10#
11#    This program is distributed in the hope that it will be useful,
12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#    GNU General Public License for more details.
15#
16#    You should have received a copy of the GNU General Public License
17#    along with this program; if not, write to the Free Software
18#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19#
20
21version='0.8.1'
22
23changes ()
24{
25echo 'Changes:
260.8.1
27	Escape colons in subtitles
280.7.5
29	No changes
300.7.4
31	Fixed bug where no pictures were found with new version of gallery 1.5
32	Fixed bug near line 279	with a missing "fi" statement.
330.7.3
34	Changed name from gallery2slideshow to gallery1-to-slideshow to avoid
35	confusion...  it does not work on gallery v.2.x albums!
36	Add -b background image option
37	Fix for finding all images in album. Thanks Ivan Espinosa!
38	Added a few more quotes around variables to help with spaces in filenames.
390.7.2	Another fix to allow newlines in gallery comments
40	These should correctly get translated into \n characters.
41	Strips out <a href=""> and </a> tags also.
420.7.0	Fix to not show hidden pictures for gallery v1.4.3-pl1
43	( thanks Frederic! )
44	Fixed bug when passing html links in subtitle field
45	dvd-slideshow supports duration in fractional seconds.
46	Allow for "." as the album path
470.5.2	-i switch not required for album path
480.5.0	Added option (-c duration) to do a crossfade between every picture.
49	This script automatically removes ":" and <return> characters
50		from the comment field.
510.4	Added default fade in at beginning and out at end
520.3	Added extra file checking before processing for better stability
53	from Scott Merrill <skippy at skippy.net>  Thanks!!!
540.2	Initial release'
55}
56
57help ()
58{
59echo "gallery1-to-slideshow $version"
60echo "gallery1-to-slideshow is part of the dvd-slideshow set of tools."
61echo 'Copyright 2003 Scott Dylewski <scott at dylewski.com>'
62echo 'http://freshmeat.net/dvdslideshow'
63echo  '
64gallery1-to-slideshow description:
65 Generates a text file listing of the pictures visible in a given
66 "Gallery" (http://gallery.sourceforge.net) photo album
67 in order to easily pass the information to dvd_slideshow
68
69Usage: gallery1-to-slideshow [-o <output_directory>] [-t <time_per_picture>]
70		[-b <background image>] [-c duration] <path_to_album>
71
72Options:
73 path_to_album
74	  Path to the album that you want to generate a slideshow for.
75
76 [-o output_directory]
77	  Path to the directory where you want to output file written.
78	  Default is the current working directory.
79
80 [-t time_per_picture]
81	  Number of seconds that the picture will be visible
82	  on the dvd movie. You can edit the output file to make
83	  changes to specific lines.
84	  Default is 5 seconds.
85
86 [-b <background image>]
87	  Specify background image to use for slideshow.
88
89 [-c duration]
90	  Add a crossfade between every picture for duration seconds.
91
92 -h or -help
93   Prints this help.
94'
95}
96
97if [ $# -lt 1 ]; then
98	help
99	exit 1
100fi
101
102
103## setup initial variables:
104debug=0  # 1 or 0
105crossfade=0
106
107for arg
108do
109	case "$arg" in
110	-i) shift ; album_path="$1" ; shift ;;
111	-o) shift; output_dir="$1"; shift ;;
112        -t) shift; duration="$1"; shift ;;
113        -c) shift; crossfade=1; crossfade_duration="$1"; shift ;;
114	-h) help ; exit 0 ; shift ;;
115	-?) help ; exit 0 ; shift ;;
116	-help) help ; exit 0 ; shift ;;
117	esac
118done
119
120if [ -z "$album_path" ] ; then
121	album_path="$1"
122fi
123if [ -z "$album_path" ] ; then
124	echo "[gallery1-to-slideshow] Error: No album specified"
125	exit 1
126fi
127if [ -z "$output_dir" ] ; then
128	output_dir="`pwd`"
129fi
130if [ -z "$duration" ] ; then
131	duration="5"
132fi
133
134## check_rm checks to see if the file exists before it's deleted:
135check_rm ()
136{
137	if [ -f $1 ] ; then
138		rm $1
139	fi
140}
141
142cleanup ()
143{
144	## clean up temporary files
145	echo "done"
146}
147
148forcequit () ## function gets run when we have some sort of forcequit...
149{
150	## clean up temporary files
151	cleanup
152	exit 0
153}
154
155trap 'forcequit' INT
156trap 'forcequit' KILL
157trap 'forcequit' TERM
158
159# sanity checking
160# did the user give us a valid input file to work with?
161if [ ! -f "$album_path" ]; then
162	# it's not a regular file
163        if [ ! -d "$album_path" ]; then
164        	# it's not a directory!
165                echo "[gallery1-to-slideshow] Bad input file (-i $album_path)!"
166                exit 1;
167        else
168		# check for '.' path
169		if [ "$album_path" == '.' ]; then
170		        # use current directory
171		        album_path="`pwd`"
172		fi
173	        # it is a directory, so use album.dat
174                echo "[gallery1-to-slideshow] Using album.dat in $album_path";
175                album_file="album.dat";
176                if [ ! -f "$album_path/$album_file" ]; then
177                # there's no album.dat file there!
178                        echo "[gallery1-to-slideshow] No such file $album_path/$album_file!";
179                        echo "[gallery1-to-slideshow] Aborting!";
180                        exit 1;
181                fi
182        fi
183else
184	# it is a regular file!
185	echo "[gallery1-to-slideshow] ERROR: you must specify a directory name!"
186	exit 1
187#        album=`basename "${album_path}"`;
188#        album_path=`echo $album_path | sed -e "s/$album//"`;
189fi
190
191# make sure $album_path has no trailing slash
192album_path=`echo "$album_path" | sed -e 's/\/$//'`;
193album=`basename "${album_path}"`;
194
195if [ -z "$output_dir" ]; then
196        # no output directory specified!
197        echo "[gallery1-to-slideshow] Invalid output destination (-o $output_dir)!";
198        echo "[gallery1-to-slideshow] *** Using $album_path instead.";
199        output_dir=$album_path;
200fi
201if [ ! -d "$output_dir" ]; then
202        # I'm sure it was a simple typo.
203        echo "[gallery1-to-slideshow] Invalid output destination (-o $output_dir)!";
204        echo "[gallery1-to-slideshow] *** Using $album_path instead.";
205        output_dir=$album_path;
206fi
207
208# make sure $output_dir has no trailing slash
209output_dir=`echo $output_dir | sed -e 's/\/$//'`;
210
211## now let's do some checking to see if the user put in the full path?
212
213echo "[gallery1-to-slideshow] path=$album_path"
214echo "[gallery1-to-slideshow] album=$album"
215echo "[gallery1-to-slideshow] album_file=$album_file"
216echo "[gallery1-to-slideshow] output_dir=$output_dir"
217
218outfile="$output_dir/$album".txt
219echo "[gallery1-to-slideshow] output file=$output_dir/$album".txt
220
221## if title is wanted:
222summary=`awk -F'"title"' '{print $2}' "${album_path}"/album.dat | awk -F'"' '{print $2}'`
223#gallery_version=`awk -F'"version"' '{print $2}' "${album_path}"/album.dat | awk -F'"' '{print $2}'`
224pictures=`awk -F'albumitem' '{print NF}' "${album_path}"/photos.dat`
225echo "#background:1::black" > "$outfile"
226echo "#background:0::background.jpg" >> "$outfile"
227echo "fadein:1" >> "$outfile"
228echo "title:$duration:$album:$summary" >> "$outfile"
229echo "[gallery1-to-slideshow] title:$duration:$album:$summary"
230echo "#background:0::black" >> "$outfile"
231## else do not make a title.
232
233## fade in after title:
234echo "fadeout:2" >> "$outfile"
235echo "background:1" >> "$outfile"
236
237# add music?  this makes it easier to just un-comment stuff
238echo '# remember format is:' >> "$outfile"
239echo '# musicfile.mp3:audio_track:[fadein:fadein_time:fadeout:fadeout_time]' >> "$outfile"
240echo "#musicfile.mp3:1:fadein:1:fadeout:5" >> "$outfile"
241
242echo 'fadein:1' >> "$outfile"
243
244## get the total number of pictures:
245# anybody know a better way to get rid of potential returns?
246pictures=`awk -F'albumitem' '{print NF}' "${album_path}"/photos.dat`
247pictures=`echo $pictures | tr [:space:] ' '`
248# sometimes the pictures string now has a space in-between
249# this is from someone entering return (^M) characters in their
250# comments fields
251total_pictures=0
252total_lines=0
253for i in $pictures ; do
254	total_pictures=$(( $total_pictures + $i ))
255	total_lines=$(( $total_lines + 1 ))
256done
257total_pictures=$(( $total_pictures - 1 ))
258
259pictures2=`awk -F'AlbumItem' '{print NF}' "${album_path}"/photos.dat`
260pictures2=`echo $pictures2 | tr [:space:] ' '`
261# sometimes the pictures string now has a space in-between
262# this is from someone entering return (^M) characters in their
263# comments fields
264total_pictures2=0
265total_lines=0
266for i in $pictures2 ; do
267	total_pictures2=$(( $total_pictures2 + $i ))
268	total_lines=$(( $total_lines + 1 ))
269done
270total_pictures2=$(( $total_pictures2 - 1 ))
271
272#echo "total_pictures=$total_pictures total_pictures2=$total_pictures2"
273
274# pick the greater of the two methods:
275if [ "$total_pictures2" -gt "$total_pictures" ] ; then
276	## Newer versions of gallery use "AlbumItem" instead of "albumitem"
277	newgallery=1
278#	pictures=`awk -F'AlbumItem' '{print NF}' "${album_path}"/photos.dat`
279	total_pictures="$total_pictures2"
280	echo "[gallery1-to-slideshow] new gallery version"
281else
282	newgallery=0
283	echo "[gallery1-to-slideshow] old gallery version"
284fi
285echo "[gallery1-to-slideshow] total pictures=$total_pictures"
286
287#pictures=$(( $pictures + 1 ))
288
289## yeah, this is slow, but not as slow as typing in everything
290## from the gallery yourself...
291j=1
292for i in `seq2 2 $total_pictures`; do
293	## get rid of potential newlines in comments sections first:
294	if [ "$newgallery" -eq 1 ] ; then
295		albumitem=`cat "$album_path/photos.dat" | tr '\n' '\a' | awk -F'AlbumItem' '{print $'"$i"'}'`
296	else
297		albumitem=`cat "$album_path/photos.dat" | tr '\n' '\a' | awk -F'albumitem' '{print $'"$i"'}'`
298	fi
299	filename[$j]=`echo "${albumitem}" | awk -F'"name"' '{print $2}' | awk -F'"' '{print $2}'`
300	comment[$j]=`echo "${albumitem}" | awk -F'"caption"' '{print $2}' | awk -F':"' '{print $2}' | awk -F'";' '{print $1}' | tr '\a' ' ' | sed -e 's/\r/\\\\n/' | sed -e 's/<a href.*">//' | sed -e 's/<\/a>//' | sed -e 's/\:/\\\:/g'`
301	hidden[$j]=`echo "${albumitem}" | awk -F'"hidden"' '{print $2}' | awk -F';' '{print $2}'`
302	if [ "${hidden[$j]}" == 'N' ] || [ -z "${hidden[$j]}" ] ; then  ## only include this picture if it's shown in the gallery
303		new_filename="${album_path}/${filename[$j]}.jpg"
304		echo "[gallery1-to-slideshow] picture $j of $total_pictures"
305		echo "[gallery1-to-slideshow] filename=${new_filename}   comment=${comment[$j]}"
306		## write to file:
307		## Fix by Ivan Espinosa 1/2006
308                if [ -r "${new_filename}" ] ; then
309                  if [ "$j" -ne 1  ] ; then
310                    if [ "$crossfade" -eq 1 ] ; then
311                      echo "crossfade:$crossfade_duration:" >> "$outfile"
312                    fi
313                  fi
314		  echo "${new_filename}:$duration:${comment[$j]}" >> "$outfile"
315		fi
316#		if [ "$crossfade" -eq 1 ] && [ "$(($i+1))" -lt "$total_pictures" ] ; then
317#		echo "crossfade:$crossfade_duration:" >> "$outfile"
318#		fi
319		let j=$j+1
320	else
321		echo "[gallery1-to-slideshow] picture $j of $total_pictures Hidden in Gallery"
322		let j=$j+1
323	fi
324done
325
326echo ' ' >> "$outfile"
327echo '# to insert a title slide in the middle of the slideshow, use this:' >> "$outfile"
328echo '#fadeout:1' >> "$outfile"
329echo '#background:1' >> "$outfile"
330echo '#background:1::background.jpg' >> "$outfile"
331echo '#title:5::Title Text' >> "$outfile"
332echo '#background:0::black' >> "$outfile"
333echo '#fadeout:1' >> "$outfile"
334echo '#fadein:1' >> "$outfile"
335echo ' ' >> "$outfile"
336
337## fade out at end
338echo "fadeout:1" >> "$outfile"
339## stop the music before the final fadeout to the background:
340echo "silence:1:" >> "$outfile"
341echo "background:1" >> "$outfile"
342echo '#fadein:1' >> "$outfile"
343echo '#background.jpg:1' >> "$outfile"
344
345
346cleanup
347
348