Community Documentation

Sample PHP script to create thumbnails for videos that don't have them

Last updated January 13, 2009. Created by mwheinz on January 13, 2009.
Log in to edit this page.

Because my hosting company has tight limits on uploading data via HTTP, I have to upload my videos via FTP and then use the URL video type to display them. The downside is that they don't get thumbnails that way, so I wrote this php script to create them.

I hope you find it useful!

<?php
// Copy this file to the root directory of your drupal site. Note you may have to tweak the options.
//
// Usage: php make_thumbs.php h t t p ://mysite.com/make_thumbs.php
//
// Set these for your site.
$home = "/home/mysite";
$ffmpeg = $home . "/bin/ffmpeg";
$tmp = $home . "/tmp";
$options = "-an -y -f mjpeg -ss 2 -s 160x92 -vframes 1 ";

if (isset(
$_SERVER['argv'][1])) {
 
$url = parse_url($_SERVER['argv'][1]);
 
$_SERVER['SCRIPT_NAME'] = $url['path'];
 
$_SERVER['HTTP_HOST'] = $url['host'];
}

include_once
'./includes/bootstrap.inc';
// disable error reporting for bootstrap process
error_reporting(E_ERROR);
// let's bootstrap: we will be able to use drupal apis
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// enable full error reporting again
error_reporting(E_ALL);

if(!empty(
$_SERVER['REQUEST_METHOD'])) {
  print
t('This script is only executable from the command line.');
  die();
}

$result = db_query("SELECT * from {video}");
while (
$video = db_fetch_object($result)) {
       
$vnid = $video->nid;
       
$data = unserialize($video->serialized_data);
        if (
is_null($data['iid']) | $data['iid'] == "") {
               
$thumbpath = $tmp . "/" . $video->nid . ".jpg";
                print
t("Generating thumbnail for " . $vnid . ".\n");
               
//print t($ffmpeg . " -i " . $video->vidfile . " " . $options . " " . $thumbpath ."\n");
               
exec($ffmpeg . " -i " . $video->vidfile . " " . $options . " " . $thumbpath, $output);
               
print_r($output);
               
$img = image_create_node_from($thumbpath);
               
$imgnid = $img->nid;
               
$data['iid'] = $imgnid;
               
$data = serialize($data);
               
db_query("UPDATE {video} SET serialized_data = '%s' WHERE nid = %d", $data, $vnid);
        } else {
                print
t("Skipping " . $vnid . ": iid is " . $data['iid'] . "\n");
        }
}
?>

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.