song plus background mp3 merge pyton
worked perfect
#worked perfect, two songs merging, excelling.
!pip install pydub --quiet
!apt install ffmpeg --quiet
from pydub import AudioSegment
import os
# Input files
ACTUAL_MP3 = "/content/actual.mp3"
BACKGROUND_MP3 = "/content/background.mp3"
OUTPUT_MP3 = "/content/final_with_bg.mp3"
# 🎧 Volume settings (can customize)
actual_gain_db = 0 # 0 dB (100% focus on actual audio)
background_gain_db = -20 # -20 dB (~30% focus on background)
# Actual (speech/music) focus: 100% (volume = 0 dB)
# Background music focus: ~30% (volume = -20 dB)
# Users can modify `background_gain_db` for custom balance
#Background Focus Approx Gain (dB)
#30% focus -20 dB
#40% focus -15 dB
#50% focus -10 dB
#https://prabodhadevotee.blogspot.com/2025/04/song-plus-background-mp3-merge-pyton.html
# Load actual and background audio
actual = AudioSegment.from_file(ACTUAL_MP3)
background = AudioSegment.from_file(BACKGROUND_MP3)
# 🔁 Loop background if shorter than actual
if len(background) < len(actual):
loops_required = (len(actual) // len(background)) + 1
background = background * loops_required
# 🪄 Match background length and apply gain
background = background[:len(actual)].apply_gain(background_gain_db)
actual = actual.apply_gain(actual_gain_db)
# 🎛️ Mix audio (overlay background onto actual)
combined = actual.overlay(background)
# 💾 Export final output
combined.export(OUTPUT_MP3, format="mp3")
print(f"✅ Final mixed audio saved to: {OUTPUT_MP3}")
---------
chatgpt
# 🎧 Default Volume Focus:
# Actual (speech/music) focus: 100% (volume = 0 dB)
# Background music focus: ~30% (volume = -20 dB)
# Users can modify `background_gain_db` for custom balance.
-----------------------------------------
Code: Merge Actual MP3 with Background (Looped if Shorter)