Skip to content

Instantly share code, notes, and snippets.

@raytiley
Created February 11, 2011 16:38
Show Gist options
  • Save raytiley/822608 to your computer and use it in GitHub Desktop.
Save raytiley/822608 to your computer and use it in GitHub Desktop.
Transcodes files in a directory to mp4 using handbrakeCLI.
<?php
//Setup Content drives and Destination path
$contentPaths = array("/Volumes/VS400/", "/Volumes/SXServer/", "/Volumes/SXSafe/");
$destinationPath = "/Volumes/VODContent/mp4/";
$transcodeCommand = "HandBrakeCLI --preset \"iPhone & iPod Touch\" --width 320 --vb 500 --two-pass --turbo --optimize ";
//pid class used to determine if script is already running.
class pid {
protected $filename;
public $already_running = false;
function __construct($directory) {
$this->filename = $directory . '/' . basename($_SERVER['PHP_SELF']) . '.pid';
if(is_writable($this->filename) || is_writable($directory)) {
if(file_exists($this->filename)) {
$pid = (int)trim(file_get_contents($this->filename));
if(posix_kill($pid, 0)) {
$this->already_running = true;
}
}
}
else {
die("Cannot write to pid file '$this->filename'. Program execution halted.\n");
}
if(!$this->already_running) {
$pid = getmypid();
file_put_contents($this->filename, $pid);
}
}
public function __destruct() {
if(!$this->already_running && file_exists($this->filename) && is_writeable($this->filename)) {
unlink($this->filename);
}
}
}
//Define some functions to do various tasks
function dirList ($directory)
{
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = opendir($directory);
// keep going until all files in directory have been read
while ($file = readdir($handler)) {
// if $file isn't this directory or its parent,
// add it to the results array
if ($file != '.' && $file != '..')
$results[] = $file;
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
function cleanFileName($fileName) {
$cleanName = str_replace(" ", "\ ", $fileName);
$cleanName = str_replace("(", "\(", $cleanName);
$cleanName = str_replace(")", "\)", $cleanName);
$cleanName = str_replace("'", "\'", $cleanName);
return $cleanName;
}
function transcodeLog($msg)
{
print $msg."\n\n";
// open file
$fd = fopen("/usr/local/cablecastHelper/".date("Y-m-d").".log", "a");
// write string
fwrite($fd, $msg . "\n");
// close file
fclose($fd);
}
//If this script is already running Exit
$pid = new pid('/tmp');
if($pid->already_running) {
exit;
}
else
{
/*******************************************
* File Finding
* Currently this finds valid cablecast shows.
* Change to fit your needs
*******************************************/
$filesToTranscode = array();
foreach($contentPaths as $dir)
{
$files = dirList($dir);
foreach($files as $file) {
if(preg_match('/(^\b[0-9]+).*\.(mpg|mpeg|MPG|MPEG)/', $file, $matches))
{
if(!file_exists($destinationPath.$matches[1])) {
$filesToTranscode[] = array("showID" => $matches[1], "path" => cleanFileName($dir.$file));
}
}
}
}
/*******************************************
* Transcoding Files
* This is where the magic happens
*******************************************/
foreach($filesToTranscode as $job)
{
transcodeLog(date("h:i:s")." - Transcoding: ".$job["path"]);
$command = $transcodeCommand."--input ".$job["path"]." --output ".$destinationPath.$job["showID"].".mp4";
exec($command, $output, $return);
if(file_exists($destinationPath.$job["showID"].".mp4")) {
transcodeLog(date("h:i:s")." - Successfully transcoded: ".$destinationPath.$job["showID"].".mp4");
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment