Spaces:
Running
Running
Create audio.py
Browse files
audio.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import imageio_ffmpeg as ffmpeg
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def merge_audio_files(mp3_names, silence_duration=500, output_path='result.mp3'):
|
| 6 |
+
"""
|
| 7 |
+
Merges a list of MP3 files into a single audio file with silence in between.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
mp3_names (list): List of MP3 file paths to merge.
|
| 11 |
+
silence_duration (int): Duration of silence (in milliseconds) between audio segments.
|
| 12 |
+
output_path (str): Path to save the resulting merged audio.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: Path to the resulting merged audio file.
|
| 16 |
+
"""
|
| 17 |
+
print(f"DEBUG: mp3_names: '{mp3_names}'")
|
| 18 |
+
|
| 19 |
+
# Get the FFmpeg executable path from imageio_ffmpeg
|
| 20 |
+
ffmpeg_path = ffmpeg.get_ffmpeg_exe()
|
| 21 |
+
print(f"DEBUG: Using FFmpeg executable at: {ffmpeg_path}")
|
| 22 |
+
|
| 23 |
+
# Validate input files
|
| 24 |
+
for mp3_file in mp3_names:
|
| 25 |
+
if not os.path.exists(mp3_file):
|
| 26 |
+
raise FileNotFoundError(f"Audio file '{mp3_file}' not found.")
|
| 27 |
+
|
| 28 |
+
# Generate silence file using FFmpeg
|
| 29 |
+
silence_file = "silence.mp3"
|
| 30 |
+
silence_duration_seconds = silence_duration / 1000 # Convert to seconds
|
| 31 |
+
silence_cmd = [
|
| 32 |
+
ffmpeg_path,
|
| 33 |
+
"-f", "lavfi",
|
| 34 |
+
"-i", "anullsrc=r=44100:cl=mono",
|
| 35 |
+
"-t", str(silence_duration_seconds),
|
| 36 |
+
"-q:a", "9", # Set quality for silence (0 is best, 9 is worst)
|
| 37 |
+
silence_file
|
| 38 |
+
]
|
| 39 |
+
print("DEBUG: Generating silence file...")
|
| 40 |
+
subprocess.run(silence_cmd, check=True, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 41 |
+
|
| 42 |
+
# Create a temporary file to define input list for FFmpeg
|
| 43 |
+
concat_list_path = "concat_list.txt"
|
| 44 |
+
with open(concat_list_path, 'w') as f:
|
| 45 |
+
for i, mp3_file in enumerate(mp3_names):
|
| 46 |
+
# Use absolute paths for FFmpeg compatibility
|
| 47 |
+
f.write(f"file '{os.path.abspath(mp3_file)}'\n")
|
| 48 |
+
if i < len(mp3_names) - 1: # Add silence between files, except after the last
|
| 49 |
+
f.write(f"file '{os.path.abspath(silence_file)}'\n")
|
| 50 |
+
|
| 51 |
+
# Merge audio files with silence and re-encode to ensure compatibility
|
| 52 |
+
try:
|
| 53 |
+
merge_cmd = [
|
| 54 |
+
ffmpeg_path,
|
| 55 |
+
"-f", "concat",
|
| 56 |
+
"-safe", "0",
|
| 57 |
+
"-i", concat_list_path,
|
| 58 |
+
"-c:a", "libmp3lame", # Re-encode to MP3 using libmp3lame
|
| 59 |
+
"-q:a", "2", # Set quality level (0 is best, 9 is worst)
|
| 60 |
+
output_path
|
| 61 |
+
]
|
| 62 |
+
print("DEBUG: Merging audio files...")
|
| 63 |
+
print(f"DEBUG: FFmpeg command: {' '.join(merge_cmd)}")
|
| 64 |
+
result = subprocess.run(merge_cmd, check=True, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 65 |
+
print(f"DEBUG: FFmpeg stdout: {result.stdout}")
|
| 66 |
+
print(f"DEBUG: FFmpeg stderr: {result.stderr}")
|
| 67 |
+
except subprocess.CalledProcessError as e:
|
| 68 |
+
print(f"DEBUG: FFmpeg error output: {e.stderr}")
|
| 69 |
+
print(f"DEBUG: FFmpeg standard output: {e.stdout}")
|
| 70 |
+
raise RuntimeError(f"FFmpeg merging failed: {e}")
|
| 71 |
+
finally:
|
| 72 |
+
# Clean up temporary files
|
| 73 |
+
if os.path.exists(concat_list_path):
|
| 74 |
+
os.remove(concat_list_path)
|
| 75 |
+
if os.path.exists(silence_file):
|
| 76 |
+
os.remove(silence_file)
|
| 77 |
+
|
| 78 |
+
print('DEBUG: Audio merging complete!')
|
| 79 |
+
return output_path
|
| 80 |
+
|
| 81 |
+
# Test the function with audio_0.mp3 and audio_1.mp3
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
mp3_files = ["audio_0.mp3", "audio_1.mp3"] # Ensure these files exist in your directory
|
| 84 |
+
try:
|
| 85 |
+
output_file = merge_audio_files(mp3_files, silence_duration=500, output_path="merged_audio_result.mp3")
|
| 86 |
+
print(f"Audio files merged successfully. Output saved at: {output_file}")
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"An error occurred during the merging process: {e}")
|