You are here

function _video_ffmpeg_helper_auto_thumbnail in Video 6

Same name and namespace in other branches
  1. 5 plugins/video_ffmpeg_helper/video_ffmpeg_helper.module \_video_ffmpeg_helper_auto_thumbnail()
  2. 6.2 plugins/video_ffmpeg_helper/video_ffmpeg_helper.module \_video_ffmpeg_helper_auto_thumbnail()

Generates a thumbnail from the video file

Parameters

$node: object with node information

Return value

a drupal file object

1 call to _video_ffmpeg_helper_auto_thumbnail()
video_upload_v_auto_thumbnail in types/video_upload/video_upload.module
Implements the hook_v_auto_thumnail

File

plugins/video_ffmpeg_helper/video_ffmpeg_helper.module, line 461
Provide some api for use ffmpeg. Simplify video nodes creation.

Code

function _video_ffmpeg_helper_auto_thumbnail(&$node) {
  if (!$node->new_video_upload_file || $node->new_video_upload_file && count($_POST) && $_POST['new_video_upload_file_fid']) {

    // we have already thumbnailed this new upload file
    return NULL;
  }
  if (!$node->new_video_upload_file && $node->current_video_upload_file_fid) {

    // no new files uploaded. skipping thumnailing stuff
    _video_image_thumbnail_debug(t('No new files to thumbnail'));
    return NULL;
  }

  // gets the newly uploaded file object
  $uploaded_file = $node->new_video_upload_file;

  // are we debugging?
  // escape the filename for safety
  $videofile = escapeshellarg($uploaded_file->filepath);

  // let's create a temp filename into the drupal temp directory
  $thumbfile = tempnam(file_directory_temp(), 'tnail-thumb');

  // get ffmpeg configurations
  $seek = variable_get('video_ffmpeg_helper_auto_thumbnail_seek', 2);
  $tnail = variable_get('video_ffmpeg_helper_ffmpeg_path', '/usr/bin/ffmpeg');
  $options = preg_replace(array(
    '/%videofile/',
    '/%thumbfile/',
    '/%seek/',
  ), array(
    $videofile,
    $thumbfile,
    $seek,
  ), variable_get('video_image_thumbnailer_options', '-i %videofile -an -y -f mjpeg -ss %seek -vframes 1 %thumbfile'));

  // executes the command
  $command = "{$tnail} {$options}";
  ob_start();
  passthru($command . " 2>&1", $tnail_return);
  $tnail_output = ob_get_contents();
  ob_end_clean();
  _video_ffmpeg_helper_get_video_info($node, $tnail_output);
  _video_image_thumbnail_debug(t('Thumbnailer command: ') . $command);
  _video_image_thumbnail_debug(t('Thumbnailer output: ') . "<pre>\n{$tnail_output}\n</pre>");
  if (!file_exists($thumbfile)) {
    $error_param = array(
      '%file' => $thumbfile,
      '%cmd' => $command,
      '%out' => $tnail_output,
    );
    $error_msg = t("error generating thumbnail for video: generated file %file does not exist.<br />Command Executed:<br />%cmd<br />Command Output:<br />%out", $error_param);

    // let's log this
    watchdog('video_ffmpeg_helper', $error_msg);
    return false;
  }
  $file = array(
    'filename' => $uploaded_file->filename . ".video-thumb.jpg",
    'filemime' => 'image/jpeg',
    'filesize' => filesize($thumbfile),
    'filepath' => $thumbfile,
    'nid' => $node->nid,
  );
  if ($tnail_return) {
    _video_image_thumbnail_debug(t('Failed to thumbnail video'));
    return $false;
  }
  _video_image_thumbnail_debug(t('Successfully thumbnailed video'));
  return (object) $file;
}