#!/usr/bin/ruby # # * BOOH * # # A.k.a `Best web-album Of the world, Or your money back, Humerus'. # # The acronyn sucks, however this is a tribute to Dragon Ball by # Akira Toriyama, where the last enemy beaten by heroes of Dragon # Ball is named "Boo". But there was already a free software project # called Boo, so this one will be it "Booh". Or whatever. # # # Copyright (c) 2004 Guillaume Cottenceau # # This software may be freely redistributed under the terms of the GNU # public license version 2. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. require 'iconv' require 'timeout' require 'gettext' include GetText bindtextdomain("booh") require 'booh/config.rb' require 'booh/version.rb' module Booh $verbose_level = 2 $CURRENT_CHARSET = `locale charmap`.chomp $convert = 'convert -interlace line +profile "*"' $convert_enhance = '-contrast -enhance -normalize' def utf8(string) return Iconv::iconv("UTF-8", $CURRENT_CHARSET, string).to_s end def from_utf8(string) return Iconv::iconv($CURRENT_CHARSET, "UTF-8", string).to_s end def make_dest_filename(orig_filename) #- we remove non alphanumeric characters but need to do that #- cleverly to not end up with two similar dest filenames. we won't #- urlencode because urldecode might happen in the browser. return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-_0-9\.\/]/ ? v.chr : sprintf("%2X", v) }.to_s end def msg(verbose_level, msg) if verbose_level <= $verbose_level if verbose_level == 0 warn _("\t***ERROR***: %s\n") % msg elsif verbose_level == 1 warn _("\tWarning: %s\n") % msg else puts msg end end end def msg_(verbose_level, msg) if verbose_level <= $verbose_level if verbose_level == 0 warn _("\t***ERROR***: %s") % msg elsif verbose_level == 1 warn _("\tWarning: %s") % msg else print msg end end end def die(msg) puts msg exit 1 end def select_theme(name) $theme = name msg 3, _("Selecting theme `%s'") % $theme themedir = "#{$FPATH}/themes/#{$theme}" if !File.directory?(themedir) die _("Theme was not found (tried %s directory).") % themedir end require "#{themedir}/metadata/parameters.rb" $default_size = $images_size.detect { |sizeobj| sizeobj['default'] } if $default_size == nil die _("Theme `%s' has no default size.") % $theme end end def entry2type(entry) if entry =~ /\.(jpg|jpeg|jpe|gif|bmp|png)$/i return 'image' elsif entry =~ /\.(mov|avi|mpg|mpeg|mpe|wmv|asx)$/i #- might consider using file magic later.. return 'video' else return nil end end def sys(cmd) msg 2, cmd system(cmd) end #- parallelizable sys def psys(cmd) if $mproc if pid = fork $pids << pid else msg 2, cmd + ' &' system(cmd) exit 0 end if $pids.length == $mproc finished = Process.wait2 $pids.delete(finished[0]) $pids = $pids.find_all { |pid| Process.waitpid(pid, Process::WNOHANG) == nil } end else sys(cmd) end end #- grab the results of a command but don't sleep forever on a runaway process def subproc_runaway_aware(command) begin timeout(5) { return `#{command}` } rescue Timeout::Error msg 1, _("forgetting runaway process (transcode sucks again?)") #- todo should slay transcode but dunno how to do that return nil end end def get_image_size(fullpath) if `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) / return { :x => $1, :y => $2 } else return nil end end #- commify from http://pleac.sourceforge.net/ (pleac rulz) def commify(n) n.to_s =~ /([^\.]*)(\..*)?/ int, dec = $1.reverse, $2 ? $2 : "" while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2' + _(",") + '\3') end int.reverse + dec end def guess_rotate(filename) orientation = `exif '#{filename}'`.detect { |line| line =~ /^Orientation/ } if orientation =~ /right - top/ angle = 90 elsif orientation =~ /left - bottom/ angle = -90 else return 0 end size = get_image_size(filename) #- remove rotate if image is obviously already in portrait (situation can come from gthumb) if size && size[:x] < size[:y] return 0 else return angle end end def rotate_pixbuf(pixbuf, angle) return pixbuf.rotate(angle == 90 ? Gdk::Pixbuf::ROTATE_CLOCKWISE : angle == 180 ? Gdk::Pixbuf::ROTATE_UPSIDEDOWN : angle == 270 ? Gdk::Pixbuf::ROTATE_COUNTERCLOCKWISE : Gdk::Pixbuf::ROTATE_NONE) end def gen_thumbnails_element(orig, xmldir, allow_background, dests) gen_thumbnails(orig, xmldir, allow_background, dests, xmldir.elements["[@filename='#{utf8(File.basename(orig))}']"], '') end def gen_thumbnails_subdir(orig, xmldir, allow_background, dests, type) #- type can be `subdirs' or `thumbnails' gen_thumbnails(orig, xmldir, allow_background, dests, xmldir, type + '-') end def gen_thumbnails(orig, xmldir, allow_background, dests, felem, attributes_prefix) if !dests.detect { |dest| !File.exists?(dest['filename']) } return true end convert_options = '' if entry2type(orig) == 'image' if felem rotate = felem.attributes["#{attributes_prefix}rotate"] if !rotate felem.add_attribute("#{attributes_prefix}rotate", rotate = guess_rotate(orig).to_i) end convert_options += "-rotate #{rotate} " if felem.attributes["#{attributes_prefix}enhance"] convert_options += ($config['convert-enhance'] || $convert_enhance) + " " end end for dest in dests if !File.exists?(dest['filename']) cmd = "#{$convert} #{convert_options}-size #{dest['size']} -resize #{dest['size']} '#{orig}' '#{dest['filename']}'" if allow_background psys(cmd) else sys(cmd) end end end return true elsif entry2type(orig) == 'video' dest_dir = make_dest_filename(File.dirname(dests[0]['filename'])) if felem #- frame-offset is an attribute that allows to specify which frame to use for the thumbnail frame_offset = felem.attributes["#{attributes_prefix}frame-offset"] if !frame_offset felem.add_attribute("#{attributes_prefix}frame-offset", frame_offset = "0") end frame_offset = frame_offset.to_i if rotate = felem.attributes["#{attributes_prefix}rotate"] convert_options += "-rotate #{rotate} " end if felem.attributes["#{attributes_prefix}enhance"] convert_options += ($config['convert-enhance'] || $convert_enhance) + " " end end for dest in dests if !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg") transcode_options = '' if felem if felem.attributes["#{attributes_prefix}color-swap"] transcode_options += '-k ' end end cmd = "transcode -a 0 -c #{frame_offset}-#{frame_offset+1} -i '#{orig}' -y jpg -o '#{dest_dir}/screenshot.jpg' #{transcode_options} 2>&1" msg 2, cmd results = subproc_runaway_aware(cmd) if results =~ /skipping frames/ && results =~ /encoded 0 frames/ msg 0, _("specified frame-offset too large? max frame was: %s. that may also be another probleme. try another value.") % results.scan(/skipping frames \[000000-(\d+)\]/)[-1] return false elsif results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg") msg 2, _("* could not extract first image of video %s with transcode, will try first converting with mencoder") % orig cmd = "mencoder '#{orig}' -nosound -ovc lavc -lavcopts vcodec=mjpeg -o '#{dest_dir}/foo.avi' -frames #{frame_offset+1} -fps 25 >/dev/null 2>/dev/null" msg 2, cmd system cmd if File.exists?("#{dest_dir}/foo.avi") cmd = "transcode -a 0 -c #{frame_offset}-#{frame_offset+1} -i '#{dest_dir}/foo.avi' -y jpg -o '#{dest_dir}/screenshot.jpg' #{transcode_options} 2>&1" msg 2, cmd results = subproc_runaway_aware(cmd) system("rm -f '#{dest_dir}/foo.avi'") if results =~ /skipping frames/ && results =~ /encoded 0 frames/ msg 0, _("specified frame-offset too large? max frame was: %s. that may also be another probleme. try another value.") % results.scan(/skipping frames \[000000-(\d+)\]/)[-1] return false elsif results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg") msg 0, _("could not extract first image of video %s encoded by mencoder") % "#{dest_dir}/foo.avi" return false end else msg 0, _("could not make mencoder to encode %s to mpeg4") % orig return false end end end if !File.exists?(dest['filename']) sys("#{$convert} #{convert_options}-size #{dest['size']} -resize #{dest['size']} #{dest_dir}/screenshot.jpg000000.jpg '#{dest['filename']}'") end end return true end end def invornil(obj, methodname) if obj == nil return nil else return obj.method(methodname).call end end def find_subalbum_info_type(xmldir) #- first look for subdirs info; if not, means there is no subdir if xmldir.attributes['subdirs-caption'] return 'subdirs' else return 'thumbnails' end end def find_subalbum_caption_info(xmldir) type = find_subalbum_info_type(xmldir) return from_utf8(xmldir.attributes["#{type}-captionfile"]), xmldir.attributes["#{type}-caption"] end end