You are here

function ffmpeg_wrapper_auto_thumbnail in Video 6.3

Generates a thumbnail from the video file Implementing hook_auto_thumbnail on inc

Parameters

$vidfile: object with element information

Return value

a drupal file objects

File

plugins/ffmpeg_wrapper.inc, line 127
Provide a api for video conversion and auto thumbnailing using ffmpeg.

Code

function ffmpeg_wrapper_auto_thumbnail($vidfile) {
  global $user;
  $uploaded_file = $vidfile;
  $fid = $uploaded_file["fid"];

  // are we debugging?
  // escape the filename for safety
  $videofile = escapeshellarg($uploaded_file['filepath']);
  $thumb_path = variable_get('video_thumb_path', 'video_thumbs');

  //files will save in files/video_thumbs/#fileId folder
  $tmp = file_directory_path() . '/' . $thumb_path . '/' . $fid;

  // Ensure the destination directory exists and is writable.
  $directories = explode('/', $tmp);

  //  array_pop($directories); // Remove the file itself.
  // Get the file system directory.
  $file_system = file_directory_path();
  foreach ($directories as $directory) {
    $full_path = isset($full_path) ? $full_path . '/' . $directory : $directory;

    // Don't check directories outside the file system path.
    if (strpos($full_path, $file_system) === 0) {
      field_file_check_directory($full_path, FILE_CREATE_DIRECTORY);
    }
  }
  $count = variable_get('no_of_video_thumbs', 5);

  // set file path
  $filepath = $vidfile['filepath'];

  // calling ffmpeg_wrapper_file_data function
  $file_data = ffmpeg_wrapper_file_data($filepath);
  $duration = $file_data['duration'];
  $files = NULL;
  for ($i = 1; $i <= $count; $i++) {

    // get ffmpeg configurations
    $seek = $duration / $count * $i;
    $thumbfile = $tmp . "/video-thumb-for-{$fid}-{$i}.png";

    //skip files already exists, this will save ffmpeg traffic
    if (!is_file($thumbfile)) {

      //      $tnail = variable_get('video_transcoder_path', '/usr/bin/ffmpeg');
      $options = preg_replace(array(
        '/%videofile/',
        '/%thumbfile/',
        '/%seek/',
      ), array(
        $videofile,
        $thumbfile,
        $seek,
      ), variable_get('video_ffmpeg_thumbnailer_options', '-i %videofile -an -y -f mjpeg -ss %seek -vframes 1 %thumbfile'));

      // executes the command
      $tnail_output = ffmpeg_wrapper_run_command($options, $error_check = true, $path = '');

      //      $command = "$tnail $options";
      //      ob_start();
      //      passthru($command." 2>&1", $tnail_return);
      //      $tnail_output = ob_get_contents();
      //      ob_end_clean();
      if (!file_exists($thumbfile)) {
        $error_param = array(
          '%file' => $thumbfile,
          '%cmd' => $options,
          '%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', $error_msg);
      }
    }

    // Begin building file object.

    //TODO : use file_munge_filename()
    $file = new stdClass();
    $file->uid = $user->uid;
    $file->status = FILE_STATUS_TEMPORARY;
    $file->filename = trim("video-thumb-for-{$fid}-{$i}.png");
    $file->filepath = $thumbfile;
    $file->filemime = file_get_mimetype("video-thumb-for-{$fid}-{$i}.png");
    $file->filesize = filesize($thumbfile);
    $file->timestamp = time();
    $files[] = $file;
  }
  return $files;
}