Tested and working simple function to join MP3 audio files one after another from given file path's list in Python
import os
import subprocess
def join_mp3s(input_paths, output_path):
if not input_paths:
raise ValueError("Input paths cannot be empty.")
if not output_path:
raise ValueError("Output path cannot be missing.")
# Create a temporary file list using a unique identifier
temp_list_path = f"{output_path}.concat"
with open(temp_list_path, "w") as f:
for path in input_paths:
f.write(f"file '{path}'\n")
# Construct the ffmpeg command
command = ["ffmpeg", "-f", "concat", "-safe", "0",
"-i", temp_list_path, "-c", "copy", output_path, "-y"]
# Execute ffmpeg and handle potential errors
try:
subprocess.run(command, check=True, stderr=subprocess.STDOUT) # Combine error streams
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Failed to join MP3s: {e.output.decode()}")
finally:
os.remove(temp_list_path) # Always clean up the temporary file
Comments
Post a Comment