How to Import Vimeo Videos into DynTube

You can utilize the following JavaScript code to seamlessly import your Vimeo videos into DynTube.

import { Vimeo } from '@vimeo/vimeo';
import fetch from 'node-fetch';
import { FormData } from 'formdata-node';

const vimeoClient = new Vimeo("VIMEO_CLIENT", "VIMEO_SECRET", "VIMEO_ACCESS_TOKEN");

async function fetchVimeoVideo(vimeoVideoId) {
  return new Promise((resolve, reject) => {
    vimeoClient.request({
      method: 'GET',
      path: `/videos/${vimeoVideoId}`,
    }, (error, body) => error ? reject(error) : resolve(body));
  });
}

function getMp4Link(video) {
  return video.download?.sort((a, b) => b.width - a.width)
    .find(file => file.type === 'video/mp4')?.link || '';
}

async function uploadToDynTube(videoMp4Link, apiKey) {
  const form = new FormData();
  form.append('url', videoMp4Link);

  const response = await fetch('https://upload.dyntube.com/v1/videos', {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${apiKey}` },
    body: form,
  });

  console.log(await response.text());
}

async function processVimeoVideo(vimeoVideoId, apiKey) {
  try {
    const video = await fetchVimeoVideo(vimeoVideoId);
    const videoMp4Link = getMp4Link(video);

    videoMp4Link
      ? await uploadToDynTube(videoMp4Link, apiKey)
      : console.log('No MP4 link found.');
  } catch (error) {
    console.error(`Error: ${error.message}`);
  }
}

// Usage
processVimeoVideo('[VIMEO_VIDEO_ID]', '[DYNTUBE_API_KEY]');

Last updated