Migrating from JSStream to Vimeo Using Drupal Migration API

Migrating from JSStream to Vimeo Using Drupal Migration API

On13th Dec 2024, 2024-12-13T17:24:47+05:30 ByKarthik Kumar D K | read
Listen Pause Resume Stop

Migrating video content from JSStream (a custom video streaming platform) to Vimeo can be a crucial task if you're looking for a more scalable, feature-rich platform for hosting and managing your videos. While manual migration might work for small datasets, automating the process via Drupal’s Migration API can save significant time and reduce errors, especially for larger video libraries.

In this article, we will walk through how to migrate video content (including video files, titles, descriptions, and tags) from JSStream to Vimeo using Drupal 10. The migration will leverage Drupal’s Migration API, custom migration process plugins, and the Vimeo API.

Key Concepts

  1. Drupal Migration API: This API allows importing content from external sources (like JSStream) and mapping it to Drupal entities (e.g., media entities).
  2. Vimeo API: The Vimeo API allows programmatic interaction with Vimeo to upload videos, set metadata, and manage video privacy and settings.
  3. Media Entities: In Drupal, media entities represent multimedia content (images, videos, etc.). We’ll use the Video media bundle to store Vimeo videos.

Prerequisites

Before proceeding with the migration, ensure you have the following setup:

  • Drupal latest version installed.
  • The required migration modules enabled: migrate, migrate_plus, migrate_tools.
  • A Vimeo Developer account to get Client ID, Client Secret, and Access Token.
  • Video files and metadata exported from JSStream in a CSV or JSON format.

1. Install Required Modules

First, install the necessary modules for migration:

composer require drupal/migrate drupal/migrate_plus drupal/migrate_tools

Then enable the modules:

drush en migrate migrate_plus migrate_tools

2. Define Migration Configuration

Create a migration configuration file in YAML to define how Drupal should import the data and map it to Drupal entities. We will assume the data from JSStream is exported to a CSV file.

Example CSV (jsstream_videos.csv)

title,description,tags,file_path

"Sample Video 1","Description for video 1","tutorial, drupal","/path/to/video1.mp4"

"Sample Video 2","Description for video 2","tutorial, drupal","/path/to/video2.mp4"

Migration YAML Configuration (jsstream_to_vimeo.yml)

Place the following YAML file under modules/custom/my_module/config/install (create the file structure if it doesn't exist).

id: jsstream_to_vimeo

label: 'Migrate JSStream Videos to Vimeo'

migration_group: jsstream_migrations

source:

  plugin: csv

  path: '/path/to/jsstream_videos.csv'

  delimiter: ','

  header_row_count: 1

  key: id

  columns:

    - title

    - description

    - tags

    - file_path

destination:

  plugin: 'entity:media'

  default_bundle: video

  update_existing: true

process:

  field_media_video_file:

    plugin: vimeo_upload

    source: file_path

  name: title

  field_description: description

  field_tags:

    plugin: explode

    source: tags

    delimiter: ','

  status: 1

migration_dependencies: []

Explanation of the YAML Configuration:

  • Source: We are using a CSV file (jsstream_videos.csv) as the source of video metadata. Adjust the path to match the location of your exported file.
  • Destination: We are creating media entities of type video. This assumes you've already set up a Video media bundle in Drupal.
  • Process: The field_media_video_file will be populated using the custom plugin (vimeo_upload). Other metadata fields (e.g., title, description, tags) are directly mapped from the CSV.
  • field_tags: Tags are split using the explode plugin to handle multiple tags.

3. Create the Custom Migration Process Plugin (vimeo_upload)

Now we need to create a custom migration process plugin (vimeo_upload) to handle uploading videos to Vimeo and setting their metadata.

Create the Custom Process Plugin

In your custom module (e.g., /modules/custom/my_module), create the following PHP file for the custom process plugin.

Custom Process Plugin (VimeoUpload.php)

namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate\Plugin\migrate\process\ProcessPluginBase;

use Vimeo\Vimeo;

use Drupal\Core\Field\FieldInterface;

use Drupal\migrate\MigrateExecutableInterface;

use Drupal\migrate\Row;

/**

 * Upload video to Vimeo and return the video URI.

 *

 * @MigrateProcessPlugin(

 *   id = "vimeo_upload"

 * )

 */

class VimeoUpload extends ProcessPluginBase {

  /**

   * Process the migration data and upload to Vimeo.

   *

   * @param string $file_path

   *   The path to the video file to be uploaded.

   * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable

   *   The migrate executable.

   * @param \Drupal\migrate\Row $row

   *   The current row of migration data.

   * @param \Drupal\Core\Field\FieldDefinitionInterface $field

   *   The field being processed.

   *

   * @return string

   *   The Vimeo video URI or another identifier.

   */

  public function transform($file_path, MigrateExecutableInterface $migrate_executable, Row $row, FieldInterface $field) {

    // Vimeo API credentials

    $client_id = 'YOUR_VIMEO_CLIENT_ID';

    $client_secret = 'YOUR_VIMEO_CLIENT_SECRET';

    $access_token = 'YOUR_VIMEO_ACCESS_TOKEN';

    // Initialize Vimeo client

    $vimeo = new Vimeo($client_id, $client_secret);

    $vimeo->setToken($access_token);

    // Upload video to Vimeo

    try {

      $response = $vimeo->upload($file_path);

      // Set video metadata (title, description, tags)

      $video_id = $response['uri'];

      $metadata = [

        'name' => $row->getSourceProperty('title'),

        'description' => $row->getSourceProperty('description'),

        'tags' => explode(',', $row->getSourceProperty('tags')),  // Split tags

      ];

      $vimeo->request($video_id, $metadata, 'PATCH');

      // Return the video URI (or other video identifier)

      return $video_id;

    }

    catch (\Exception $e) {

      // Log the error if the upload fails

      \Drupal::logger('my_module')->error('Vimeo upload failed: ' . $e->getMessage());

      return NULL;  // Return NULL in case of failure

    }

  }

}

Explanation of the vimeo_upload Plugin:

  • Vimeo API credentials: You need to replace 'YOUR_VIMEO_CLIENT_ID', 'YOUR_VIMEO_CLIENT_SECRET', and 'YOUR_VIMEO_ACCESS_TOKEN' with your actual Vimeo API credentials.
  • Uploading video: The upload() method uploads the video to Vimeo.
  • Setting metadata: After uploading, the video's title, description, and tags are updated via the Vimeo API using the PATCH request.
  • Returning the video URI: The plugin returns the Vimeo video URI (or another identifier) to Drupal.

Make sure you have installed the Vimeo PHP SDK via Composer:

composer require vimeo/vimeo-api

4. Run the Migration

Once the migration configuration and custom plugin are set up, you can execute the migration using Drush.

Import Migration Configuration: Import the configuration file into Drupal:

drush cim

Run the Migration: Execute the migration:

drush migrate-import jsstream_to_vimeo

Check Migration Status: To check the migration status:

drush migrate-status

5. Verify Data on Vimeo

After the migration is complete, follow these steps to verify the data:

  • Check Vimeo: Log into your Vimeo account and check if the videos have been uploaded.
  • Check metadata: Ensure that the video titles, descriptions, and tags were properly set on Vimeo.
  • Test playback: Ensure the videos are playing correctly and accessible as needed.

Conclusion

Migrating video content from JSStream to Vimeo via Drupal 10’s Migration API provides a flexible and automated way to move large datasets with minimal manual intervention. By utilizing Drupal’s migration tools, custom process plugins, and the Vimeo API, you can easily transfer video files, along with their metadata (title, description, tags), to Vimeo.

This approach is highly scalable and efficient, making it an ideal solution for organizations and developers who need to migrate substantial video libraries while maintaining the integrity of metadata and video playback functionality.

Thanks for reading the article, for more drupal related articles read and subscribe to peoples blog articles.

Labels


Related Articles

Recent Articles

Recent Quick Read

Recent Great People

We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies