Programmatically creating video fields

You, DrupalDrupal video
Back

Thanks @Silicon.Valet for effort and finally we could come-up with a solution to create video fields programmatically. Creating video fields are useful when you have videos in your hand and need to import to Drupal using Drush or running a script. I prefer Drush, cause its more straightforward. I'm not going to explain how to use Drush here, but you can create Drush command and put those code to run for that command would be great.

<?php
      // Node initialization stuff redacted. look elsewhere for that.

     // Add the file to the file table
      $video = field_file_save_file($file_temp, array(), $dir, $account);

      // Load transcoder.inc from the node module.
      if (module_load_include('inc', 'video', 'includes/transcoder') === FALSE) {
        echo "ERR 003: SERVER SIDE ERROR - FILE COULD NOT BE TRANSCODED";
        exit;
      }

     // Get a transcoder object to generate the thumbnails
      $transcoder = new video_transcoder;
      $thumbs = $transcoder->generate_thumbnails($video);
      $thumb  = array_pop($thumbs);

      $video['data'] = array(
          'dimensions'                       => '320x240',
          'player_dimensions'            => '320x240',
          'convert_video_on_save'     => 1,
          'video_thumb'                    => $thumb->filepath,
          'bypass_autoconversion'     => 0,
          'use_default_video_thumb' => 0
      );

      // Assign the file field content to the cck field
      $node->field_status_video = array( 0 => $video);
    
      // Save the node
      node_save($node);

     // Queue the file for processing
      $res = db_query(
        "INSERT INTO {video_files} (fid, nid, dimensions, status) VALUES (%d, %d, '%s', 1)",
        $video['fid'],
        $node->nid,
        '320x240'
      );
      $vid = db_last_insert_id('video_files','vid');

      // populate the conversion information.
      $data = array(
        'vid'            => $vid,
        'filename'   => $video['filename'],
        'filepath'     => $video['destination'],
        'filemime'   => $video['filemime'],
        'filesize'      => $video['filesize'],
        'status'        => 1,
        'preset'       => 'hq_flash'
      );
      $res = db_query(
        "UPDATE {video_files} SET data = '%s' WHERE vid = %d",
        serialize($data),
        $vid
      );
?>
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.