Skip to content

Instantly share code, notes, and snippets.

@chpatrick
Created May 8, 2024 10:45
Show Gist options
  • Save chpatrick/a1dd81a400609cea7c3a9474885f5482 to your computer and use it in GitHub Desktop.
Save chpatrick/a1dd81a400609cea7c3a9474885f5482 to your computer and use it in GitHub Desktop.
Python script for copying podcasts/audiobooks to an MP3 player. This is useful for waterproof MP3 players like the Shokz OpenSwim that have limited controls. This will take any format, apply loudness normalization, split it into 5 minute chunks and copy it to the player as MP3. Requires ffmpeg and mp3splt.
import argparse
from pathlib import Path
import tempfile
from subprocess import check_call
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--input", type=Path, required=True)
parser.add_argument("--media-dir", type=Path, default=Path("/run/media/your_user/your_device"))
args = parser.parse_args()
if not args.media_dir.is_dir():
raise RuntimeError("Player not mounted.")
out_dir = args.media_dir / args.input.stem.replace(".", "_")
print(f"Writing to {out_dir}")
with tempfile.TemporaryDirectory() as tmp_dir:
normalized_file = Path(tmp_dir) / "normalized.mp3"
check_call([
"ffmpeg",
"-i", str(args.input),
"-filter:a", "speechnorm=e=6.25:r=0.00001:l=1",
"-b:a", "192k",
str(normalized_file),
])
check_call([
"mp3splt",
str(normalized_file),
"-t", "5.0",
"-a",
"-d", str(out_dir),
])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment