Created
January 22, 2009 07:04
-
-
Save fuba/50458 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub capture_thumbnails { | |
my %opt = @_; | |
my $file = $opt{file} or die; | |
my $offset = $opt{offset} || 0; | |
my @stats = stat($file); | |
my $deflength = 60 * ($stats[7]/120000000); | |
my $un_reset = $opt{un_reset}; | |
my $newfile = $file; | |
$newfile =~ s/^.*?([^\/]+)\.ts$/$1/; | |
my $workdir = $opt{workdir} || '/volumes/sanjou400/friio_cache/'.time; | |
mkpath $workdir unless (-e $workdir); | |
my $width = $opt{width} || 640; | |
my $height = $width * ($opt{frac} || (9/16)); | |
my $n = $opt{number} || 10; | |
my $time_unit = $opt{time_unit} || (($opt{length} || $deflength) / $n); | |
my @files; | |
unless ($un_reset) { | |
rmtree $workdir; | |
mkpath $workdir; | |
} | |
my $ffmpeg = $opt{ffmpeg} || 'ffmpeg'; | |
for my $i (1..$n) { | |
my $filename = "$workdir/${i}.png"; | |
if ($un_reset) { | |
push @files, $filename if (-e $filename); | |
next; | |
} | |
my $num = ($i*$time_unit) + $offset; | |
warn "$ffmpeg -deinterlace -vframes 1 -ss $num -i \"$file\" -s ${width}x${height} -f image2 \"$filename\""; | |
system("$ffmpeg -deinterlace -vframes 1 -ss $num -i \"$file\" -s ${width}x${height} -f image2 \"$filename\""); | |
push @files, $filename if (-e $filename); | |
} | |
return undef unless (@files); | |
my %r = ( | |
workdir => $workdir, | |
files => \@files, | |
title => $newfile, | |
width => $width, | |
height => $height, | |
number => $n, | |
time_unit => $time_unit, | |
); | |
for my $k (keys %r) { | |
$opt{$k} = $r{$k}; | |
} | |
return \%opt; | |
} | |
sub gradation_thumbnail { | |
my %p = @_; | |
return '' unless $p{files}; | |
my @files = @{$p{files}}; | |
my $convert = $p{convert} || 'convert'; | |
my $composite = $p{composite} || 'composite'; | |
my $height = $p{height}; | |
my $width = $p{width}; | |
my $workdir = $p{workdir}; | |
my $base = $p{base}; | |
return '' unless @files; | |
return '' unless $base; | |
my $unit = 1 / scalar(@files); | |
my @hue_files; | |
for my $i (0..$#files) { | |
my $file = $files[$i]; | |
my $new = $file.'_hue.png'; | |
my $hue_s = ($unit * ($i - 0.5)); | |
my $hue_e = ($unit * ($i + 1.5)); | |
($hue_s, $hue_e) = ($hue_e, $hue_s) if ($hue_s > $hue_e); | |
$hue_s = 0 if ($hue_s < 0); | |
$hue_e = 1 if ($hue_e > 1); | |
my $hue_s_str = sprintf "%.2f", $hue_s; | |
my $hue_e_str = sprintf "%.2f", $hue_e; | |
my $color = '(r+g+b)/3'; | |
my $command_hue = "$convert \"$file\" -fx \"(hue>${hue_s_str}&&hue<${hue_e_str})?p:${color}\" \"$new\""; | |
warn $command_hue; | |
`$command_hue`; | |
push @hue_files, $new; | |
} | |
my $files_str = join " ", map {"\"$_\""} @hue_files; | |
my $mask_file = "${workdir}/mask.png"; | |
my $maskcommand = "$composite $files_str -compose plus \"$mask_file\""; | |
`$maskcommand`; | |
return '' unless (-e $mask_file); | |
my @pattern = qw/difference multiply/; | |
my $p = $pattern[floor(rand(scalar(@pattern)))]; | |
my $result = "${workdir}/result_${p}.png"; | |
my $command = "$composite -compose $p \"$mask_file\" \"$base\" \"$result\""; | |
`$command`; | |
return '' unless (-e $result); | |
return $result; | |
} | |
sub strip_thumbnail_h { | |
my %p = @_; | |
return '' unless $p{files}; | |
my @files = @{$p{files}}; | |
my $convert = $p{convert} || 'convert'; | |
my $composite = $p{composite} || 'composite'; | |
my $height = $p{height}; | |
my $width = $p{width}; | |
my $workdir = $p{workdir}; | |
my @strip_files; | |
my @strip_filter_files; | |
my $unit = floor($width / scalar(@files)); | |
for my $i (0..$#files) { | |
my $file = $files[$i]; | |
my $new = $file.'_strip.png'; | |
my $new_prev = $file.'_strip_prev.png'; | |
my $left = $i*$unit; | |
my $command_strip = "$convert -crop ${unit}x${height}+${left}+0 $file $new"; | |
system($command_strip); | |
push @strip_files, $new; | |
$left -= $unit; | |
if ($left >= 0) { | |
$strip_filter_files[$i-1] = $new_prev; | |
} | |
else { | |
$left = $unit * $#files; | |
$strip_filter_files[$#files] = $new_prev; | |
} | |
$command_strip = "$convert -crop ${unit}x${height}+${left}+0 $file $new_prev"; | |
system($command_strip); | |
} | |
my $files_str = join " ", @strip_files; | |
my $command = "$convert +append $files_str ${workdir}/original.png"; | |
system("$command");# && convert -rotate 270 $target $target"); | |
my $filter_files_str = join " ", @strip_filter_files; | |
my $filtercommand = "$convert +append $filter_files_str ${workdir}/filter.png"; | |
system("$filtercommand"); | |
my @pattern = qw/plus difference multiply bumpmap/; | |
my $p = $pattern[floor(rand(scalar(@pattern)))]; | |
`$composite -compose $p ${workdir}/filter.png ${workdir}/original.png \"${workdir}/result.png\"`; | |
return "${workdir}/result.png"; | |
} | |
sub strip_thumbnail { | |
my %p = @_; | |
return '' unless $p{files}; | |
my @files = @{$p{files}}; | |
my $convert = $p{convert} || 'convert'; | |
my $composite = $p{composite} || 'composite'; | |
my $height = $p{height}; | |
my $width = $p{width}; | |
my $workdir = $p{workdir}; | |
my @strip_files; | |
my @strip_filter_files; | |
my $unit = floor($height / scalar(@files)); | |
for my $i (0..$#files) { | |
my $file = $files[$i]; | |
my $new = $file.'_strip.png'; | |
my $new_prev = $file.'_strip_prev.png'; | |
my $top = $i*$unit; | |
my $command_strip = "$convert -crop ${width}x${unit}+0+${top} $file $new"; | |
system($command_strip); | |
push @strip_files, $new; | |
$top -= $unit; | |
if ($top >= 0) { | |
$strip_filter_files[$i-1] = $new_prev; | |
} | |
else { | |
$top = $unit * $#files; | |
$strip_filter_files[$#files] = $new_prev; | |
} | |
$command_strip = "$convert -crop ${width}x${unit}+0+${top} $file $new_prev"; | |
system($command_strip); | |
} | |
my $files_str = join " ", @strip_files; | |
my $command = "$convert -append $files_str ${workdir}/original.png"; | |
system("$command");# && convert -rotate 270 $target $target"); | |
my $filter_files_str = join " ", @strip_filter_files; | |
my $filtercommand = "$convert -append $filter_files_str ${workdir}/filter.png"; | |
system("$filtercommand"); | |
my @pattern = qw/plus difference multiply bumpmap/; | |
my $p = $pattern[floor(rand(scalar(@pattern)))]; | |
`$composite -compose $p ${workdir}/filter.png ${workdir}/original.png \"${workdir}/result.png\"`; | |
return "${workdir}/result.png"; | |
} | |
sub upload_thumbnail { | |
my $file = shift; | |
my $param = capture_thumbnails( | |
file => $file, | |
width => 1920, | |
number => 4+int(rand(6)), | |
); | |
$param->{base} = ( int(rand(10)) % 2 ) ? strip_thumbnail(%$param) : strip_thumbnail_h(%$param); | |
my $result_file = gradation_thumbnail(%$param); | |
return '' unless (-e $result_file); | |
my $flickr = Flickr::Upload->new({ | |
key => 'secret', | |
secret => 'secret', | |
}); | |
my $auth_token = 'secret'; | |
warn 'done composite and upload to flickr'; | |
my $photo_id = $flickr->upload( | |
photo => $result_file, | |
auth_token => $auth_token, | |
is_public => 1, | |
title => $param->{title}, | |
async => 0, | |
); | |
return 'http://www.flickr.com/photos/fuba_recorder/'.$photo_id; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment