> ## Documentation Index
> Fetch the complete documentation index at: https://docs.videodb.io/llms.txt
> Use this file to discover all available pages before exploring further.

# AI-Powered Content Moderation

> Moderate video content using AI scene indexing with no external APIs required

<a href="https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/examples/Content_Moderation.ipynb" target="_blank">
  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" noZoom />
</a>

## The Idea

Content moderation can be complex, often requiring multiple tools, manual timestamp extraction, and intricate integration work. Setting up these pipelines involves managing credentials, parsing responses, and stitching everything together.

VideoDB simplifies this into a **"Prompt-and-Filter"** workflow using native AI scene indexing. No external credentials needed. No manual timestamp extraction. Just **prompt engineering** that creates structured labels (**CONTENT\_SAFE**/**CONTENT\_UNSAFE**) from unstructured video content.

The innovation is simple: instead of generic video descriptions, we give the AI a **strict moderation role** with deterministic output labels. This turns unstructured video into structured, searchable data that can be filtered instantly.

Want stricter moderation? **Update the prompt**. Need different criteria? Change a few lines. It's content moderation reimagined for the prompt engineering era.

## Setup

### Install Dependencies

<CodeGroup>
  ```python Python theme={null}
  pip install videodb
  ```

  ```javascript Node.js theme={null}
  npm install videodb
  ```
</CodeGroup>

### Connect to VideoDB

Get your API key from [VideoDB Console](https://console.videodb.io). Free for first 50 uploads, no credit card required.

<CodeGroup>
  ```python Python theme={null}
  from videodb import connect

  conn = connect(api_key="YOUR_API_KEY")
  coll = conn.get_collection()
  ```

  ```javascript Node.js theme={null}
  import { connect } from 'videodb';

  const conn = await connect({ apiKey: process.env.VIDEO_DB_API_KEY });
  const coll = await conn.getCollection();
  ```
</CodeGroup>

## Implementation

### Step 1: Upload Video

We'll use a Breaking Bad clip with mixed content to test the moderation workflow.

<CodeGroup>
  ```python Python theme={null}
  # Upload video from YouTube
  video = coll.upload(url='https://www.youtube.com/watch?v=Xa7UaHgOGfM')
  print(f"Uploaded Video ID: {video.id}")

  # Preview the video
  video.play()
  ```

  ```javascript Node.js theme={null}
  // Upload video from YouTube
  const video = await coll.upload({ url: 'https://www.youtube.com/watch?v=Xa7UaHgOGfM' });
  console.log(`Uploaded Video ID: ${video.id}`);

  // Get the player URL
  const playerUrl = video.playerUrl;
  console.log(playerUrl);
  ```
</CodeGroup>

### Step 2: Index Scenes with Moderator Prompt

This is the core innovation. We give the AI a strict role as a Content Moderator with deterministic output labels. The prompt instructs the AI to analyze visual content for specific inappropriate elements and respond with either `CONTENT_SAFE` or `CONTENT_UNSAFE`.

This structured labeling transforms unstructured video into searchable, filterable data.

<CodeGroup>
  ```python Python theme={null}
  from videodb import SceneExtractionType

  # Define strict moderation instructions
  moderation_prompt = """
  You are a Content Moderator. Analyze the visual content for inappropriate elements:
  1. Violence (fighting, hitting, shooting)
  2. Weapons (guns, knives)
  3. Blood or Gore
  4. Drug use
  5. Sexual content

  If ANY of these are detected, your response must start with:
  "CONTENT_UNSAFE: [brief reason]"

  If the scene is clean and safe, your response must start with:
  "CONTENT_SAFE: [brief description]"
  """

  # Index video in 5-second chunks for granular moderation
  scene_index_id = video.index_scenes(
      prompt=moderation_prompt,
      extraction_type=SceneExtractionType.time_based,
      extraction_config={
          "time": 5,       # Check every 5 seconds
          "frame_count": 3 # Analyze 3 frames per segment
      }
  )

  print("Moderation indexing complete!")
  ```

  ```javascript Node.js theme={null}
  import { SceneExtractionTypeValues } from 'videodb';

  // Define strict moderation instructions
  const moderationPrompt = `
  You are a Content Moderator. Analyze the visual content for inappropriate elements:
  1. Violence (fighting, hitting, shooting)
  2. Weapons (guns, knives)
  3. Blood or Gore
  4. Drug use
  5. Sexual content

  If ANY of these are detected, your response must start with:
  "CONTENT_UNSAFE: [brief reason]"

  If the scene is clean and safe, your response must start with:
  "CONTENT_SAFE: [brief description]"
  `;

  // Index video in 5-second chunks for granular moderation
  const sceneIndexId = await video.indexScenes({
      prompt: moderationPrompt,
      extractionType: SceneExtractionTypeValues.timeBased,
      extractionConfig: {
          time: 5,       // Check every 5 seconds
          frameCount: 3  // Analyze 3 frames per segment
      }
  });

  console.log("Moderation indexing complete!");
  ```
</CodeGroup>

**Why this works:** By enforcing strict output formats (`CONTENT_SAFE`/`CONTENT_UNSAFE`), we can use simple keyword searches to filter content. No complex parsing or external API integration needed.

### Step 3: Review Scene Indexes (Optional)

Want to see what the AI detected? Check the scene indexes to understand how content was labeled.

<CodeGroup>
  ```python Python theme={null}
  # Fetch scene indexes
  scene_indexes = video.get_scene_index(scene_index_id)

  # Print first 5 scenes
  for i, scene in enumerate(scene_indexes[:5]):
      print(f"Scene {i+1}:")
      print(f"  Time: {scene['start']}s - {scene['end']}s")
      print(f"  Status: {scene['description']}\n")
  ```

  ```javascript Node.js theme={null}
  // Fetch scene indexes
  const sceneIndexes = await video.getSceneIndex(sceneIndexId);

  // Print first 5 scenes
  sceneIndexes.slice(0, 5).forEach((scene, i) => {
      console.log(`Scene ${i+1}:`);
      console.log(`  Time: ${scene.start}s - ${scene.end}s`);
      console.log(`  Status: ${scene.description}\n`);
  });
  ```
</CodeGroup>

**Sample output:**

```
Scene 1:
  Time: 0.0s - 5.005s
  Status: CONTENT_SAFE: The images display title cards with a smoky background...

Scene 2:
  Time: 5.005s - 10.01s
  Status: CONTENT_SAFE: Two men in indoor settings, no inappropriate elements...

Scene 5:
  Time: 20.02s - 25.025s
  Status: CONTENT_UNSAFE: Implied physical confrontation and aggressive interaction...
```

### Step 4: Filter for Safe Content

Now the magic happens. Because we structured the AI's responses with `CONTENT_SAFE` labels, we can use a simple keyword search to filter the entire video.

<CodeGroup>
  ```python Python theme={null}
  from videodb import SearchType, IndexType

  # Search for safe content using keyword search
  safe_results = video.search(
      query="CONTENT_SAFE",
      search_type=SearchType.keyword,
      index_type=IndexType.scene,
      scene_index_id=scene_index_id
  )

  # Get the safe segments
  safe_shots = safe_results.get_shots()
  print(f"Found {len(safe_shots)} safe segments")

  # Inspect first few segments
  for i, shot in enumerate(safe_shots[:3]):
      print(f"Segment {i+1} ({shot.start}s - {shot.end}s): {shot.text}")
  ```

  ```javascript Node.js theme={null}
  import { SearchTypeValues, IndexTypeValues } from 'videodb';

  // Search for safe content using keyword search
  const safeResults = await video.search({
      query: "CONTENT_SAFE",
      searchType: SearchTypeValues.keyword,
      indexType: IndexTypeValues.scene,
      sceneIndexId: sceneIndexId
  });

  // Get the safe segments
  const safeShots = safeResults.getShots();
  console.log(`Found ${safeShots.length} safe segments`);

  // Inspect first few segments
  safeShots.slice(0, 3).forEach((shot, i) => {
      console.log(`Segment ${i+1} (${shot.start}s - ${shot.end}s): ${shot.text}`);
  });
  ```
</CodeGroup>

### Step 5: Play the Clean Version

The filtered results come with a stream URL ready for instant playback. No rendering, no waiting.

<CodeGroup>
  ```python Python theme={null}
  # Get the stream URL
  print("Stream URL:", safe_results.stream_url)

  # Play in notebook/browser
  safe_results.play()
  ```

  ```javascript Node.js theme={null}
  // Get the stream URL
  console.log("Stream URL:", safeResults.streamUrl);

  // Use this URL in any video player
  ```
</CodeGroup>

Here's the result - a clean version with all inappropriate content removed:

<iframe className="w-full aspect-video rounded-xl" src="https://stream.videodb.io/v3/published/manifests/9ff92994-de07-46b2-b2db-c652e22d2b5c.m3u8" title="AI-Powered Content Moderation Result" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

## What You Get

* No external APIs or credentials required
* Full control over moderation criteria through prompts
* Instant filtering without video re-encoding
* Granular 5-second scene analysis
* Real-time playback of cleaned content
* Customizable: change prompt to adjust moderation standards instantly

## Perfect For

* Educational platforms serving minor audiences
* Family-friendly streaming services
* Corporate training content libraries
* Social media platforms with content policies
* Broadcasting companies creating TV-safe edits
* User-generated content platforms with safety requirements

## The Result

What used to require multiple integrations, manual timestamp extraction, and complex video editing pipelines now works with just prompt engineering. Change your moderation criteria instantly by updating the prompt—no re-processing needed.

Pure simplicity powered by VideoDB's native AI indexing.

<Card icon="notebook" title="Explore Full Notebook" href="https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/examples/Content_Moderation.ipynb">
  Open the complete implementation in Google Colab with detailed explanations and working code.
</Card>

## Related Tutorials

<CardGroup cols={2}>
  <Card title="Profanity Detection" icon="ban" href="/examples-and-tutorials/safety-compliance/beep-profanity">
    Detect and censor curse words with audio overlays
  </Card>

  <Card title="Keyword Search" icon="search" href="/examples-and-tutorials/video-rag/keyword-search">
    Find and extract specific content from your videos
  </Card>
</CardGroup>
