> ## 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.

# Audio Overlay

> Combine multiple audio tracks with video

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

## Overview

Welcome to the groovy world of audio overlays with VideoDB! 🎶 In this tutorial, we're diving into the magic of adding audio overlays to your video assets. Picture this: you've got your video content all set, but it's missing that extra oomph. That's where audio overlay swoops in to save the day! With VideoDB's easy-to-use feature, you can seamlessly weave in background music, voiceovers, or funky sound effects, transforming your videos from ordinary to extraordinary. Let's crank up the volume and get ready to rock and roll!

## Setup

### Installing packages

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

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

### API Keys

Before proceeding, ensure access to [VideoDB](https://videodb.io)

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

## Steps

### Step 1: Connect to VideoDB

Connect to VideoDB to establish a session for uploading and manipulating video files. Import the necessary modules from VideoDB library to access functionalities.

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

  # Set your API key
  api_key = "your_api_key"

  # Connect to VideoDB
  conn = videodb.connect(api_key=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>

### Step 2: Upload Video

Upload the video to VideoDB collection. You can upload the video asset from your local device or from a YouTube URL to upload the video from its source.

<CodeGroup>
  ```python Python theme={null}
  video = coll.upload(url="https://youtu.be/e49VEpWg61M")
  video.play()
  ```

  ```javascript Node.js theme={null}
  const video = await coll.uploadURL({
    url: "https://youtu.be/e49VEpWg61M"
  });
  console.log(video.playerUrl);
  ```
</CodeGroup>

You can upload from your local file system too by passing `file_path` in `upload()`

Here's the video we'll be using for this tutorial:

<iframe className="w-full aspect-video rounded-xl" src="https://www.youtube.com/embed/e49VEpWg61M" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

### Step 3: Upload Audio

Upload the audio file to VideoDB collection. You can upload the audio asset from your local device or from a YouTube URL to upload the audio from its source. Make sure to mention `media_type` if you want to use audio track of a video.

<CodeGroup>
  ```python Python theme={null}
  audio = coll.upload(url="https://youtu.be/_Gd8mbQ3-mI", media_type="audio")
  ```

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

  const audio = await coll.uploadURL({
    url: "https://youtu.be/_Gd8mbQ3-mI",
    mediaType: MediaType.audio
  });
  ```
</CodeGroup>

### Step 4: Create assets

Create assets for Audio and Video using `VideoAsset` and `AudioAsset` from the Editor SDK.

<CodeGroup>
  ```python Python theme={null}
  from videodb.editor import VideoAsset, AudioAsset

  video_asset = VideoAsset(id=video.id, volume=0.5) # Reducing volume of the original video

  audio_asset = AudioAsset(id=audio.id)
  ```

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

  const videoAsset = new EditorVideoAsset({
    id: video.id,
    volume: 0.5 // Reducing volume of the original video
  });

  const audioAsset = new EditorAudioAsset({
    id: audio.id
  });
  ```
</CodeGroup>

### Step 5: Create a Timeline

Create a timeline using the `Track` and `Clip` pattern. Add the video clip and audio clip to a track, then add the track to the timeline.

<CodeGroup>
  ```python Python theme={null}
  from videodb.editor import Timeline, Track, Clip

  # Create a new timeline
  timeline = Timeline(conn)

  # Create a track with video and audio clips
  track = Track()

  # Add video clip (full duration)
  video_clip = Clip(asset=video_asset, duration=float(video.length))
  track.add_clip(0, video_clip)

  # Add audio overlay clip starting at 0
  audio_clip = Clip(asset=audio_asset, duration=float(video.length))
  track.add_clip(0, audio_clip)

  timeline.add_track(track)
  ```

  ```javascript Node.js theme={null}
  import { EditorTimeline, Track, Clip } from 'videodb';

  // Create a new timeline
  const timeline = new EditorTimeline(conn);

  // Create a track with video and audio clips
  const track = new Track();

  // Add video clip (full duration)
  const videoDuration = parseFloat(video.length);
  const videoClip = new Clip({ asset: videoAsset, duration: videoDuration });
  track.addClip(0, videoClip);

  // Add audio overlay clip starting at 0
  const audioClip = new Clip({ asset: audioAsset, duration: videoDuration });
  track.addClip(0, audioClip);

  timeline.addTrack(track);
  ```
</CodeGroup>

### Final Step: Review and Share

Preview the video with the integrated voiceover to ensure it functions correctly. Once satisfied, generate a stream of the video and share the link for others to view and enjoy this wholesome creation!

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

  stream = timeline.generate_stream()
  play_stream(stream)
  ```

  ```javascript Node.js theme={null}
  const stream = await timeline.generateStream();
  console.log(stream);
  ```
</CodeGroup>

<iframe className="w-full aspect-video rounded-xl" src="https://www.youtube.com/embed/WWV_8FtIqiU" title="Audio Overlay Video Output" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

## Conclusion

And there you have it, folks! We've unlocked the door to audio awesomeness with VideoDB's audio overlay feature. By seamlessly integrating background music, voiceovers, or sound effects into your videos, you're not just adding layers of sound – you're adding layers of engagement, emotion, and excitement!

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

## Related Tutorials

<CardGroup cols={2}>
  <Card title="AI Voiceovers" icon="volume-2" href="/examples-and-tutorials/content-factory/voiceovers">
    Generate professional narration with AI voice synthesis
  </Card>

  <Card title="Intro/Outro" icon="film" href="/examples-and-tutorials/programmatic-editing/intro-outro">
    Add polished opening and closing sequences
  </Card>
</CardGroup>
