Video insert api

agilpwc - January 26, 2007 - 06:38
Project:Video
Version:HEAD
Component:Code
Category:task
Priority:normal
Assigned:heshanmw
Status:active
Description

I wrote an api funtion for the video module that inserts videos. Modified similar function from audio module.

This has many practical uses, like mass import, possible use for new cck video field, video egg, etc. Other modules can create videos.

It's basically just one function. I tested it on 5.0 but it should work unchanged on 4.7

AttachmentSize
video_insert_api_0.patch1.71 KB

#1

agilpwc - January 26, 2007 - 06:39

Here is a sample script that uses the new video_insert api.

AttachmentSize
insert.php_.txt 415 bytes

#2

liquidcms - January 29, 2007 - 20:51

very cool.. thanks...

so, anyone done the cck field yet that uses this very impressive video module??? just checking?

#3

liquidcms - January 29, 2007 - 20:51

very cool.. thanks...

so, anyone done the cck field yet that uses this very impressive video module??? just checking.

#4

agilpwc - February 16, 2007 - 15:13

fax8 what is the time frame for getting this looked committed?

#5

fax8 - February 16, 2007 - 17:30
Status:needs review» needs work

Thank for your work

You use an incorrect usage of user_access().

You call it as user_access('create'). It should be user_access('create video');

Moreover it looks like calling drupal_access_denied(); on insufficient permissions is
not the correct approach... If we consider that this feature will be used by scripts or
other modules it's not good to just stop everything telling access denied.

You should use drupal_message and maybe a watchdog message to be sure the
user will get that the video submission is failed because an incorrect permissions.

Moreover I would like you to consider changing the function from

video_api_insert($filepath = '', $title = 'video', $body = '', $width = 425, $height = 350, $video_size = 1, $seconds = 1)

to something easier to use and read such as:
video_api_insert($info = array())

where $info is an associative array containing video information.
The core development team just did a similar change on l() for Drupal 6 http://drupal.org/node/111347.

Another approach could be directly pass a node object.

Fabio

#6

ardee - March 5, 2008 - 23:56

And it would be SOOO great if you could also pass it a second file path, for the thumbnail, and it would do with that whatever the Video module does -- which is to create an Image node and reference its nid!

#7

vhmauery - March 6, 2008 - 14:48
Status:needs work» active

Just so you know, a video insert api function has been added to the 5.x code -- it was based on how the image api worked. It does not currently allow for a second file to be added for the thumbnail, but it does call into the ffmpeg hooks to automatically generate the thumbnail. The function is called video_upload_create_node_from.

#8

ardee - March 6, 2008 - 16:30

Interesting! And thanks for the quick response to the first new message in this thread in over a year!

What would you suggest for this situation: We have over 500 FLVs and associated JPGs (as thumbnails) and want to have Video nodes with associated Image nodes as thumbnails (no ffmpeg).

But here's a twist: I've already "manually" created Drupal Nodes (of type Video) and entries in the video table for all of them, using various SQL imports and updates. Basically, all I have left to do is figure out a way to get the thumbnails into Image nodes and have them be referenced by the correct records in the Video table.

I already know which video uses which JPG, but that mapping (nid-to-jpg) is to the picture's filename. (The mapping info exists in a separate table that's soon to be deleted, a remnant from my having used CCK & custom code instead of Video in Drupal 4.7). How do I smoothly create all the Image nodes and set the associated NIDs into the corresponding Video table entries? (By the way, these are "url" videos, not "upload" videos, but only because we store then on a separate server from the one running Drupal.)

Any advice on this tricky task would be gratefully received!

UPDATE: A simple SQL query has now copied the thumbnail filename into custom_field_1 for each entry in the Video table. (So there's no more need for that "other" table I'd mentioned above.) Now the task is: For each entry in Video, create a new Image node that uses the picture file from custom_field_1, then put that Image node's nid somewhere into the Video records (so I can get it into the serialized_data portion). I just don't have the knowledge of how to create Image nodes programatically...

#9

vhmauery - March 6, 2008 - 16:37
Status:active» closed

Well, since the video nodes are already created, all you have to do is create the image nodes for the thumbnails. Each thumbnail is stored as an image node. The nid of the image node is stored in the serialized_data column of the db as part of an array, keyed as iid. So basically, all you should have to do is create entries like the following:

mysql> select serialized_data from video;
+-----------------------------------------+
| serialized_data                         |
+-----------------------------------------+
| a:2:{s:3:"iid";N;s:9:"video_fid";i:13;} |
| a:2:{s:3:"iid";N;s:9:"video_fid";i:21;} |
+-----------------------------------------+
2 rows in set (0.00 sec)

Of course, if the type is 'url', then there is no need for 'video_fid' to be a part of serialized data.

My best advice would be to write a script that does the following:

<?php
foreach ($jpg in glob('*.jpg')) {
 
$img = image_create_node_from($jpg);
 
$nid_from_jpg = magic_to_get_video_nid($jpg);
 
$data = unserialize(db_result(db_query('SELECT serialized_data FROM {video} WHERE nid=%d', $nid_from_jpg)));
 
$data['iid'] = $img->nid;
 
$data = serialize($data);
 
db_query("UPDATE {video} SET serialized_data = '%s' WHERE nid=%d", $data, $nid_from_jpg);
}
?>

Now, I totally just pulled that off the top of my head. It will take some refining, but you get the idea. I hope that helps.

Since this task is complete (video does have an insert api now) I am going to close this task.

#10

ardee - March 7, 2008 - 17:05

Thank you, vhmauery, that helps a lot!

First off, I hadn't yet encountered the image_create_node_from() function, which is clearly critical to my task.

Second, I was planning to write the "serialized_data" as a string, being unaware of the functions to manipulate it as a real data type. So you've helped there too.

I guess I only need to figure out how to implement a "magic_to_get_video_nid()" function and I should be good to go.

Thank you VERY much!

-ardee.

UPDATE: If I loop not on actual files but on the filenames stored in custom_field_1 of each entry in the video table, then I won't need a "magic_to_get_video_nid()" function, right?

UPDATE2: Here's what I'm about to try:

<?php
  $result
= db_query("SELECT * from {video}");
  while (
$video = db_fetch_object($result)) {
   
$vnid = $video->nid;
   
$thumbpath = 'thumbs/' . $video->custom_field_1;
   
$imgnid = image_create_node_from($thumbpath);
   
$data = unserialize($video->serialized_data);
   
$data['iid'] = $imgnid;
   
$data = serialize($data);
   
db_query("UPDATE {video} SET serialized_data = '%s' WHERE nid = %d", $data, $video->nid);
  }
?>

UPDATE: Almost have it working, with some issues. One issue is: by unserializing and reserializing the data, you get a gigantic load of extra, convoluted stuff (and it doesn't work!), so I'm going back to having it put the bare minimum into that field (like 'a:1:{s:3:"iid";i:999;}' where you replace 999 with the the node ID of the created image node).

FINAL UPDATE: After many false starts, finally this code worked for me:

<?php
  $result
= db_query("SELECT * from {video}");
  while (
$video = db_fetch_object($result)) {
   
$vnid = $video->nid;
   
$thumbpath = 'thumbs/' . $video->custom_field_1;
   
$img = image_create_node_from($thumbpath);
   
$imgnid = $img->nid;
   
$data = 'a:2:{s:3:"iid";i:' . $imgnid . ';s:17:"object_parameters";a:1:{s:5:"wmode";s:11:"transparent";}}';
   
db_query("UPDATE {video} SET serialized_data = '%s' WHERE nid = %d", $data, $vnid);
   
drupal_set_message("Image $thumbpath done. ");
  }
?>

Once again, vhmauery, I appreciate your input. Without it, I either wouldn't have succeeded at all or it would have taken much longer.

#11

porkchop_d_clown - January 13, 2009 - 16:24

Because my hosting company has tight limits on uploading data via HTTP, I have to upload my videos via FTP and then use the URL video type to display them. The downside is that they don't get thumbnails that way, so I wrote this php script to create them, based on ardee's work and on video_render.php.

I hope you find it useful!

Update: it looks like some auto-formatter or another tried to turn part of my code into a hyper link. I changed the comment to fool the formatter.

<?php
// Copy this file to the root directory of your drupal site. Note you may have to tweak the options.
//
// Usage: php make_thumbs.php h t t p ://mysite.com/make_thumbs.php
//
// Set these for your site.
$home = "/home/mysite";
$ffmpeg = $home . "/bin/ffmpeg";
$tmp = $home . "/tmp";
$options = "-an -y -f mjpeg -ss 2 -s 160x92 -vframes 1 ";

if (isset(
$_SERVER['argv'][1])) {
 
$url = parse_url($_SERVER['argv'][1]);
 
$_SERVER['SCRIPT_NAME'] = $url['path'];
 
$_SERVER['HTTP_HOST'] = $url['host'];
}

include_once
'./includes/bootstrap.inc';
// disable error reporting for bootstrap process
error_reporting(E_ERROR);
// let's bootstrap: we will be able to use drupal apis
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// enable full error reporting again
error_reporting(E_ALL);

if(!empty(
$_SERVER['REQUEST_METHOD'])) {
  print
t('This script is only executable from the command line.');
  die();
}

$result = db_query("SELECT * from {video}");
while (
$video = db_fetch_object($result)) {
       
$vnid = $video->nid;
       
$data = unserialize($video->serialized_data);
        if (
is_null($data['iid']) | $data['iid'] == "") {
               
$thumbpath = $tmp . "/" . $video->nid . ".jpg";
                print
t("Generating thumbnail for " . $vnid . ".\n");
               
//print t($ffmpeg . " -i " . $video->vidfile . " " . $options . " " . $thumbpath ."\n");
               
exec($ffmpeg . " -i " . $video->vidfile . " " . $options . " " . $thumbpath, $output);
               
print_r($output);
               
$img = image_create_node_from($thumbpath);
               
$imgnid = $img->nid;
               
$data['iid'] = $imgnid;
               
$data = serialize($data);
               
db_query("UPDATE {video} SET serialized_data = '%s' WHERE nid = %d", $data, $vnid);
        } else {
                print
t("Skipping " . $vnid . ": iid is " . $data['iid'] . "\n");
        }
}
?>

#12

heshanmw - November 9, 2009 - 02:18
Assigned to:agilpwc» heshanmw
Status:closed» active
 
 

Drupal is a registered trademark of Dries Buytaert.