Skip to content

Instantly share code, notes, and snippets.

@woochica
Created January 9, 2026 09:58
Show Gist options
  • Select an option

  • Save woochica/f7880dfeb5d074b873dedf3658da6c39 to your computer and use it in GitHub Desktop.

Select an option

Save woochica/f7880dfeb5d074b873dedf3658da6c39 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import glob
import subprocess
import sys
import os
# Find all mp3 files in current directory
mp3_files = sorted(glob.glob("*.mp3"))
# Check if any mp3 files exist
if not mp3_files:
print("No MP3 files found in current directory")
sys.exit(1)
# Build the concat string with pipe separators
# Escape special characters in filenames for ffmpeg concat protocol
def escape_filename(filename):
"""Escape special characters for ffmpeg concat protocol"""
# Escape backslashes first, then other special characters
escaped = filename.replace("\\", "\\\\")
escaped = escaped.replace("'", "\\'")
escaped = escaped.replace(" ", "\\ ")
escaped = escaped.replace("|", "\\|")
return escaped
concat_string = "|".join(escape_filename(f) for f in mp3_files)
# Output filename
output = "output.mp3"
# Run ffmpeg
print(f"Concatenating {len(mp3_files)} MP3 files...")
print(f"Files: {', '.join(mp3_files)}")
try:
subprocess.run(
["ffmpeg", "-i", f"concat:{concat_string}", "-acodec", "copy", output],
check=True
)
print(f"Done! Output saved to {output}")
except subprocess.CalledProcessError as e:
print(f"Error running ffmpeg: {e}")
sys.exit(1)
except FileNotFoundError:
print("Error: ffmpeg not found. Please install ffmpeg.")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment