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;
|
|
|
|
|
|
|
|
$LOG = "$root/daemon.log";
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (open(FILE, ">>$LOG"))
|
|
|
|
{
|
2008-01-05 13:57:02 +00:00
|
|
|
print FILE scalar(localtime)." ".$$."\n";
|
|
|
|
print "------------------------------------\n";
|
|
|
|
print join("\n",@_)."\n";
|
|
|
|
print "------------------------------------\n\n";
|
2007-12-18 00:22:51 +00:00
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub interrupt
|
|
|
|
{
|
|
|
|
appendlog(@_);
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or interrupt "could not connect to db";
|
|
|
|
|
|
|
|
#video status:
|
|
|
|
# 0 - new entry - nothing done yet
|
|
|
|
# 1 - valid public video
|
|
|
|
# 2 - error: invalid audio and/or video stream
|
|
|
|
# 3 - error: file not found
|
|
|
|
# 4 - error: file is not a video
|
|
|
|
# 5 - error: video is a duplicate
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
#get fresh video id from db
|
|
|
|
my $sth = $dbh->prepare(qq{select id from uploaded where status = 0 limit 1}) or interrupt $dbh->errstr;
|
|
|
|
|
|
|
|
$sth->execute() or interrupt $dbh->errstr;
|
|
|
|
my ($id) = $sth->fetchrow_array();
|
|
|
|
$sth->finish() or interrupt $dbh->errstr;
|
|
|
|
|
|
|
|
if($id)
|
|
|
|
{
|
2007-12-28 17:18:44 +00:00
|
|
|
$info = `export SDL_VIDEODRIVER="dummy"; ffplay -stats -an -vn -nodisp $root/tmp/$id 2>&1`;
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
if($info =~ /ignoring/)
|
|
|
|
{
|
2008-01-05 13:57:02 +00:00
|
|
|
appendlog "id: $id",
|
|
|
|
"error: invalid stream",
|
|
|
|
"ffplay msg: $info";
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
#write status 2 to uploaded table
|
|
|
|
$dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 2, $id) or interrupt $dbh->errstr;
|
|
|
|
unlink "$root/tmp/$id";
|
|
|
|
}
|
|
|
|
elsif ($info =~ /I\/O error occured/)
|
|
|
|
{
|
2008-01-05 13:57:02 +00:00
|
|
|
appendlog "id: $id",
|
|
|
|
"error: file not found",
|
|
|
|
"ffplay msg: $info";
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
#write status 3 to uploaded table
|
|
|
|
$dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 3, $id) or interrupt $dbh->errstr;
|
|
|
|
unlink "$root/tmp/$id";
|
|
|
|
}
|
|
|
|
elsif ($info =~ /Unknown format/ or $info =~ /could not find codec parameters/)
|
|
|
|
{
|
2008-01-05 13:57:02 +00:00
|
|
|
appendlog "id: $id",
|
|
|
|
"error: file is of unknown format",
|
|
|
|
"ffplay msg: $info";
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
#write status 4 to uploaded table
|
|
|
|
$dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 4, $id) or interrupt $dbh->errstr;
|
|
|
|
unlink "$root/tmp/$id";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$sha = new Digest::SHA(256);
|
2007-12-28 17:18:44 +00:00
|
|
|
$sha->addfile("$root/tmp/$id") or die "cannot open $root/tmp/$id";
|
2007-12-18 00:22:51 +00:00
|
|
|
$sha = $sha->hexdigest;
|
|
|
|
|
|
|
|
#check if this hash is already in database
|
|
|
|
my $sth = $dbh->prepare(qq{select id from videos where hash = ? limit 1}) or interrupt $dbh->errstr;
|
|
|
|
$sth->execute($sha) or interrupt $dbh->errstr;
|
|
|
|
my ($resultid) = $sth->fetchrow_array();
|
|
|
|
$sth->finish() or interrupt $dbh->errstr;
|
|
|
|
|
2008-01-05 15:11:56 +00:00
|
|
|
#if so, then video is a duplicate (alternatively ALL HAIL QUANTUM COMPUTING)
|
2007-12-18 00:22:51 +00:00
|
|
|
if($resultid)
|
|
|
|
{
|
2008-01-05 13:57:02 +00:00
|
|
|
appendlog "id: $id",
|
|
|
|
"error: video already uploaded: $resultid";
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
#write status 5 to uploaded table
|
|
|
|
$dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 5, $id) or interrupt $dbh->errstr;
|
|
|
|
unlink "$root/tmp/$id";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
($container, $duration) = $info =~ /Input \#0, (\w+),.+?\n.+?Duration: (\d{2}:\d{2}:\d{2}\.\d)/;
|
|
|
|
|
|
|
|
#these two regexes have to be applied seperately because nobody knows which stream (audio or video) comes first
|
|
|
|
($audio) = $info =~ /Audio: (\w+)/;
|
2007-12-29 04:23:11 +00:00
|
|
|
($video, $width, $height, $fps) = $info =~ /Video: ([\w\d]+),.+?(\d+)x(\d+),.+?(\d+\.\d+) fps/;
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
if(!$audio or !$video or !$duration)
|
|
|
|
{
|
2008-01-05 13:57:02 +00:00
|
|
|
appendlog "id: $id",
|
|
|
|
"error: error: stream is missing or video is corrupt",
|
|
|
|
"audio: $audio",
|
|
|
|
"video: $video",
|
|
|
|
"duration: $duration",
|
|
|
|
"ffplay msg: $info";
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
#write status 2 to uploaded table
|
|
|
|
$dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 2, $id) or interrupt $dbh->errstr;
|
|
|
|
unlink "$root/tmp/$id";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$filesize = -s "$root/tmp/$id";
|
|
|
|
|
|
|
|
#convert hh:mm:ss.s duration to full seconds - thanks perl for making this so damn easy!
|
|
|
|
#don't want to know how this would look in python or php... hell I don't even have to create extra variables!
|
|
|
|
$duration =~ /^(\d{2}):(\d{2}):(\d{2})\.(\d)$/;
|
|
|
|
$duration = int($1*3600 + $2*60 + $3 + $4/10 + .5);
|
|
|
|
|
|
|
|
#create thumbnail
|
|
|
|
$thumbnailsec = int($duration/3 + .5);
|
|
|
|
|
|
|
|
#the width/height calculation could of course be much shorter but less readable then
|
|
|
|
#all thumbs have equal height
|
|
|
|
$tnmaxheight = 120;
|
|
|
|
$tnheight = $tnmaxheight;
|
|
|
|
$tnwidth = int($tnheight*($width/$height)/2 + .5)*2;
|
|
|
|
|
|
|
|
system "ffmpeg -i $root/tmp/$id -vcodec mjpeg -vframes 1 -an -f rawvideo -ss $thumbnailsec -s ".$tnwidth."x$tnheight $root/video-stills/$id";
|
|
|
|
|
2008-01-05 15:11:56 +00:00
|
|
|
$vmaxheight = 640;
|
2007-12-18 00:22:51 +00:00
|
|
|
|
|
|
|
#check if the upload already is in the right format and smaller/equal max-width/height
|
|
|
|
if ($container eq 'ogg' and $video eq 'theora' and $audio eq 'vorbis' and $height <= $vmaxheight)
|
|
|
|
{
|
|
|
|
appendlog $id, "file already is ogg-theora/vorbis";
|
|
|
|
|
|
|
|
#add video to videos table
|
|
|
|
$dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator,
|
2007-12-22 12:30:11 +00:00
|
|
|
subject, source, language, coverage, rights, license, ?, ?, ?, ?, ?, ?, 0, 0
|
2007-12-18 00:22:51 +00:00
|
|
|
from uploaded where id = ?}, undef, $filesize, $duration, $width,
|
|
|
|
$height, $fps, $sha, $id) or interrupt $dbh->errstr;
|
|
|
|
|
|
|
|
#move video
|
|
|
|
move "$root/tmp/$id", "$root/videos/$id";
|
|
|
|
}
|
|
|
|
else #encode video
|
|
|
|
{
|
|
|
|
#calculate video width
|
2008-01-05 15:11:56 +00:00
|
|
|
#TODO: ffmpeg only accepts values dividable by 8 !!! ( check that )
|
2007-12-18 00:22:51 +00:00
|
|
|
$vheight = $vmaxheight <= $height ? $vmaxheight : $height;
|
|
|
|
$vwidth = int($vheight*($width/$height)/2 + .5)*2;
|
2008-01-05 13:57:02 +00:00
|
|
|
|
|
|
|
$abitrate = 64;
|
|
|
|
$vbitrate = int($filesize*8) / $duration + .5) - $abitrate;
|
|
|
|
|
2008-01-05 15:11:56 +00:00
|
|
|
#TODO: add metadata information
|
2008-01-05 13:57:02 +00:00
|
|
|
system "ffmpeg2theora --optimize --videobitrate $vbitrate --audiobitrate $abitrate --sharpness 0 --width $vwidth --height $vheight --output $root/videos/$id $root/tmp/$id";
|
|
|
|
|
2007-12-18 00:22:51 +00:00
|
|
|
appendlog $id, $audio, $video, $vwidth, $vheight, $fps, $duration, $sha;
|
|
|
|
|
2007-12-29 04:23:11 +00:00
|
|
|
$filesize = -s "$root/videos/$id";
|
|
|
|
|
2007-12-18 00:22:51 +00:00
|
|
|
#add video to videos table
|
|
|
|
$dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator,
|
2007-12-22 12:30:11 +00:00
|
|
|
subject, source, language, coverage, rights, license, ?, ?, ?, ?, ?, ?, 0, 0
|
2007-12-18 00:22:51 +00:00
|
|
|
from uploaded where id = ?}, undef, $filesize, $duration, $vwidth,
|
|
|
|
$vheight, $fps, $sha, $id) or interrupt $dbh->errstr;
|
|
|
|
|
|
|
|
#delete temp file
|
2008-01-05 15:11:56 +00:00
|
|
|
#TODO: use /tmp you insensitive clod !
|
2007-12-18 00:22:51 +00:00
|
|
|
unlink "$root/tmp/$id";
|
|
|
|
}
|
|
|
|
|
|
|
|
#delete from uploaded table
|
|
|
|
$dbh->do(qq{delete from uploaded where id = ?}, undef, $id) or interrupt $dbh->errstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-05 15:11:56 +00:00
|
|
|
TODO: maybe make this event-driven by using the kernels has-this-file-changed-interface ?
|
2007-12-18 00:22:51 +00:00
|
|
|
sleep 10;
|
|
|
|
}
|
|
|
|
}
|