2007-12-18 00:22:51 +00:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
|
|
|
use Proc::Daemon;
|
|
|
|
use DBI;
|
|
|
|
use Digest::SHA;
|
|
|
|
use File::Copy;
|
|
|
|
|
2008-01-05 15:11:56 +00:00
|
|
|
#TODO: put this into central configuration file
|
2007-12-18 00:22:51 +00:00
|
|
|
$database = 'yolanda';
|
|
|
|
$dbhost = 'localhost';
|
|
|
|
$dbuser = 'root';
|
|
|
|
$dbpass = '';
|
|
|
|
$root = '/var/www/yolanda';
|
|
|
|
|
|
|
|
#TODO: deamonize by uncommenting this line
|
|
|
|
#Proc::Daemon::Init;
|
|
|
|
|
2008-04-03 12:52:49 +00:00
|
|
|
$LOG = "/dev/null";
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
|
2008-01-05 15:11:56 +00:00
|
|
|
#TODO: maybe keep file open the whole time ?
|
2007-12-18 00:22:51 +00:00
|
|
|
sub appendlog
|
|
|
|
{
|
2008-02-14 22:35:25 +00:00
|
|
|
if (open(FILE, ">>$LOG"))
|
|
|
|
{
|
|
|
|
print FILE scalar(localtime)." ".$$."\n";
|
|
|
|
print "------------------------------------\n";
|
|
|
|
print join("\n",@_)."\n";
|
|
|
|
print "------------------------------------\n\n";
|
|
|
|
close FILE;
|
|
|
|
}
|
2007-12-18 00:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub interrupt
|
|
|
|
{
|
2008-02-14 22:35:25 +00:00
|
|
|
appendlog(@_);
|
|
|
|
die;
|
2007-12-18 00:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or interrupt "could not connect to db";
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
2008-02-14 22:35:25 +00:00
|
|
|
#get fresh video id from db
|
2008-04-09 12:23:14 +00:00
|
|
|
my $sth = $dbh->prepare(qq{select id, filesize, duration, width, height, fps, hash
|
2008-04-09 19:36:34 +00:00
|
|
|
from uploaded where filesize != -1 and duration != -1 and width != -1 and height != -1 limit 1}) or interrupt $dbh->errstr;
|
2008-02-14 22:35:25 +00:00
|
|
|
|
|
|
|
$sth->execute() or interrupt $dbh->errstr;
|
2008-04-09 12:23:14 +00:00
|
|
|
my ($id,$filesize, $duration, $width, $height, $fps, $sha) = $sth->fetchrow_array();
|
2008-02-14 22:35:25 +00:00
|
|
|
$sth->finish() or interrupt $dbh->errstr;
|
|
|
|
|
|
|
|
if($id)
|
|
|
|
{
|
2008-04-09 12:23:14 +00:00
|
|
|
$vmaxheight = 640;
|
|
|
|
|
|
|
|
#video height is either the maximum video height
|
|
|
|
#or (when the original is smaller than that) the original height
|
|
|
|
#check for multiple by 8
|
|
|
|
$vheight = $vmaxheight <= $height ? $vmaxheight : int($height/8 + .5)*8;
|
|
|
|
$vwidth = int($vheight*($width/$height)/8 + .5)*8;
|
|
|
|
|
|
|
|
$abitrate = 64;
|
|
|
|
$vbitrate = int(($filesize*8) / $duration + .5) - $abitrate;
|
|
|
|
|
|
|
|
#TODO: add metadata information
|
|
|
|
system "ffmpeg2theora --optimize --videobitrate $vbitrate --audiobitrate $abitrate --sharpness 0 --width $vwidth --height $vheight --output $root/videos/$id /tmp/$id";
|
|
|
|
|
|
|
|
appendlog $id, $audio, $video, $vwidth, $vheight, $fps, $duration, $sha;
|
|
|
|
|
|
|
|
$filesize = -s "$root/videos/$id";
|
|
|
|
|
|
|
|
#add video to videos table
|
|
|
|
$dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator,
|
|
|
|
subject, source, language, coverage, rights, license, ?, duration, ?, ?, fps, hash, 0, 0
|
|
|
|
from uploaded where id = ?}, undef, $filesize, $vwidth,
|
|
|
|
$vheight, $id) or interrupt $dbh->errstr;
|
|
|
|
|
|
|
|
#delete temp file
|
|
|
|
unlink "/tmp/$id";
|
|
|
|
|
|
|
|
#TODO:create torrent file
|
2008-02-14 22:35:25 +00:00
|
|
|
|
2008-04-09 12:23:14 +00:00
|
|
|
#delete from uploaded table
|
|
|
|
$dbh->do(qq{delete from uploaded where id = ?}, undef, $id) or interrupt $dbh->errstr;
|
2008-02-14 22:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-02 09:56:02 +00:00
|
|
|
#TODO: maybe make this event-driven by using the kernels has-this-file-changed-interface ?
|
2008-02-14 22:35:25 +00:00
|
|
|
sleep 10;
|
|
|
|
}
|
2007-12-18 00:22:51 +00:00
|
|
|
}
|