-
-
Save digitalhitler/9665d5fcc877a0a60475ab5873874605 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
<?php | |
// Run from the command line, so we'll grab arguments from there | |
// input arguments: 0=>script, 1=>file_name, 2=>temp_file | |
$file_name = $argv[1]; | |
$temp_file = $argv[2]; | |
$uploads = '/opt/lampp/htdocs/user_uploads/'; | |
// Set up file location strings | |
$save_to = $uploads.'video/'.$file_name.'.mp4'; | |
$save_thumb_to = $uploads.'image/200/'.$file_name.'.jpg'; | |
$save_large_thumb_to = $uploads.'image/800/'.$file_name.'.jpg'; | |
// first we need to extract at least one thumbnail. | |
$command = "ffmpeg -i ".$temp_file." -an -ss 00:00:03 -an -r 1 -vframes 1 -y -vf scale='-1:min(200\, iw*3/2)' ".$save_thumb_to.' 2>&1 >> /opt/lampp/logs/ffmpeg_log.log'; | |
exec($command); | |
// Let's make a larger thumbnail too | |
$command = "ffmpeg -i ".$temp_file." -an -ss 00:00:03 -an -r 1 -vframes 1 -y -vf scale='-1:min(800\, iw*3/2)' ".$save_large_thumb_to.' 2>&1 >> /opt/lampp/logs/ffmpeg_log.log'; | |
exec($command); | |
// ffmpeg command to convert video | |
// MP4 | |
$command = "ffmpeg -i ".$temp_file." -y -vf scale='min(900\, iw*3/2)':-1 -vcodec libx264 -coder 1 -bf 16 -refs 1 -flags2 +dct8x8 -acodec libfaac -ac 2 -ar 48000 ".$save_to." 2>&1 >> /opt/lampp/logs/ffmpeg_log.log"; | |
exec($command); // 2>&1 redirect STDERR to STDOUT and send to log. | |
// Now we need to run the mp4 we just made through qt-faststart to move the moov metadata to the start of the file to allow browsers to start playing the content before the whole file is downloaded. | |
$command = "/opt/lampp/background_scripts/qtfaststart.py $save_to"; | |
exec($command); | |
// Set the correct permissions for the new file | |
$command = "chmod 744 $save_to"; | |
exec($command); | |
// Grab the dimensions using mediainfo | |
$command = "/usr/bin/mediainfo $save_to --Output=XML"; | |
exec($command, $output); | |
// Load the output from mediainfo | |
$xml = new SimpleXMLElement(implode("\n",$output)); | |
// Select the width and height of the video | |
$video = $xml->File->track[1]; | |
$width = (int) str_replace(array('pixels',' '),'',$video->Width); | |
$height = (int) str_replace(array('pixels',' '),'',$video->Height); | |
// Save the dimensions to the media table | |
// connect to the database | |
try { | |
$dbh = new PDO("mysql:host=localhost;dbname=database_name", 'user_name', 'password'); | |
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
} | |
catch(PDOException $e) { | |
$e->getMessage(); | |
} | |
// We can detect if any error occurred during processing by checking for the videos dimensions | |
if( ! isset($xml->File->track[1]) ){ | |
$sql = " | |
UPDATE `database`.`table` | |
SET `error` = :error | |
WHERE `file_name` = :file_name; | |
"; | |
$sth = $dbh->prepare($sql); | |
$sth->bindParam(':error', '1'); | |
$sth->bindParam(':file_name', $file_name); | |
$sth->execute(); | |
die(); | |
} | |
// Select the width and height of the video | |
$video = $xml->File->track[1]; | |
$width = (int) str_replace(array('pixels',' '),'',$video->Width); | |
$height = (int) str_replace(array('pixels',' '),'',$video->Height); | |
$sql = " | |
UPDATE `database`.`table` | |
SET `width` = :width, `height` = :height | |
WHERE `file_name` = :file_name; | |
"; | |
$sth = $dbh->prepare($sql); | |
$sth->bindParam(':width', $width); | |
$sth->bindParam(':height', $height); | |
$sth->bindParam(':file_name', $file_name); | |
$sth->execute(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment