-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCombineAudio.py
81 lines (67 loc) · 2.71 KB
/
CombineAudio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import sys
from pydub import AudioSegment
class CombineAudio:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"audio1": ("AUDIO",),
"audio2": ("AUDIO",),
"volume1": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 2.0,
"step": 0.1
}),
"volume2": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 2.0,
"step": 0.1
}),
},
}
RETURN_TYPES = ("AUDIO",)
FUNCTION = "combine_audio"
CATEGORY = "audio"
def combine_audio(self, audio1, audio2, volume1=1.0, volume2=1.0):
try:
# Load the audio files using pydub
sound1 = AudioSegment.from_file(audio1.file)
sound2 = AudioSegment.from_file(audio2.file)
# Apply volume adjustments (pydub uses dB, so we convert from linear scale)
if volume1 != 1.0:
sound1 = sound1 + (20 * float(volume1)) # Convert to dB
if volume2 != 1.0:
sound2 = sound2 + (20 * float(volume2)) # Convert to dB
# Match lengths
if len(sound1) > len(sound2):
sound2 = sound2 + AudioSegment.silent(duration=len(sound1) - len(sound2))
else:
sound1 = sound1 + AudioSegment.silent(duration=len(sound2) - len(sound1))
# Combine audio (overlay maintains both tracks)
combined = sound1.overlay(sound2)
# Export the combined audio
output_path = os.path.join(os.path.dirname(audio1.file), "combined_audio.mp4")
combined.export(output_path, format="mp4")
print(f"Successfully created combined audio at: {output_path}", file=sys.stderr)
# Create new LazyAudioMap with the combined audio
combined_audio = audio1.__class__(
file=output_path,
start_time=0.0,
duration=max(audio1.duration, audio2.duration) if hasattr(audio1, 'duration') and hasattr(audio2, 'duration') else len(combined) / 1000.0
)
return (combined_audio,)
except Exception as e:
print(f"Error combining audio: {str(e)}", file=sys.stderr)
# Return original audio for debugging purposes
return (audio1,)
NODE_CLASS_MAPPINGS = {
"CombineAudio": CombineAudio
}
NODE_DISPLAY_NAME_MAPPINGS = {
"CombineAudio": "Combine Audio Tracks"
}