Text to Video with Highlight Keywords and Hide Subtitles

This example demonstrates how to create a video with keyword highlighting in subtitles and how to hide subtitles entirely. Keyword highlighting emphasizes important words, while hiding subtitles removes them from the video.

Overview

This example covers:

  • Getting an access token
  • Enabling keyword highlighting in subtitles
  • Hiding subtitles in specific scenes or entire video
  • Understanding keyword detection
  • Combining highlighting with custom subtitle styles
  • Monitoring job status and retrieving the final video

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";

const STORY_TEXT_1 = "AI is poised to significantly impact educators and course creators on social media.";
const STORY_TEXT_2 =
  "By automating tasks like content generation, visual design, and video editing, AI will save time and enhance consistency.";

async function createTextToVideoWithHighlightKeywords() {
  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");
    console.log("Token expires in:", tokenResponse.data.expires_in, "seconds\n");

    // Step 2: Create Video with Keyword Highlighting and Hidden Subtitles
    console.log("Step 2: Creating video with keyword highlighting...");
    const storyboardResponse = await axios.post(
      `${API_BASE_URL}/v2/video/storyboard/render`,
      {
        videoName: "text_to_video_highlight_keywords",
        // Define keyword color at video level
        subtitleStyle: {
          fontFamily: "Poppins",
          fontSize: 48,
          color: "rgba(255, 255, 255, 1)",
          backgroundColor: "rgba(0, 0, 0, 0.7)",
          keywordColor: "rgba(255, 215, 0, 1)", // Gold color for highlighted keywords
          position: "bottom-center",
        },
        scenes: [
          {
            story: STORY_TEXT_1,
            createSceneOnNewLine: false,
            createSceneOnEndOfSentence: false,
            highlightKeywords: true, // Enable keyword highlighting for this scene
            hideSubtitles: false, // Show subtitles with highlighted keywords
          },
          {
            story: STORY_TEXT_2,
            createSceneOnNewLine: false,
            createSceneOnEndOfSentence: false,
            highlightKeywords: false, // Disable keyword highlighting
            hideSubtitles: true, // Hide subtitles completely for this scene
          },
        ],
      },
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: accessToken,
        },
      }
    );

    const renderJobId = storyboardResponse.data.data.jobId;
    console.log("Video with keyword highlighting 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 with keyword highlighting 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
createTextToVideoWithHighlightKeywords();

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'

STORY_TEXT_1 = "AI is poised to significantly impact educators and course creators on social media."
STORY_TEXT_2 = "By automating tasks like content generation, visual design, and video editing, AI will save time and enhance consistency."

def create_text_to_video_with_highlight_keywords():
    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')
        print(f"Token expires in: {token_response.json()['expires_in']} seconds\n")

        # Step 2: Create Video with Keyword Highlighting and Hidden Subtitles
        print('Step 2: Creating video with keyword highlighting...')
        storyboard_response = requests.post(
            f'{API_BASE_URL}/v2/video/storyboard/render',
            json={
                'videoName': 'text_to_video_highlight_keywords',
                # Define keyword color at video level
                'subtitleStyle': {
                    'fontFamily': 'Poppins',
                    'fontSize': 48,
                    'color': 'rgba(255, 255, 255, 1)',
                    'backgroundColor': 'rgba(0, 0, 0, 0.7)',
                    'keywordColor': 'rgba(255, 215, 0, 1)',  # Gold color for highlighted keywords
                    'position': 'bottom-center'
                },
                'scenes': [
                    {
                        'story': STORY_TEXT_1,
                        'createSceneOnNewLine': False,
                        'createSceneOnEndOfSentence': False,
                        'highlightKeywords': True,  # Enable keyword highlighting for this scene
                        'hideSubtitles': False      # Show subtitles with highlighted keywords
                    },
                    {
                        'story': STORY_TEXT_2,
                        'createSceneOnNewLine': False,
                        'createSceneOnEndOfSentence': False,
                        'highlightKeywords': False, # Disable keyword highlighting
                        'hideSubtitles': True       # Hide subtitles completely for this scene
                    }
                ]
            },
            headers={
                'Content-Type': 'application/json',
                'Authorization': access_token
            }
        )
        storyboard_response.raise_for_status()

        render_job_id = storyboard_response.json()['data']['jobId']
        print('Video with keyword highlighting 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 with keyword highlighting 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_with_highlight_keywords()

Key Parameters

Highlight Keywords

  • scenes[].highlightKeywords: Boolean
    • true - AI automatically detects and highlights important keywords
    • false - All text appears in the same color

Hide Subtitles

  • scenes[].hideSubtitles: Boolean (default: false)
    • true - Subtitles are completely hidden for this scene
    • false - Subtitles are displayed normally

Keyword Color

  • subtitleStyle.keywordColor: RGBA color for highlighted keywords
    • Example: "rgba(255, 215, 0, 1)" for gold color
    • Only applies when highlightKeywords is true

How Keyword Highlighting Works

  1. AI analyzes the text content of each scene
  2. Important words and phrases are identified (nouns, key verbs, important concepts)
  3. These keywords are displayed in the keywordColor
  4. Other words appear in the standard color

Use Cases

Keyword Highlighting

  • Emphasize important concepts in educational videos
  • Draw attention to product names or features
  • Highlight key takeaways in presentations
  • Make call-to-action words stand out
  • Improve viewer engagement and retention

Hidden Subtitles

  • Create cinematic scenes without text
  • Display only visuals and voice-over
  • Create intro/outro sequences
  • Scenes where text would be distracting
  • Videos for multilingual dubbing

Combining Features

You can mix scenes with different configurations:

scenes: [
  {
    story: "Intro text",
    hideSubtitles: true, // No subtitles on intro
  },
  {
    story: "Main content with important keywords",
    highlightKeywords: true, // Highlight keywords
    hideSubtitles: false,
  },
  {
    story: "Regular content",
    highlightKeywords: false, // No highlighting
    hideSubtitles: false,
  },
];

Important Notes

  1. Keyword Detection: The AI automatically identifies keywords - you don't manually specify which words to highlight
  2. Color Requirement: You must define keywordColor in subtitleStyle for highlighting to be visible
  3. Scene-Level Control: Both highlightKeywords and hideSubtitles are scene-specific
  4. Default Behavior: By default, hideSubtitles is false (subtitles are shown)

Response

The API returns a job ID for monitoring the video creation progress. Once completed, you'll receive a video URL with keyword highlighting applied according to your configuration.

Notes

  • Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual API credentials
  • Keyword highlighting requires AI processing which may slightly increase processing time
  • The keyword color should contrast well with the regular text color for visibility
  • Hidden subtitles still affect scene timing based on text length