Index: ffmpeg_converter.module =================================================================== RCS file: /cvs/drupal/contributions/modules/ffmpeg_converter/ffmpeg_converter.module,v retrieving revision 1.39 diff -u -r1.39 ffmpeg_converter.module --- ffmpeg_converter.module 20 Apr 2009 21:11:45 -0000 1.39 +++ ffmpeg_converter.module 19 Oct 2009 22:43:22 -0000 @@ -444,12 +444,26 @@ case 'insert': case 'update': // Look for new files. - if (!empty($original) && empty($converted) && !ffmpeg_converter_file_is_queued($node->nid, $field['field_name'])) { + if (!empty($original) && empty($converted)) { list($field_instance, $file) = array_pop($original); - if (ffmpeg_converter_check_file($file['filename'], $preset_key)) { - // Queue file for processing. - ffmpeg_converter_queue_filefield($node->nid, $field['field_name'], 0, $preset_key); - drupal_set_message(t('The file %file was added to the job queue.', array('%file' => check_plain($file['filename'])))); + $snapshot = FALSE; + $convert = FALSE; + $message = 'The file %file was added to the job queue to'; + if (!ffmpeg_converter_file_is_queued($node->nid, $field['field_name']) && ffmpeg_converter_check_file($file['filename'], $preset_key)) { + $convert = TRUE; + $message .= ' convert the file'; + } + if (!ffmpeg_converter_snapshots_are_queued($node->nid, $field['field_name']) && ffmpeg_converter_check_snapshots($node, $field['field_name'])) { + $snapshot = TRUE; + if ($convert) { + $message .= ' and '; + } + $message .= ' create snapshots of the file'; + } + $message .= '.'; + if ($convert || $snapshot) { + ffmpeg_converter_queue($node->nid, $field['field_name'], 0, $preset_key, $convert, $snapshot); + drupal_set_message(t($message, array('%file' => check_plain($file['filename'])))); } } break; @@ -511,7 +525,6 @@ return FALSE; } - /** * Add a file field to the process queue. * @@ -524,13 +537,22 @@ * $node->{$field_name} array. * @param $preset_key * The key of the FFmpeg Converter preset to use. + * @param $convert + * If TRUE, convert source file to preset. + * @param $snapshot + * If TRUE, create snapshots of source file. * @return * FALSE if Job queue is unavailable, NULL otherwise. */ -function ffmpeg_converter_queue_filefield($nid, $field_name, $field_instance, $preset_key) { +function ffmpeg_converter_queue($nid, $field_name, $field_instance, $preset_key, $convert = FALSE, $snapshot = FALSE) { if (function_exists('job_queue_add')) { - cache_clear_all('ffmpeg_converter_queued_files', 'cache'); - return job_queue_add('ffmpeg_converter_convert_filefield', t('Convert a file field with FFmpeg.'), array($nid, $field_name, $field_instance, $preset_key)); + if ($convert) { + cache_clear_all('ffmpeg_converter_queued_files', 'cache'); + } + if ($snapshot) { + cache_clear_all('ffmpeg_converter_queued_snapshots', 'cache'); + } + return job_queue_add('ffmpeg_converter_convert_queue', t('Convert file and/or create snapshots of a file field with FFmpeg.'), array($nid, $field_name, $field_instance, $preset_key, $convert, $snapshot)); } else { return false; @@ -569,6 +591,36 @@ /** + * Check if a file's snapshots are already in the job queue. + * + * @param $nid + * The nid of the node containing the file. + * @param $field + * The name of the field containing the file. + * @return + * TRUE or FALSE. + */ +function ffmpeg_converter_snapshots_are_queued($nid, $field) { + $cache = cache_get('ffmpeg_converter_queued_snapshots'); + + if ($cache) { + $files = $cache->data; + } + else { + $jobs = db_query("SELECT * FROM {job_queue} WHERE function = 'ffmpeg_converter_snapshots'"); + $files = array(); + while ($job = db_fetch_array($jobs)) { + $args = unserialize($job['arguments']); + $files[] = $args[0] .'-'. $args[1]; + } + cache_set('ffmpeg_converter_queued_snapshots', $files); + } + + return in_array($nid .'-'. $field, $files); +} + + +/** * Add a Drupal file to the process queue. * * @param $fid @@ -586,7 +638,7 @@ } else { return false; - } +} } @@ -611,6 +663,7 @@ /** * Enter description here... * + * * @param $nid * The id of the container node. * @param $field_name @@ -620,16 +673,48 @@ * $node->{$field_name} array. * @param $preset_key * The key of the FFmpeg Converter preset to use. + * @param $convert + * If TRUE, convert source file to preset. + * @param $snapshot + * If TRUE, create snapshots of source file. */ -function ffmpeg_converter_convert_filefield($nid, $field_name, $field_instance, $preset_key) { - +function ffmpeg_converter_convert_queue($nid, $field_name, $field_instance, $preset_key, $convert, $snapshot) { $node = node_load($nid); + + if ($convert && !empty($node)) { + $node = ffmpeg_converter_convert_filefield($node, $field_name, $field_instance, $preset_key); + } + if ($snapshot && ($node || !empty($node))) { + $node = ffmpeg_converter_snapshots($node, $field_name, $field_instance, $preset_key); + } + + // Save the node. + if ($node || !empty($node)) { + node_save($node); + } +} + + +/** + * Enter description here... + * + * @param $node + * The container node. + * @param $field_name + * The name of the field that contains the source file. + * @param $field_instance + * The field instance as an integer representing the key in the + * $node->{$field_name} array. + * @param $preset_key + * The key of the FFmpeg Converter preset to use. + */ +function ffmpeg_converter_convert_filefield($node, $field_name, $field_instance, $preset_key) { // Clear the cached list of queued files. cache_clear_all('ffmpeg_converter_queued_files', 'cache'); // Check the necessary prerequisites. - if (empty($node) || empty($node->{$field_name}[$field_instance]['fid']) || !function_exists('content_fields')) { + if (empty($node->{$field_name}[$field_instance]['fid']) || !function_exists('content_fields')) { return false; } @@ -650,10 +735,10 @@ // Setup paths. $input_file = $node->{$field_name}[$field_instance]['filepath']; $output_file = ffmpeg_converter_filename($input_file, $configuration['ffmpeg_output_type']); - $output_file = file_directory_temp() . '/' . basename($output_file); + $output_file = file_directory_temp() .'/'. basename($output_file); // Run conversion and check result. - $result = ffmpeg_converter_convert($input_file, $output_file, $configuration, l(t('Source node'), 'node/' . $node->nid)); + $result = ffmpeg_converter_convert($input_file, $output_file, $configuration, l(t('Source node'), 'node/'. $node->nid)); if (!$result) { return false; } @@ -665,12 +750,11 @@ $validators = array(); // Create the file object. - $destination_path = filefield_widget_file_path($field); + $destination_path = str_replace('/'. $node->{$field_name}[$field_instance]['filename'], '', $node->{$field_name}[$field_instance]['filepath']); $file = field_file_save_file($output_file, $validators, $destination_path); if (!is_array($file['data'])) { $file['data'] = array(); - } // Copy file description from original to destination file. if (!empty($node->{$field_name}[$field_instance]['data']['description'])) { @@ -688,9 +772,56 @@ // Add the file to the node. $node->{$field_name}[] = $file; - // Try to get video duration and determine if this is a video file. + // Save the node. + $node = node_submit($node); + + watchdog('ffmpeg_converter', 'FFmpeg converted the file %file to @format.', + array('%file' => basename($input_file), '@format' => $configuration['ffmpeg_output_type']), + WATCHDOG_NOTICE, l(t('Source node'), 'node/'. $node->nid)); + + return $node; +} + + +/** + * Enter description here... + * + * @param $node + * The container node. + * @param $field_name + * The name of the field that contains the source file. + * @param $field_instance + * The field instance as an integer representing the key in the + * $node->{$field_name} array. + * @param $preset_key + * The key of the FFmpeg Converter preset to use. + */ +function ffmpeg_converter_snapshots($node, $field_name, $field_instance, $preset_key) { + + cache_clear_all('ffmpeg_converter_queued_snapshots', 'cache'); + + // Check the necessary prerequisites. + if (empty($node->{$field_name}[$field_instance]['fid']) || !function_exists('content_fields')) { + return FALSE; + } + + // Load the .inc pages that we need. + module_load_include('inc', 'node', 'node.pages'); + module_load_include('inc', 'filefield', 'field_file'); + + if (!function_exists('field_file_save_file')) { + return FALSE; + } + + // Get preset configuration. + $configuration = ffmpeg_converter_options($preset_key); + + // Setup paths. + $input_file = $node->{$field_name}[$field_instance]['filepath']; + + // Try to get video duration and determine if this is a vide $duration = ffmpeg_wrapper_file_duration($input_file, false); - + if ($duration) { // Take three snapshot images of the film. @@ -712,7 +843,8 @@ if ($snapshot = ffmpeg_converter_create_snapshot($input_file, file_directory_temp(), $timecode, $size)) { // Create the snapshot file object. - $s_file = field_file_save_file($snapshot, $validators, $destination_path); + $destination_path = str_replace('/'. $node->{$field_name}[$field_instance]['filename'], '', $node->{$field_name}[$field_instance]['filepath']); + $s_file = field_file_save_file($snapshot, array(), $destination_path); if (is_array($s_file)) { @@ -737,14 +869,14 @@ } } - - // Save the node. + $node = node_submit($node); - node_save($node); - - watchdog('ffmpeg_converter','FFmpeg converted the file %file to @format.', - array('%file' => basename($input_file), '@format' => $configuration['ffmpeg_output_type']), - WATCHDOG_NOTICE, l(t('Source node'), 'node/' . $node->nid)); + + watchdog('ffmpeg_converter', 'FFmpeg created the snapshots for %file.', + array('%file' => basename($input_file)), + WATCHDOG_NOTICE, l(t('Source node'), 'node/'. $node->nid)); + + return $node; } @@ -1163,12 +1295,12 @@ $height = ''; // Check field display settings. - if (isset($element['items'][0]['#formatter'])) { - $format = $element['items'][0]['#formatter']; + $field = content_fields($element['#field_name']); + if (arg(0) == 'node' && arg(1) == $nid) { + $format = $field['display_settings']['full']['format']; } else { - $field = content_fields($element['#field_name']); - $format = $field['display_settings']['full']['format']; + $format = $field['display_settings']['teaser']['format']; } $parts = explode('_', $format); $link_style = array_pop($parts);