Audio overlay in videos: Step-by-step guide using FFmpeg and FastPix API

October 9, 2024
10 Min
Video Engineering
Jump to
Share
This is some text inside of a div block.

Overlaying audio onto video is a common task in video editing. Studies show that 60% of viewers engage more with videos that have good audio, while 33% stop watching if the audio is poor within the first 30 seconds. In this blog, we’ll explore how to easily add audio to video using tools like FFmpeg and the FastPix API, making sure everything is synced perfectly.

What is audio overlay?

Why audio overlay matters:

Audio overlay is important in areas like video production, ads, and podcasts. If the audio isn’t properly synced with the video, it can hurt the viewer’s experience, leading to confusion or making them stop watching.

A well-done audio overlay ensures that sound effects, music, and dialogue are perfectly timed. Whether you’re editing videos or using APIs to add audio, mastering this skill is essential for creating professional media. Tools like FFmpeg or FastPix API allow developers to control audio tracks and make sure everything stays in sync for top-quality results.

Step-by-step approach to overlaying audio on video

Before we jump into the technical aspects, it's important to know what tools you'll need. FFmpeg is a popular choice for audio and video processing. In this guide, we'll use FFmpeg as our initial tool, but we’ll also discuss other advanced techniques for different user levels and platforms.

1. Understanding the timeline

To properly overlay audio, you must understand your timeline. A timeline is a visual representation of a video's sequence of events, where you can arrange and edit video clips, audio, and other media. The most critical elements to focus on are:

  • Timestamps: Each point in the timeline corresponds to a specific moment in the video.
  • Audio sync points: If you’re adding audio that must sync with specific actions in the video (e.g., dialogue or effects), you must ensure they align perfectly.

2. Preparing your files

You'll need your video file and the audio file you plan to overlay. Ensure that both are in compatible formats FFmpeg supports a wide range of formats, including MP4, MOV, WAV, and MP3.

Here’s an example setup:

  • Video file: video.mp4
  • Audio file: background_music.mp3

3. Basic audio overlay using FFmpeg

FFmpeg is powerful for this task because it provides detailed control over timing, quality, and transitions.

Here’s a simple command to overlay audio:

Code snippet:

1ffmpeg -i video.mp4 -i background_music.mp3 -c:v copy -c:a aac -strict experimental output.mp4

  • - `-i video.mp4`: This specifies the input video file.
  • - `-i background_music.mp3`: This adds the audio file.
  • - `-c:v copy`: Copies the video without re-encoding.
  • - `-c:a aac`: Encodes the audio using AAC codec for compatibility.

This command:

  • Takes the video from video.mp4
  • Adds the audio from background_music.mp3
  • Copies the video codec (-c:v copy) without changing the video quality
  • Encodes the audio with the AAC codec
  • Outputs the result as output.mp4

4. Syncing audio with the video timeline

If you need the audio to start at a specific time, you can adjust the timing with FFmpeg’s its offset option:

Code snippet:

1ffmpeg -i video.mp4 -itsoffset 00:00:05 -i background_music.mp3 -c:v copy -c:a aac -strict experimental output.mp4

In this example, the audio will start 5 seconds after the video begins. This approach is useful if the audio should align with a certain event or visual cue in the video.

5. Advanced synchronization using time stamps

Sometimes, basic time offsets aren't enough. For instance, you might need to fade in music at a particular moment or adjust audio levels over time. FFmpeg allows you to control audio precisely along the timeline:

Fade in:

1ffmpeg -i video.mp4 -i background_music.mp3 -filter_complex "[1]afade=t=in:st=5:d=3" -c:v copy -c:a aac output.mp4

This command fades the audio in, starting at 5 seconds and lasting for 3 seconds.

Adjusting audio volume:

1ffmpeg -i video.mp4 -i background_music.mp3 -filter_complex "[1]volume=0.5" -c:v copy -c:a aac output.mp4

This command reduces the background music volume by half.

The above commands using ffmpeg requires comprehensive knowledge on video engineering and is a tedious process for beginners or developers who are looking for a quick solution without many intricacies. There are many platforms which provide no-code solutions for integrating these features and one of the best options is FastPix API

FastPix API for seamless audio overlay

Unlike FFmpeg, FastPix simplifies the process by allowing you to specify the audio track and overlay timing with just a few JSON parameters. FastPix is inherently scalable with API-based processing, making it suitable for dynamic workloads like video-on-demand services. FastPix API allows to overlay any number of audio on the video timeline by specifying the “imposeTracks” parameter in the payload to create media from url or by uploading the media.

Step-by-step guide to overlay audio on video

The imposeTracks parameter takes a list of json objects. Each json object has the url to the overlay media file, start time at which the audio should be overlaid on the video timeline, end time till which the audio should be overlaid on the media file, fade in duration and fade out duration all in seconds.

Payload for creating media from url with impose audio tracks:

1POST https://v1.fastpix.io/on-demand 
2
3"inputs": [ 
4        { 
5            "type": "video", 
6            "url": "https://static.fastpix.io/sample.mp4", 
7            "startTime": 0, 
8            "endTime": 60 
9         }, 
10        { 
11            "type": "audio", 
12            "imposeTracks": [ 
13                { 
14                    "url": "https://fastpix-audio.com/example-impose-audio-track.m4a", 
15                    "startTime": 0, 
16                    "endTime": 5, 
17                    "fadeInLevel": 1, 
18                    "fadeOutLevel": 4 
19                } 
20            ] 
21        } 
22	] 

In the above payload imposeTracks parameter in the input with “type”:”audio” contains a list of json objects. The parameters provided in the imposeTracks help in understanding the start and end time of the overlay audio on the video and the fade in, fade out duration.

You can also provide multiple overlay audio for timestamps on the timeline for different use cases like this:

Let's assume that there is a 1-minute video on which you wish to overlay different audio at different timestamps with different fade-ins and fade-outs. A normal approach with code will make the command very sophisticated and very hard to understand. From the payload example below, you can do this task with ease.

1POST https://v1.fastpix.io/on-demand 
2
3"inputs": [ 
4        { 
5            "type": "video", 
6            "url": " https://static.fastpix.io/sample.mp4", 
7            "startTime": 0, 
8            "endTime": 60 
9         }, 
10        { 
11            "type": "audio", 
12            "imposeTracks": [ 
13                { 
14                    "url": "https://fastpix-audio.com/example-impose-audio-track.m4a", 
15                    "startTime": 0, 
16                    "endTime": 13, 
17                    "fadeInLevel": 1, 
18                    "fadeOutLevel": 4 
19                }, 
20
21{ 
22                    "url": "https://fastpix-audio.com/example-impose-audio-track-1.m4a", 
23                    "startTime": 14, 
24                    "endTime": 23, 
25                    "fadeInLevel": 1, 
26                    "fadeOutLevel":2  
27              }, 
28
29{ 
30                    "url": "https://fastpix-audio.com/example-impose-audio-track-2.m4a", 
31                    "startTime": 24, 
32                    "endTime": 60, 
33                    "fadeInLevel": 2, 
34                    "fadeOutLevel": 4 
35                } 
36            ] 
37        } 
38  ] 

Here, in the above payload, we can see that there are multiple JSON objects in the list of imposeTracks. Each json object has its own URL, start and end times.

This helps in easy audio overlay onto any video asset with customizable options.

Similarly, you can use the FastPix API for overlaying audio on video timeline for upload media.

This way you can implement audio overlay with ease on the video timeline without using complex code and enjoy the various applications of overlaying audio with ease.

Save Time with Video Search

Know more

Application of audio overlay:

1. Media production (Film, TV, and video)

  • Background music: Adding music over dialogue or sound effects to enhance the emotional impact or set the tone of a scene.
  • Voiceovers: Inserting narration over video or other audio, commonly used in documentaries, instructional videos, or commercials.
  • Sound effects: Overlaying sound effects (like footsteps, explosions, etc.) onto a video to complement visuals.

2. Podcasts

  • Intro/outro music: Adding music or sound bites at the beginning and end of a podcast episode for branding or setting a mood.
  • Interviews: Layering interview recordings with background audio such as ambiance or promotional material.

3. Gaming

  • In-game narration: Adding commentary or narrative tracks over gameplay to guide or inform players.
  • Soundtracks: Overlaying music tracks or sound effects that enhance the gaming experience.

4. Advertising

  • Jingles and slogans: Overlaying a catchy tune or voiceover onto a product demonstration or promotional video to capture attention.
  • Audio branding: Creating a unique sound identity for a brand by overlaying specific sound bites in ads.

5. Music production

  • Mixing tracks: Combining different vocal and instrumental tracks to create a cohesive song.
  • Mashups: Overlaying multiple songs or sounds to create new, innovative mixes.
  • Remixes: Re-arranging or adding additional audio elements to an existing track to produce a new version.

6. Education

  • E-learning and training videos: Overlaying voiceover instructions or explanations over instructional content.
  • Language learning: Overlaying translations or pronunciation guides onto foreign language audio or video material.

7. Virtual and augmented reality (VR/AR)

  • Immersive sound design: Using spatial audio overlays to simulate a more immersive experience, such as virtual environments where background music or sounds react to user interactions.

These are just a few applications, and audio overlay is essential for enhancing the auditory experience in both creative and functional contexts.

Advanced media with FastPix API:

FastPix API can be used to create advanced media with its diverse and easy to integrate features, making it a perfect replacement for the developers trying to develop video editing tools or any of its applications.  

Here we discuss some approaches where we can integrate various features together to get the best video transformation with ease just from the FastPix API.

Audio replace + audio overlay

Case study: Replacing audio and adding background music at specific timestamps

With the FastPix API, replacing audio in a video file and adding background music (BGM) at specific timestamps is a straightforward process. The API allows you to seamlessly replace the existing audio and overlay BGMs in one operation. This can all be done using a single API payload, as demonstrated below:

Audio replace and audio overlay with FastPix

1POST https://v1.fastpix.io/on-demand 
2
3"inputs": [ 
4        { 
5            "type": "video", 
6            "url": "https://static.fastpix.io/sample.mp4", 
7            "startTime": 0, 
8            "endTime": 60 
9        }, 
10        { 
11            "type": "audio", 
12
13“swapTrackUrl”: “https://fastpix-audio.com/example-impose-audio-track.m4a” 
14            "imposeTracks": [ 
15                { 
16                    "url": "https://fastpix-audio.com/example-impose-audio-track.m4a", 
17                    "startTime": 0, 
18                    "endTime": 5, 
19                    "fadeInLevel": 1, 
20                    "fadeOutLevel": 4 
21                } 
22            ] 
23        } 
24  ] 

In the payload above, the main video URL is provided, along with audio tracks for replacement and overlay. The 'swap' track replaces the existing audio, while the 'impose' tracks are used to overlay new audio at specified timestamps. This allows both operations audio replacement and overlaying to be performed in a single request.

Intro + outro + audio overlay

add Intro and outro with audio overlay on video with FastPix

Case Study: Adding intro, outro, and background music to a video

With the FastPix API, you can add an intro and outro to a video file while also overlaying background music (BGM) at specific timestamps. This entire operation can be executed using a single API payload, as demonstrated below:

1POST https://v1.fastpix.io/on-demand 
2
3"inputs": [ 
4        { 
5            "type": "video", 
6            "url": “https://static.fastpix.io/sample.mp4”, 
7
8“introUrl”: “https://static.fastpix.io/sample-1.mp4”, 
9
10“outroUrl”: “https://static.fastpix.io/sample-2.mp4” 
11            "startTime": 0, 
12            "endTime": 60 
13       }, 
14        { 
15            "type": "audio", 
16            "imposeTracks": [ 
17                { 
18                    "url": "https://fastpix-audio.com/example-impose-audio-track.m4a", 
19                    "startTime": 0, 
20                    "endTime": 5, 
21                    "fadeInLevel": 1, 
22                    "fadeOutLevel": 4 
23                } 
24            ] 
25        } 
26  ] 

In the above payload we can see that there is a URL of the main video and then the intro and outro urls and the impose tracks are mentioned in the type “audio”. This helps in replacing the audio and then overlay audio at the specified timestamps with just one payload.

Audio overlay + trim media

Overlay and trim media with FastPix

Case study: Trim a video and overlay audio at specific timestamps.

You can perform this operation using a single payload as shown below:

1POST https://v1.fastpix.io/on-demand 
2
3"inputs": [ 
4        { 
5            "type": "video", 
6            "url": “https://static.fastpix.io/sample.mp4”, 
7            "startTime": 0, 
8            "endTime": 30 
9      }, 
10        { 
11            "type": "audio", 
12            "imposeTracks": [ 
13                { 
14                    "url": "https://fastpix-audio.com/example-impose-audio-track.m4a", 
15                    "startTime": 0, 
16                    "endTime": 5, 
17                    "fadeInLevel": 1, 
18                    "fadeOutLevel": 4 
19                } 
20            ] 
21        } 
22  ] 

In the above payload we can see that there are startTime and endTime parameters for specifying the trim duration and URL of the main video and the impose tracks are mentioned in the type “audio”. This helps in trimming and overlaying audio at the specified timestamps with just one payload.

Conclusion

Whether you’re using the command-line power of FFmpeg or the simplicity of FastPix API, mastering audio overlay is essential for creating high-quality video content. FFmpeg gives you precise control over every aspect of the process, while FastPix allows for rapid integration and customization through easy-to-use API calls. With these tools, you’re now equipped to enhance your video projects with perfectly timed audio. Try them out today and see how much more immersive your videos can become.

FAQs:

How does FFmpeg perform compared to FastPix API in terms of processing speed?

FFmpeg is highly optimized for local performance. It can process a 1-minute 1080p video with audio overlay in approximately X seconds on average, depending on the hardware. Whereas FastPix shifts the processing to the cloud, which saves local resources. The response time for API requests varies, but it’s typically fast enough for most cloud-based video workflows.

Which option is more scalable: FFmpeg or FastPix API?

While FFmpeg can handle large volumes of video processing, it requires complex configurations for scaling, like setting up clusters or integrating with cloud services like AWS. FastPix API is inherently scalable due to its cloud-based nature, making it easier to manage dynamic workloads such as video-on-demand services.

What are the cost implications of using FFmpeg versus FastPix API?

FFmpeg itself is open-source and free to use, but infrastructure costs can add up if you need to maintain servers and handle scaling. FastPix API typically follows a pay-as-you-go model. Costs depend on the number of media processed, making it affordable for developers who want to avoid heavy upfront infrastructure costs.

Which option integrates better with modern workflows?

FFmpeg works well in custom-built video pipelines, but it might require additional configurations for CI/CD or cloud-based architectures. FastPix API seamlessly integrates with cloud environments, CI/CD pipelines, and can easily be used in microservice-based architectures.

How do FFmpeg and FastPix API handle error debugging?

FFmpeg provides extensive logging, which is helpful for debugging, but it can require significant manual intervention and deep knowledge to interpret. FastPix simplifies error handling with detailed JSON responses, making it quicker for developers to troubleshoot issues, especially in API-driven workflows.

Try for Free

Enjoyed reading? You might also like

Try FastPix today!

FastPix grows with you – from startups to growth stage and beyond.