Skip to content

Instantly share code, notes, and snippets.

@elliotwoods
Created October 31, 2011 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotwoods/1329309 to your computer and use it in GitHub Desktop.
Save elliotwoods/1329309 to your computer and use it in GitHub Desktop.
PHP script to auto generate video previews for video files from 5D
<?php
// run this script from command line e.g.
// php compress.php > videos.html
// I really DO NOT suggest you try and load this php url in your browser
$nVideos = 0;
//these 2 variables are used to render an absolute path
//this is important for the the flash flv player
//the base url
$base_url = "http://www.kimchiandchips.com/";
//the path on your server where the base url points to
//we presume that the flv's are somewhere beneath this path
$base_path = "/home/kimchiandchips/kimchiandchips/";
traverse(".");
function traverse($path) {
echo("<h1>$path</h1>\n");
global $allhtml, $thishtml;
$d = dir($path);
while (false !== ($entry = $d->read())) {
$fullpath = $path . "/" . $entry;
if (substr($fullpath,-1) == ".")
continue;
if (is_dir($fullpath))
traverse($fullpath);
if (substr($fullpath, -4) == ".MOV")
compress($fullpath);
}
$d->close();
}
function compress ($path) {
global $nVideos;
$mov = $path;
$flv = substr($path, 0, -3) . "flv";
$jpg = substr($path, 0, -3) . "jpg";
shell_exec("ffmpeg -threads 1 -y -i \"" . $mov . "\" -s 1280x720 -f flv -vcodec flv -b 2000000 -ab 128000 -ar 44100 \"" . $flv . "\"");
$size = 0;
$offset = 2.0;
$runtimes = 0;
while ($size == 0 && $runtimes < 5) {
$command = sprintf("ffmpeg -threads 1 -y -itsoffset -%.1f -i \"" . $mov . "\" -vcodec mjpeg -vframes 1 -s 480x270 \"" . $jpg . "\"", $offset);
shell_exec($command);
$size = filesize($jpg);
$offset /= 2.0;
$runtimes++;
}
$nVideos++;
?>
<h2><? echo $mov; ?></h2>
<div id="v<? echo $nVideos; ?>">
<a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this video.
</div>
<script type="text/javascript" src="https://media.dreamhost.com/mp4/swfobject.js"></script>
<script type="text/javascript">
var swf = new SWFObject("https://media.dreamhost.com/mp4/player.swf", "mpl", "480", "270", 8);
swf.addParam("allowfullscreen", "true");
swf.addParam("allowscriptaccess", "always");
swf.addVariable("file", "<? echo getUrl($flv); ?>");
swf.addVariable("image", "<? echo $jpg ?>");
swf.write("v<? echo $nVideos; ?>");
</script>
<a href="<? echo $mov; ?>">Original</a>
<a href="<? echo $flv; ?>">FLV</a>
<br />
<?
}
function getUrl($path) {
global $base_path, $base_url;
$real = realpath($path);
$url = str_replace($base_path, $base_url, $real);
//$url = str_replace(" ", "%20", $url);
return $url;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment