I'm using the video module to make a video library. I set it up with a very basic video cck type which has a title and description and video file field and a node reference field to the node that it is attached to. I created a view of video nodes to act as the "library" which displays the thumbnail and link to video node .

Now my problem is I need to create video nodes for videos that already are attached to other nodes in my system. They are attached as file attachments and have an entry in the file table and exist in the files directory on the server. So i could not upload all those again of course. Plus i still want people to attach videos to other nodes just as they have as file attachments and i want to automatically add them to the video library via creating a video node behind the scenes.

I do this via a function in my modules hook_cron. It finds any video files not already made into video nodes and makes video nodes for them. Doing this i found one issue where if extension was uppercase on file name then the video module could not find a suitable player. I filed an issue for video 6.4.rc4 . Simple fix. Here is the function i call in hook_cron.

function _make_video_nodes()
{

  global $user;
  # Select the nodes that have video files but not video nodes yet.
   $sql = "select u.nid, u.description as title , u.vid, u.fid, n.uid
   from upload u 
   join files f on u.fid = f.fid 
   join node n on u.vid = n.vid  
   left outer join content_type_video v on u.fid = v.field_video_fid 
   where v.field_video_fid is null and f.filemime like 'video%' ";
 
  $result = db_query($sql);

  while ($obj = db_fetch_object($result))
  {
    
    # Create video  node object. I set some defaults for the video field -- list and dimensions
    $node = (object)array('type'=>'video', 'uid'=>$obj->uid, 'published'=>1, 'title'=>$obj->title );
    $node->field_video = array(array('fid' => $obj->fid, 'list'=>1,
          'data'=>array('dimensions' => '640x480', 'player_dimensions' => '640x480')
          ,));
    $node->field_full_node = array(array('nid' => $obj->nid));
    $node = node_submit($node);
    node_save($node);

    if ($node->nid)
    {
      # For some reason the uid is never set so i set it explicitly
      $sql = 'Update node set uid = ' . $obj->uid . ' where nid = ' . $node->nid;
      update_sql($sql);
      watchdog('mymodule', '@type: added video node %nid  %title .', array('@type' => $node->type, '%nid'=> $node->nid, '%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
    }
  }
}


Thats it. Works pretty good so far. Afterwards I have to edit the video nodes and choose the thumbnail. Thats the only issue i have not worked out programatically. But i don't have many existing videos so not a problem.

Comments

WorldFallz’s picture

Just an fyi, the filefield_sources module allows reuse of files attached to other nodes. It won't automate like your solution, but it does handle the "i still want people to attach videos to other nodes just as they have as file attachments" part.

pjsz’s picture

Hmm, thanks . i'll have no give that a try. I wonder if it works with video module ? Its just altering the filefield attachment form i guess so it should work. If it does thats a great tip. Thanks.