You are here

function video_convert_process in Video 6.5

Same name and namespace in other branches
  1. 6.4 video_widget.inc \video_convert_process()

Adds a video to the video rendering table.

If auto converting, it will convert your video to flv right now. We are passing the element by reference just in case we ever want to add more to the element during this process.

1 call to video_convert_process()
video_widget_process in ./video_widget.inc
Video_widget_process for API handlers for any video types.

File

./video_widget.inc, line 230
Common Video module widget functions

Code

function video_convert_process(&$element, $field) {
  $value =& $element['#value'];

  // Set the dimensions when the user has no permission
  if (!user_access('override player dimensions')) {
    $value['data']['dimensions'] = $field['widget']['default_dimensions'];
    $value['data']['player_dimensions'] = $field['widget']['default_player_dimensions'];
  }

  // If the dimensions #value is empty, it is probably because the user
  // clicked the Save button directly and did not choose dimensions.
  // Take the default dimensions.
  $dimensions = NULL;
  if (!empty($value['data']['dimensions'])) {
    $dimensions = $value['data']['dimensions'];
  }
  elseif (!empty($element['data']['dimensions'])) {
    $dimensions = $element['data']['dimensions']['#default_value'];
  }
  if (!empty($value['fid']) && empty($value['data']['bypass_autoconversion'])) {
    $convert = FALSE;
    $fid = intval($value['fid']);

    // Setup our conversion class and check for the fid existence.
    $transcoder = video_get_transcoder();

    // Lets verify that we haven't added this video already.  Multiple validation fails will cause this to be ran more than once
    if (!($video = $transcoder
      ->load_job($fid))) {

      // Get a proper video object
      $file = db_fetch_object(db_query('SELECT f.* FROM {files} f WHERE f.fid = %d', $fid));

      // Move the file to the original folder
      $filedir = dirname($file->filepath);
      $originaldir = $filedir . '/original';
      if (!field_file_check_directory($originaldir, FILE_CREATE_DIRECTORY)) {
        watchdog('video_command', 'Video conversion failed. Could not create directory %dir for storing original videos.', array(
          '%dir' => $originaldir,
        ), WATCHDOG_ERROR);
        return;
      }

      // Lets move our video and then convert it.
      $filepath = $file->filepath;
      if (!file_move($filepath, $originaldir . '/' . $file->filename)) {
        watchdog('video_command', 'Could not move video %orig to the original folder.', array(
          '%orig' => $file->filepath,
        ), WATCHDOG_ERROR);
        return;
      }

      // Update our filepath since we moved it
      $file->filepath = $filepath;
      drupal_write_record('files', $file, 'fid');
      $element['#value']['filepath'] = $filepath;

      // Video has not been added to the queue yet so lets add it.
      $file->dimensions = $dimensions;
      if (!$transcoder
        ->create_job($file)) {
        drupal_set_message(t('Something went wrong with your video job creation.  Please check your recent log entries for further debugging.'), 'error');
      }
      $convert = TRUE;

      // Lets queue our node status to unpublished.
      $element['#unpublish'] = TRUE;
      $video = $transcoder
        ->load_job($fid);
    }
    elseif ($video->status != VIDEO_RENDERING_COMPLETE) {

      // Lets queue our node status to unpublished.
      $element['#unpublish'] = TRUE;
    }

    // Our video should be in the database pending, lets see if we need to convert it now.
    // Check if we are going from unselected to selected or if this is a new video and we have checked the checkbox
    $convert_video_on_save = FALSE;
    $element_data_convert_on_save = '';
    $file_date_convert_on_save = '';
    $convert_on_save = variable_get('video_convert_on_save', FALSE);
    if (isset($element['data']['convert_video_on_save']['#value'])) {
      $element_data_convert_on_save = $element['data']['convert_video_on_save']['#value'];
    }
    if (isset($value['data']['convert_video_on_save'])) {
      $file_date_convert_on_save = $value['data']['convert_video_on_save'];
    }
    $convert_video_on_save = $element_data_convert_on_save || $file_date_convert_on_save;
    if ((!isset($element['#default_value']['data']['convert_video_on_save']) || !$element['#default_value']['data']['convert_video_on_save']) && $convert_video_on_save || $convert && $convert_video_on_save || $convert_on_save) {
      $return = video_process_video($video);
      if ($return === FALSE) {
        drupal_set_message(t('Something went wrong with your video conversion. Please check your recent log entries for further debugging.'), 'error');
      }
      elseif ($return === TRUE) {

        // We are always unpublished until we are converted.
        unset($element['#unpublish']);
        drupal_set_message(t('Successfully converted your video.'));
      }
    }
    elseif ($convert) {
      drupal_set_message(t('Video submission queued for processing. Please wait: our servers are preparing your video for display.'));
    }
  }
}