You are here

function _video_field_file_autoconversion in Video 7.2

Same name and namespace in other branches
  1. 7 video.field.inc \_video_field_file_autoconversion()

Video file save to the video_queue table for conversions

2 calls to _video_field_file_autoconversion()
video_field_insert in ./video.field.inc
Implements hook_field_insert().
video_field_update in ./video.field.inc
Implements hook_field_update().

File

./video.field.inc, line 1284
Implement a video field, based on the file module's file field.

Code

function _video_field_file_autoconversion($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $transcoder = new Transcoder();
  if (empty($field['settings']['autoconversion']) || !$transcoder
    ->hasTranscoder()) {
    return;
  }
  foreach ($items as $delta => &$item) {
    $fid = intval($item['fid']);

    // skip adding entry if bypass conversion is checked
    if (!empty($item['bypass_autoconversion'])) {

      // delete the conversion job if any
      video_jobs::delete($fid);
      return;
    }

    // Try to load the job
    $video = video_jobs::load($fid);

    // Create the job if it doesn't exist
    if (!$video) {
      list($entity_id, $entity_vid, $bundle) = entity_extract_ids($entity_type, $entity);

      // get the default dimension when not available in $item
      $info_instance = field_info_instance('node', $field['field_name'], $bundle);
      if (empty($item['dimensions'])) {
        $item['dimensions'] = $info_instance['settings']['default_dimensions'];
      }
      if (!video_jobs::create($item['fid'], $item['dimensions'], $entity_id, $entity_type, $field['field_name'], $langcode, $delta)) {
        drupal_set_message(t('Something went wrong with your video job creation. Please check your recent log entries for further debugging.'), 'error');
        return;
      }

      // Load a fresh copy of the job
      $video = video_jobs::load($fid);

      // for case that video was not created through the form, we should check if it has a allowed extension and if needed, generate thumbnails.
      $fextension = video_utility::getExtension($video->filename);
      if (strpos($info_instance['settings']['file_extensions'], $fextension) === false) {
        drupal_set_message(t('The extension "' . $fextension . '" of file ' . $video->filename . ' is not allowed for import. Encoding job for ' . $transcoder
          ->getTranscoder()
          ->getName() . ' cannot be created.'), 'error');
        return;
      }

      // if thumbnail(s) already exists or autocreation for thumbnails is disabled, do nothing.
      if (empty($item['thumbnail']) && $field['settings']['autothumbnail'] == 'auto') {
        $thumbs = $transcoder
          ->extractFrames($item, $field);

        // if thumbnails creation fails, we can assume, that something in the file is wrong and encoding will also fail
        if ($thumbs === FALSE) {
          drupal_set_message(t('Thumbnails creation for ' . $video->filename . ' failed. Encoding job for ' . $transcoder
            ->getTranscoder()
            ->getName() . ' cannot be created. Please check your recent log entries for further debugging.'), 'error');
          return;
        }
        else {
          drupal_set_message('Thumbnails successfully created for ' . $video->filename . '.', 'status');
        }
      }
    }

    // re queue for video conversion
    if (!empty($item['re_convert_video'])) {
      $video->video_status = VIDEO_RENDERING_PENDING;
      $video->statusupdated = time();
      $video->dimensions = $item['dimensions'];
      video_jobs::update($video);
    }
    if ($video->video_status == VIDEO_RENDERING_PENDING) {

      // Convert on save if the job is pending and if it is requested
      // The automatic conversions of files is handled in _video_field_convert_on_save(),
      // which is called from hook_entity_insert/hook_entity_update.
      if (!empty($item['convert_video_on_save'])) {

        // Only save the fid, because the location of the file may change
        // between now and _video_field_convert_on_save().
        $entity->video_convert_on_save[$video->fid] = $video->fid;
      }
      else {
        drupal_set_message(t('Transcoding job was successfully queued for %transcoder-name. The video %video-name will be converted soon.', array(
          '%transcoder-name' => $transcoder
            ->getTranscoder()
            ->getName(),
          '%video-name' => $video->filename,
        )));
      }
    }
  }
}