Skip to content

Instantly share code, notes, and snippets.

@Medalink
Created October 1, 2012 16:17
Show Gist options
  • Save Medalink/3812783 to your computer and use it in GitHub Desktop.
Save Medalink/3812783 to your computer and use it in GitHub Desktop.
SMIL Generator
<pre>
SMIL Information:
<?php
// RTMP URL
define( "RTMP_URL", "rtmp://glc.us.com/vod" );
// Video Directory.
//define( "VIDEO_DIR", realpath( dirname( __FILE__ ) . "/glcmedia/video/" ) );
// Manual
define( "VIDEO_DIR", realpath( "/Server/www/GLC/glc.us.com/trunk/glcmedia/video" ) );
//define( "VIDEO_DIR", realpath( "/Renders/mp4" ) );
// Logs var
$logs = array();
// Usefull functions.
function logit( $msg ) {
global $logs;
array_push( $logs, $msg );
return;
}
// This funtion also fixes issues with naming conventions.
function get_dir_tree( $source_dir, $filters = array() ) {
$dirs = array_diff( scandir( $source_dir ), array_merge( array( ".", "..", ".dat", ".xej" ), $filters ) );
$dir_array = array();
foreach( $dirs as $d ) {
if( preg_match( "/(\.dat|\.xej)/", $d ) )
break;
// Fix bad habbits of windows users!
if( preg_match( "/[ ]/", $d ) )
{
$new = str_replace( " ", "_", $d );
@rename( $source_dir . DIRECTORY_SEPARATOR . $d, strtolower( $source_dir . DIRECTORY_SEPARATOR . $new ) );
logit( "SYS::RENAME::DIR => " . $d . " to " . strtolower( $new ) . PHP_EOL );
$d = $new;
}
// Remove _thumb.extension and _bitrate.jpg stuff.
if( preg_match( "/(thumb|1400|1000|650|350)\.jpg/", $d ) )
{
$new = str_replace( "_thumb", "", $d );
$new = str_replace( "_1400.", ".", $new );
$new = str_replace( "_1000.", ".", $new );
$new = str_replace( "_650.", ".", $new );
$new = str_replace( "_350.", ".", $new );
$new = str_replace( "_iphone_", "_", $new );
@rename( $source_dir . DIRECTORY_SEPARATOR . $d, $source_dir . DIRECTORY_SEPARATOR . $new );
logit( "SYS::RENAME::JPG => " . $d . " to " . $new . PHP_EOL );
$d = $new;
}
// Force lowercase only.
@rename( $source_dir . DIRECTORY_SEPARATOR . $d, strtolower( $source_dir . DIRECTORY_SEPARATOR . $d ) );
$dir_array[$d] = is_dir( $source_dir . DIRECTORY_SEPARATOR . $d ) ? get_dir_tree( $source_dir . DIRECTORY_SEPARATOR . $d, $filters ) : $dir_array[$d] = $d;
}
return $dir_array;
}
// Build our directory array.
$aVideos = get_dir_tree( VIDEO_DIR );
// Copy/Paste it:
$aSourceVids = $aVideos;
$s = "_";
$count = 0;
$bitrates = array( "1400", "1000", "650", "350" );
$ibitrates = array( "470", "270", "170" );
// Connect to the DB and pull all show data.
$mysqli = new mysqli( "localhost", "root", "pass", "table", 3306 );
$q = "SELECT `show_id`,`name` FROM `vod_show`;";
if( $result = $mysqli->query( $q ) ) {
// Pull into assoc array
while( $row = $result->fetch_assoc() ) {
$vod_show_cache[] = $row;
}
$result->free();
}
$sql = "";
// Now we loop and build our SMIL data.
foreach( $aVideos as $name => $language )
{
// Loop our DB data and match the name to the vod_id.
$parts = explode( "_", $name );
if( count( $parts ) > 1 )
$simplename = $parts[0] . " " . $parts[1];
else
$simplename = $parts[0];
foreach( $vod_show_cache as $showdata ) {
if( strtolower( $showdata["name"] ) == $simplename )
{ // Match
$vod_show_id = $showdata["show_id"];
break;
}
else
$vod_show_id = 0;
}
foreach( $language as $episode )
{
foreach( $episode as $number => $data)
{
$file_dir = VIDEO_DIR . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . "en" . DIRECTORY_SEPARATOR . $number . DIRECTORY_SEPARATOR;
// SMIL Data
$smil_header = "<smil>" . PHP_EOL . " <head> " . PHP_EOL . " <meta base=\"" . RTMP_URL . "\" />" . PHP_EOL;
$smil_header .= " </head>" . PHP_EOL . " <body>" . PHP_EOL . " <switch>" . PHP_EOL;
$smil_video = NULL;
$video = NULL;
// iPhone video specific data.
$smil_ivideo = NULL;
$ivideo = NULL;
$invalid_smil = true;
$sql_smil = NULL;
// Handle each video
$i = 1;
$j = 1;
foreach( $data as $key => $value )
{
// Do not leave old data.
$video = NULL;
$ivideo = NULL;
if( preg_match( "/iphone_(470|270|170)\.mp4/", $value ) )
{
$parts = explode( "_", $value );
$rate = 0;
foreach( $parts as $field => $block )
{
$tmp = str_replace( ".mp4", "", $block );
if( in_array( $tmp, $ibitrates ) )
$rate = $tmp;
}
if( $rate != 0 )
{
$j++;
$new = $name . $s . $number . $s . $rate . ".mp4";
$ivideo = " <video src=\"mp4:/" . $name . "/en/" . $number . "/" . $value . "\" system-bitrate=\"" . $rate . "\" />" . PHP_EOL;
}
$invalid_smil = false;
}
elseif( preg_match( "/\.mp4/", $value ) )
{
$parts = explode( "_", $value );
$rate = 0;
foreach( $parts as $field => $block )
{
$tmp = str_replace( ".mp4", "", $block );
if( in_array( $tmp, $bitrates ) )
$rate = $tmp;
}
if( $rate != 0 )
{
$i++;
$new = $name . $s . $number . $s . $rate . ".mp4";
$video = " <video src=\"mp4:/" . $name . "/en/" . $number . "/" . $value . "\" system-bitrate=\"" . $rate . "\" />" . PHP_EOL;
}
$invalid_smil = false;
}
elseif( preg_match( "/\.jpg/", $value ) )
{
$i--;
$j--;
$new = $name . $s . $number . ".jpg";
if( strlen( $value ) != strlen( $new ) )
{
rename( $file_dir . $value, $file_dir . $new );
logit( "SYS::RENAME::JPG => " . $value . " -> " . $new . PHP_EOL );
$value = $new;
}
}
$smil_ivideo .= $ivideo;
$smil_video .= $video;
}
if( !$invalid_smil )
{
$sql .= ( "REPLACE INTO `vod_file` (`filename`,`type`,`date_added`,`vod_show_id`,`is_enabled`) VALUES('" . $name . $s . $number . "','.smil',NOW()," . $vod_show_id . ",2); <br>" );
$smil_footer = " </switch>" . PHP_EOL . " </body>" . PHP_EOL . "</smil>" . PHP_EOL;
//logit( "SYS::OUTPUT_DIR => " . $file_dir . PHP_EOL );
$file_name = $name . $s . $number . ".smil";
$handle = fopen( $file_dir . $file_name, "w" );
fwrite( $handle, $smil_header . $smil_video . $smil_footer );
fclose( $handle );
$count++;
logit( "SYS::WRITE::SMIL => " . $file_name . " | videos -> " . $i . PHP_EOL );
// iPhone videos.
if( !empty( $smil_ivideo ) )
{
$file_name = $name . $s . $number . "_iphone.smil";
$handle = fopen( $file_dir . $file_name, "w" );
fwrite( $handle, $smil_header . $smil_ivideo . $smil_footer );
fclose( $handle );
logit( "SYS::WRITE::SMIL => " . $file_name . " | videos -> " . $j . PHP_EOL );
}
}
$i = 0;
}
logit( PHP_EOL );
}
}
print"Total: " . $count . "<br>---<br>";
print_r( implode( $logs ) );
print_r( $sql );
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment