2007-12-18 00:22:51 +00:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
|
|
|
use Proc::Daemon;
|
|
|
|
use DBI;
|
|
|
|
use Digest::SHA;
|
|
|
|
use File::Copy;
|
2008-04-15 18:56:13 +00:00
|
|
|
use XML::Simple qw(:strict);
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
$root = '/var/www/yolanda';
|
|
|
|
|
2008-04-15 18:56:13 +00:00
|
|
|
#set global config variable
|
|
|
|
$config = XMLin("$root/config/backend.xml", KeyAttr => {string => 'id'}, ForceArray => [ 'string' ], ContentKey => '-content');
|
|
|
|
$config = $config->{"strings"}->{"string"};
|
|
|
|
|
2007-12-18 00:22:51 +00:00
|
|
|
#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";
|
2008-04-11 08:48:25 +00:00
|
|
|
#print "------------------------------------\n";
|
|
|
|
#print join("\n",@_)."\n";
|
|
|
|
#print "------------------------------------\n\n";
|
2008-02-14 22:35:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-04-15 18:56:13 +00:00
|
|
|
$dbh = DBI->connect("DBI:mysql:".$config->{"database_name"}.":".$config->{"database_host"}, $config->{"database_username"}, $config->{"database_password"}) or die $DBI::errstr;
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
#video height is either the maximum video height
|
|
|
|
#or (when the original is smaller than that) the original height
|
2008-04-29 07:02:07 +00:00
|
|
|
#check for multiple of 8
|
2008-04-15 18:56:13 +00:00
|
|
|
$vheight = $height <= $config->{"video_height_max"} ? int($height/8 + .5)*8 : $config->{"video_height_max"};
|
2008-04-09 12:23:14 +00:00
|
|
|
$vwidth = int($vheight*($width/$height)/8 + .5)*8;
|
|
|
|
|
2008-04-15 18:56:13 +00:00
|
|
|
#if calculated width is greater than max_width, recalculate height
|
|
|
|
if($vwidth > $config->{"video_width_max"})
|
|
|
|
{
|
|
|
|
$vwidth = $config->{"video_width_max"};
|
|
|
|
$vheight = int($vwidth*($height/$width)/8 + .5)*8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#calculate video bitrate - try to keep the original filesize
|
|
|
|
$vbitrate = int((int(($filesize*8) / $duration + .5) - $config->{"video_audio_bitrate"})/1000);
|
|
|
|
#check if the bitrate is lower than $config->{"video_bitrate_max"} and adjust if necessary
|
|
|
|
$vbitrate = $vbitrate <= $config->{"video_bitrate_max"} ? $vbitrate : $config->{"video_bitrate_max"};
|
2008-04-09 12:23:14 +00:00
|
|
|
|
|
|
|
#TODO: add metadata information
|
2008-04-15 18:56:13 +00:00
|
|
|
$ffmpeg = system "ffmpeg2theora --optimize --videobitrate $vbitrate --audiobitrate ".$config->{"video_audio_bitrate"}." --sharpness 0 --width $vwidth --height $vheight --output $root/videos/$id /tmp/$id";
|
2008-04-09 12:23:14 +00:00
|
|
|
|
2008-04-10 11:57:31 +00:00
|
|
|
appendlog $id, $vbitrate, $filesize, $vwidth, $vheight, $fps, $duration, $sha, $ffmpeg;
|
2008-04-09 12:23:14 +00:00
|
|
|
|
2008-04-10 11:57:31 +00:00
|
|
|
#only insert into videos table when everything went right
|
|
|
|
if($ffmpeg == 0)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
2008-04-09 12:23:14 +00:00
|
|
|
|
2008-04-10 11:57:31 +00:00
|
|
|
#delete from uploaded table and from /tmp
|
2008-04-09 12:23:14 +00:00
|
|
|
unlink "/tmp/$id";
|
|
|
|
$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
|
|
|
}
|