Basic Text to Video

This example demonstrates how to create a simple video from text using the Pictory API. The video will automatically select visuals based on the text content.

Overview

This example covers:

  • Getting an access token
  • Render a video storyboard from text
  • Monitoring job status

Node.js Example

Prerequisites

npm install axios

Complete Code

import axios from "axios";

const API_BASE_URL = "https://api.pictory.ai/pictoryapis";
const CLIENT_ID = "YOUR_CLIENT_ID";
const CLIENT_SECRET = "YOUR_CLIENT_SECRET";

// Sample text for the video
const SAMPLE_TEXT =
  "AI is poised to significantly impact educators and course creators on social media. By automating tasks like content generation, visual design, and video editing, AI will save time and enhance consistency. This allows creators to focus on higher-level strategies and ensures a cohesive brand presence. Personalization is another key benefit. AI can analyze social media interactions to tailor content to individual learner needs, making learning more engaging and effective. This level of personalization, previously difficult to achieve at scale, will become more accessible. AI also offers advanced analytics, providing insights into content performance and audience engagement. Creators can quickly refine their strategies based on real-time data, ensuring their content remains relevant and impactful. However, the rise of AI brings challenges. There's a risk that over-reliance on AI-generated content could diminish authenticity. Automated content might lack the personal touch that makes learning experiences unique. Educators will need to balance AI efficiency with maintaining the human element in their content. Ultimately, AI will be a powerful tool for those who embrace it. Educators and course creators who adapt will likely expand their reach, improve content quality, and engage more effectively with their audiences on social media.";

async function createTextToVideo() {
  try {
    // Step 1: Get Access Token
    console.log("Step 1: Getting access token...");
    const tokenResponse = await axios.post(
      `${API_BASE_URL}/v1/oauth2/token`,
      {
        client_id: CLIENT_ID,
        client_secret: CLIENT_SECRET,
      },
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    const accessToken = tokenResponse.data.access_token;
    console.log("Access token obtained successfully");

    // Step 2: Create and Render Video Storyboard
    console.log("Step 2: Creating and rendering video storyboard...");
    const storyboardResponse = await axios.post(
      `${API_BASE_URL}/v2/video/storyboard/render`,
      {
        videoName: "basic_text_to_video",
        scenes: [
          {
            story: SAMPLE_TEXT,
            createSceneOnNewLine: true,
            createSceneOnEndOfSentence: true,
          },
        ],
      },
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: accessToken,
        },
      }
    );

    const renderJobId = storyboardResponse.data.data.jobId;
    console.log("Storyboard render job created");
    console.log("Job ID:", renderJobId, "\n");

    // Step 3: Monitor Job Status
    console.log("Step 3: Monitoring job status...");
    let jobCompleted = false;
    let jobResult = null;

    while (!jobCompleted) {
      const jobStatusResponse = await axios.get(`${API_BASE_URL}/v1/jobs/${renderJobId}`, {
        headers: {
          Authorization: accessToken,
        },
      });

      const status = jobStatusResponse.data.data.status;
      console.log("Current status:", status);

      if (status === "completed") {
        jobCompleted = true;
        jobResult = jobStatusResponse.data;
        console.log("\nVideo created successfully!");
        console.log("Video URL:", jobResult.data.videoUrl);
      } else if (status === "failed") {
        throw new Error("Job failed: " + JSON.stringify(jobStatusResponse.data));
      } else {
        // Wait 5 seconds before checking again
        await new Promise(resolve => setTimeout(resolve, 5000));
      }
    }

    return jobResult;
  } catch (error) {
    console.error("Error:", error.response?.data || error.message);
    throw error;
  }
}

// Run the function
createTextToVideo();

Python Example

Prerequisites

pip install requests

Complete Code

import requests
import time
import json

API_BASE_URL = 'https://api.pictory.ai/pictoryapis'
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'

# Sample text for the video
SAMPLE_TEXT = "AI is poised to significantly impact educators and course creators on social media. By automating tasks like content generation, visual design, and video editing, AI will save time and enhance consistency. This allows creators to focus on higher-level strategies and ensures a cohesive brand presence. Personalization is another key benefit. AI can analyze social media interactions to tailor content to individual learner needs, making learning more engaging and effective. This level of personalization, previously difficult to achieve at scale, will become more accessible. AI also offers advanced analytics, providing insights into content performance and audience engagement. Creators can quickly refine their strategies based on real-time data, ensuring their content remains relevant and impactful. However, the rise of AI brings challenges. There's a risk that over-reliance on AI-generated content could diminish authenticity. Automated content might lack the personal touch that makes learning experiences unique. Educators will need to balance AI efficiency with maintaining the human element in their content. Ultimately, AI will be a powerful tool for those who embrace it. Educators and course creators who adapt will likely expand their reach, improve content quality, and engage more effectively with their audiences on social media."

def create_text_to_video():
    try:
        # Step 1: Get Access Token
        print('Step 1: Getting access token...')
        token_response = requests.post(
            f'{API_BASE_URL}/v1/oauth2/token',
            json={
                'client_id': CLIENT_ID,
                'client_secret': CLIENT_SECRET
            },
            headers={
                'Content-Type': 'application/json'
            }
        )
        token_response.raise_for_status()

        access_token = token_response.json()['access_token']
        print('Access token obtained successfully')

        # Step 2: Create and Render Video Storyboard
        print('Step 2: Creating and rendering video storyboard...')
        storyboard_response = requests.post(
            f'{API_BASE_URL}/v2/video/storyboard/render',
            json={
                'videoName': 'basic_text_to_video',
                'scenes': [
                    {
                        'story': SAMPLE_TEXT,
                        'createSceneOnNewLine': True,
                        'createSceneOnEndOfSentence': True
                    }
                ]
            },
            headers={
                'Content-Type': 'application/json',
                'Authorization': access_token
            }
        )
        storyboard_response.raise_for_status()

        render_job_id = storyboard_response.json()['data']['jobId']
        print('Storyboard render job created')
        print(f'Job ID: {render_job_id}\n')

        # Step 3: Monitor Job Status
        print('Step 3: Monitoring job status...')
        job_completed = False
        job_result = None

        while not job_completed:
            job_status_response = requests.get(
                f'{API_BASE_URL}/v1/jobs/{render_job_id}',
                headers={
                    'Authorization': access_token
                }
            )
            job_status_response.raise_for_status()

            status = job_status_response.json()['data']['status']
            print(f'Current status: {status}')

            if status == 'completed':
                job_completed = True
                job_result = job_status_response.json()
                print('\nVideo created successfully!')
                print(f"Video URL: {job_result['data']['videoUrl']}")
            elif status == 'failed':
                raise Exception(f"Job failed: {json.dumps(job_status_response.json())}")
            else:
                # Wait 5 seconds before checking again
                time.sleep(5)

        return job_result

    except requests.exceptions.RequestException as error:
        print(f'Error: {error}')
        if hasattr(error, 'response') and error.response is not None:
            print(f'Response: {error.response.text}')
        raise

# Run the function
if __name__ == '__main__':
    create_text_to_video()

Key Parameters

  • videoName: A descriptive name for your video project
  • scenes: An array containing scene objects
    • story: The text content that will be converted to video
    • createSceneOnNewLine: When true, creates a new scene at each new line in the text
    • createSceneOnEndOfSentence: When true, creates a new scene at the end of each sentence

Response

The API will return a job ID that you can use to monitor the video creation progress. Once completed, you'll receive a video URL where you can download or view the generated video.

Notes

  • Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual API credentials
  • The video creation process may take several minutes depending on the length of the content
  • Videos are automatically created with visuals that match the text content