diff options
author | Fabio Varesano <fax8@13637.no-reply.drupal.org> | 2006-11-12 18:34:28 +0000 |
---|---|---|
committer | Fabio Varesano <fax8@13637.no-reply.drupal.org> | 2006-11-12 18:34:28 +0000 |
commit | 1965e6716fe93bdcd14d0d0a24bd8d39fd67dddb (patch) | |
tree | 08fa8e08251ce29c9b3c1dd8ee498966506077e1 | |
parent | e31b60659bd95b9978328bf43a02f34941b6acca (diff) | |
download | video-1965e6716fe93bdcd14d0d0a24bd8d39fd67dddb.tar.gz video-1965e6716fe93bdcd14d0d0a24bd8d39fd67dddb.tar.bz2 |
Patch #82954 by vhmauery: video_upload automatic thumbnailing
Add automatic generation of video thumbnails using ffmpeg
-rw-r--r-- | plugins/video_image/video_image.module | 241 |
1 files changed, 211 insertions, 30 deletions
diff --git a/plugins/video_image/video_image.module b/plugins/video_image/video_image.module index 18c249b..d8c5496 100644 --- a/plugins/video_image/video_image.module +++ b/plugins/video_image/video_image.module @@ -19,31 +19,125 @@ function video_image_help($section) { } } +/** + * Implementation of hook_menu() + */ +function video_image_menu($may_cache) { + $items = array(); + if ($may_cache) { + $items[] = array( + 'path' => 'admin/content/video/image', + 'title' => t('video image'), + 'description' => t('administer video_image module settings'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('video_image_admin_settings'), + 'access' => user_access('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + ); + } + return $items; +} + +/** + * Validation for settings form + */ +function video_image_admin_settings_validate($form_id, &$form_values, &$form) { + if ($form_values['video_image_auto_thumbnail']) { + if (function_exists('is_executable')) { + $test = 'is_executable'; + } else { + $test = 'file_exists'; + } + if (!$test($form_values['video_image_ffmpeg_path'])) { + form_set_error('video_image_ffmpeg_path', t('Set correct path for ffmpeg')); + } + if (!is_numeric($form_values['video_image_auto_thumbnail_seek'])) { + form_set_error('video_image_auto_thumbnail_seek', t('Seek time must be an integer')); + } + } +} + +/** + * Settings form + */ +function video_image_admin_settings() { + if (module_exists('video_upload')) { + if (variable_get('video_image_auto_thumbnail', 0)) { + $upload_weight = db_result(db_query("SELECT weight FROM {system} WHERE name='video_upload'")); + db_query("UPDATE {system} SET weight=".($upload_weight+1)." WHERE name='video_image'"); + } + } + $form = array(); + $form['video_image_publish_thumbnail'] = array( + '#type' => 'checkbox', + '#title' => t('Publish the video thumbnails'), + '#description' => t('Checking this value will cause the video thumbnail image nodes to be published and therefore could show up in blocks. Usually, this is not what you want because then you could end up with both the thumbnail node and the video node showing up and since there is no way to link the image node to the video node, this is not desirable. However, with this unchecked, the administrator will end up with a lot of unpublished nodes.'), + '#default_value' => _video_publish_thumbnails(), + ); + $form['video_image_promote_thumbnail'] = array( + '#type' => 'checkbox', + '#title' => t('Promote the thumbnails to the front page'), + '#default_value' => _video_promote_thumbnails(), + ); + $form['autothumb'] = array( + '#type' => 'fieldset', + '#title' => t('Automatic video thumbnailing'), + ); + $form['autothumb']['video_image_auto_thumbnail'] = array( + '#type' => 'checkbox', + '#title' => t('Auto thumbnail for videos (using ffmpeg)'), + '#description' => t('This requires setting the path to the ffmpeg executable below. If set up correctly, this will auto-generate a thumbnail for each video created.'), + '#default_value' => variable_get('video_image_auto_thumbnail', false), + ); + $form['autothumb']['video_image_auto_thumbnail_only'] = array( + '#type' => 'checkbox', + '#title' => t('Use auto-thumbnailer exclusively for video images'), + '#description' => t('If checked, this will disable the file upload box for the user-supplied thumbnail. Only check this if you have checked to be sure that auto-thumbnailing works. Auto thumbnail must be selected for this to be enabled.'), + '#default_value' => variable_get('video_image_auto_thumbnail_only', false), + '#disabled' => !variable_get('video_image_auto_thumbnail', false), + ); + $form['autothumb']['video_image_auto_thumbnail_seek'] = array( + '#type' => 'textfield', + '#title' => t('Video seek offset for thumbnail'), + '#description' => t('Time in seconds to seek into video before extracting the thumbnail'), + '#default_value' => variable_get('video_image_auto_thumbnail_seek', 2), + ); + $form['autothumb']['video_image_ffmpeg_path'] = array( + '#type' => 'textfield', + '#title' => t('ffmpeg executable path'), + '#description' => t('Set the full path to the ffmpeg executable here to enable automatic thumbnailing of videos'), + '#default_value' => variable_get('video_image_ffmpeg_path', '/usr/bin/ffmpeg'), + ); + $form['autothumb']['video_image_auto_thumbnail_debug'] = array( + '#type' => 'checkbox', + '#title' => t('Debug auto-thumbnail process'), + '#default_value' => variable_get('video_image_auto_thumbnail_debug', false), + ); + return system_settings_form($form); +} /** * Implementation of hook_form_alter() */ function video_image_form_alter($form_id, &$form) { - if($form_id == 'video_node_form' && isset($form['video'])) { + if($form_id == 'video_node_form') { - $form['image'] = array('#type' => 'fieldset', '#title' => t('Image thumbnails'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -17, '#description' => t('Use this form to upload an image.')); + $node = $form['#node']; + $value = ($node->new_image) ? '#value' : '#default_value'; + $form['iid'] = array('#type' => 'hidden', $value => $node->iid); + if (function_exists('_image_check_settings')) { + _image_check_settings(); + $form['#attributes'] = array("enctype" => "multipart/form-data"); - if (function_exists('_image_check_settings')) { - _image_check_settings(); - $node = $form['#node']; - $form['#attributes'] = array("enctype" => "multipart/form-data"); + if (!variable_get('video_image_auto_thumbnail_only', false)) { + $form['image'] = array('#type' => 'fieldset', '#title' => t('Image thumbnails'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -17, '#description' => t('Use this form to upload an image.')); - if ($node->iid) { - $image = node_load($node->iid); - $form['image']['image_thumbnail'] = array('#type' => 'item', '#title' => t('Thumbnail'), '#value' => image_display($image, 'thumbnail')); - } - $value = ($node->new_image) ? '#value' : '#default_value'; - $form['image']['iid'] = array('#type' => 'hidden' , $value => $node->iid); $form['image']['image'] = array('#type' => 'file', '#title' => t('Image')); - $form['image']['image_title'] = array('#type' => 'textfield', '#title' => t('Image title'), '#default_value' => ''); + $form['image']['image_title'] = array('#type' => 'textfield', '#title' => t('Image title'), '#default_value' => $node->image->image_title); } + } } } @@ -62,44 +156,53 @@ function video_image_nodeapi(&$node, $op, $teaser) { $node->serial_data['iid'] = $node->iid; break; case 'prepare': - // check that image.module is actived - if ( !module_exists("image")) { - db_query("UPDATE {system} SET status = 0 WHERE name ='video_image' AND type = 'module' LIMIT 1"); - drupal_set_message(t('video_image module requires the %module module.<br />To prevent system errors video_image module has been disabled.<br />Please install the image module and then reactivate the video_image module.', array('%module' => l('image', 'http://drupal.org/project/image'))), 'error'); - break; + if (variable_get('video_image_auto_thumbnail_debug', false) && variable_get('video_image_auto_thumbnail', false)) { + drupal_set_message(t('video_image_nodeapi: prepare: ready to thumbnail image')); } - $image->title = $_POST['edit']['image_title']; + $field_name = file_check_upload('image'); $image->uid = $node->uid; $image->name = $node->name; $image->created = $node->created; $image->type = 'image'; - image_prepare($image, 'image'); + $image->status = _video_publish_thumbnails(); + $image->promote = _video_promote_thumbnails(); + if (!$field_name && variable_get('video_image_auto_thumbnail', false)) { + $image->title = $_SESSION['video_upload_file']->filename; + $field_name = _video_image_auto_thumbnail($node); + } + else { + $image->title = $_POST['edit']['image_title']; + $field_name = 'image'; + } + image_prepare($image, $field_name); if ($image->images) { node_validate($image); if (!form_get_errors()) { $image = node_submit($image); node_save($image); - $node->iid = $image->nid; + $node->iid = $node->serial_data['iid'] = $image->nid; + $_SESSION['video_upload_file']->iid = $image->nid; $node->new_image = TRUE; } } - elseif ($_POST['edit']['iid']) { - $node->iid = $_POST['edit']['iid']; + else if (isset($_SESSION['video_upload_file']->iid)) { + $node->iid = $_SESSION['video_upload_file']->iid; } break; case 'view': - if($teaser) { - if ($node->serial_data['image_teaser'] || $node->serial_data['iid']) { //If we are dealing with a teaser. - $node->teaser = theme('video_image_teaser', $node); + if ($node->iid) { + if($teaser) { + $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_teaser', $node)); } - } - else { - if ($node->serial_data['image_view'] || $node->serial_data['iid']) { - $node->body = theme('video_image_body', $node) . $node->body; + else { + $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_body', $node)); } } break; + case 'delete': + node_delete(array('nid' => $node->iid)); + break; } } } @@ -150,4 +253,82 @@ function theme_video_image_body($node) { return $output; } +/* Generates a thumbnail from the video file using ffmpeg + * + * @param $node + * object with node information + * + * @return + * a drupal file object + */ +function _video_image_auto_thumbnail(&$node) { + if(empty($_SESSION['video_upload_file']) || + !$_SESSION['video_upload_file']->newfile || + $node->iid || $_SESSION['video_upload_file']->iid || + $_SESSION['video_upload_file']->thumbnailed) { + if (variable_get('video_image_auto_thumbnail_debug', false)) { + if (empty($_SESSION['video_upload_file'])) + drupal_set_message(t('no video has been uploaded: make sure that video_image weight is greater than video_upload weight; make sure that the video file is not too large to be uploaded.')); + } + return null; + } + $videofile = escapeshellarg($_SESSION['video_upload_file']->filepath); + + $filepath = tempnam(file_directory_temp(), 'ffmpeg-thumb'); + $seek = variable_get('video_image_auto_thumbnail_seek', 2); + $ffmpeg = variable_get('video_image_path_to_ffmpeg', '/usr/bin/ffmpeg'); + $command = "$ffmpeg -i $videofile -y -f mjpeg -ss $seek -vframes 1 -an $filepath"; + if (variable_get('video_image_auto_thumbnail_debug', false)) { + drupal_set_message(t('ffmpeg command: ').$command); + } + $ffmpeg_output = shell_exec($command." 2>&1"); + if (variable_get('video_image_auto_thumbnail_debug', false)) { + drupal_set_message(t('ffmpeg output: ').$ffmpeg_output); + } + if (!file_exists($filepath)) { + drupal_set_message(t('video_image_auto_thumbnail: file %file does not exist', array('%file' => $filepath)), 'error'); + } + $file = array( + 'filename' => $_SESSION['video_upload_file']->filename . ".video-thumb.jpg", + 'filemime' => 'image/jpeg', + 'filesize' => filesize($filepath), + 'filepath' => $filepath, + 'nid' => $node->nid, + ); + $_SESSION['video_upload_file']->thumbnailed = TRUE; + if (variable_get('video_image_auto_thumbnail_debug', false)) { + drupal_set_message(t('Successfully thumbnailed video')); + } + return (object)$file; +} + +/* If the user has set a promote preference, use that, otherwise return + * if 'promote' is set in the drupal content type settings + * + * @return + * Returns whether we should promote thumbnails or not + */ +function _video_promote_thumbnails() { + $settings_override = variable_get('video_image_promote_thumbnail', NULL); + if ($settings_override === NULL) { + $node_options = variable_get('node_options_image', array('status', 'promote')); + return in_array('promote', $node_options); + } + return $settings_override; +} + +/* If the user has set a publish preference, use that, otherwise return + * if 'status' is set in the drupal content type settings + * + * @return + * Returns whether we should publish thumbnails or not + */ +function _video_publish_thumbnails() { + $settings_override = variable_get('video_image_publish_thumbnail', NULL); + if ($settings_override === NULL) { + $node_options = variable_get('node_options_image', array('status', 'promote')); + return in_array('status', $node_options); + } + return $settings_override; +} ?> |