function video_thumbnails_cron() { // Query for all nodes that have the uploaded video paragraph type and do not yet have a thumbnail set. $query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'node') ->fieldCondition('field_mg_paragraph', 'paragraphs_type', 'mg_parag_video') ->fieldIsNull('field_mg_field_image'); $result = $query->execute(); if (!empty($result['node'])) { $nids = array_keys($result['node']); $batch_size = 5; // Set the batch size to process 5 nodes at a time. $total_batches = ceil(count($nids) / $batch_size); for ($batch = 0; $batch < $total_batches; $batch++) { $offset = $batch * $batch_size; $batch_nids = array_slice($nids, $offset, $batch_size); $batch_operations = array(); foreach ($batch_nids as $nid) { $batch_operations[] = array('video_thumbnails_process_node', array($nid)); } $batch = array( 'operations' => $batch_operations, 'finished' => 'video_thumbnails_batch_finished', 'title' => t('Processing video thumbnails'), 'init_message' => t('Starting processing video thumbnails.'), 'progress_message' => t('Processed @current out of @total nodes.'), 'error_message' => t('An error occurred while processing video thumbnails.'), 'progressive' => FALSE, ); batch_set($batch); } } } function video_thumbnails_process_node($nid, &$context) { // Get the node object. $node = node_load($nid); // Get the uploaded video paragraph from the node. $paragraphs = field_get_items('node', $node, 'field_mg_paragraph'); $mg_parag_video_paragraph = null; foreach ($paragraphs as $paragraph) { if ($paragraph['paragraphs_type'] == 'mg_parag_video') { $mg_parag_video_paragraph = $paragraph; break; } } if (!$mg_parag_video_paragraph) { return; } // Get the file path for the uploaded video. $video_file = file_load($mg_parag_video_paragraph['file_id']); $video_file_path = $video_file->uri; // Generate a thumbnail image for the video using FFMPEG. $thumbnail_file_path = // Use FFMPEG to generate thumbnail image for $video_file_path. // Save the thumbnail image as a file in Drupal's public files directory. $thumbnail_file = file_save_data(file_get_contents($thumbnail_file_path), 'public://' . basename($thumbnail_file_path)); if ($thumbnail_file) { // Set the mg_field_image field for the node to point to the generated thumbnail file. $node->field_mg_field_image[LANGUAGE_NONE][0]['fid'] = $thumbnail_file->fid; field_attach_update('node', $node); } $context['results'][] = $nid; } function video_thumbnails_batch_finished($success, $results, $operations) { if ($success) { $message = t('Video thumbnails processing has completed successfully.'); drupal_set_message($message); } else { $message = t('Video thumbnails processing has completed with errors.'); drupal_set_message($message, 'error'); } }