File size: 5,752 Bytes
af4851b | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import json
import shutil
import subprocess
import wave
import zipfile
from pathlib import Path
from typing import Dict, Iterable, List, Tuple
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC, TIT2, TPE1
def export_audiobook(
*,
session_root: Path,
chapters: List[Dict[str, object]],
output_format: str,
metadata: Dict[str, str],
embed_markers: bool,
cover_path: Path | None = None,
) -> Dict[str, object]:
exports_dir = session_root / "exports"
exports_dir.mkdir(parents=True, exist_ok=True)
rendered = _rendered_files(session_root)
if not rendered:
raise ValueError("No rendered chapters available for export")
if output_format == "zip":
archive = exports_dir / "reading-room-chapters.zip"
with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED) as zf:
for source in rendered:
zf.write(source, arcname=source.name)
return {"file": str(archive), "format": "zip", "size_bytes": archive.stat().st_size}
base_wav = exports_dir / "reading-room.wav"
total_seconds = _concat_wav(rendered, base_wav)
chapter_offsets = _chapter_offsets(chapters)
if output_format == "m4a":
output = exports_dir / "reading-room.m4a"
_ffmpeg_transcode(
source=base_wav,
target=output,
codec_args=["-c:a", "aac", "-b:a", "96k"],
metadata=metadata,
chapter_offsets=chapter_offsets if embed_markers else None,
)
elif output_format == "mp3":
output = exports_dir / "reading-room.mp3"
_ffmpeg_transcode(
source=base_wav,
target=output,
codec_args=["-c:a", "libmp3lame", "-b:a", "128k"],
metadata=metadata,
chapter_offsets=None,
)
_tag_mp3(output, metadata, cover_path)
else:
raise ValueError("Unsupported export format")
return {
"file": str(output),
"format": output_format,
"runtime_seconds": total_seconds,
"size_bytes": output.stat().st_size,
"chapter_count": len(chapters),
}
def _rendered_files(session_root: Path) -> List[Path]:
render_dir = session_root / "renders"
return sorted(render_dir.glob("*.wav"))
def _concat_wav(inputs: Iterable[Path], output: Path) -> int:
inputs = list(inputs)
params = None
total_frames = 0
frames: List[bytes] = []
for wav_path in inputs:
with wave.open(str(wav_path), "rb") as handle:
if params is None:
params = handle.getparams()
elif handle.getframerate() != params.framerate:
raise ValueError("Mismatched sample rates across rendered chapters")
chunk = handle.readframes(handle.getnframes())
frames.append(chunk)
total_frames += handle.getnframes()
if params is None:
raise ValueError("No WAV inputs to concatenate")
with wave.open(str(output), "wb") as handle:
handle.setparams(params)
for chunk in frames:
handle.writeframes(chunk)
return int(total_frames / params.framerate)
def _ffmpeg_transcode(
*,
source: Path,
target: Path,
codec_args: List[str],
metadata: Dict[str, str],
chapter_offsets: List[Tuple[str, int, int]] | None,
) -> None:
target.parent.mkdir(parents=True, exist_ok=True)
ffmetadata_path = None
cmd = ["ffmpeg", "-y", "-i", str(source)]
if chapter_offsets:
ffmetadata_path = target.with_suffix(".ffmeta")
ffmetadata_path.write_text(_ffmetadata(metadata, chapter_offsets), encoding="utf-8")
cmd.extend(["-i", str(ffmetadata_path), "-map_metadata", "1"])
else:
for key, value in metadata.items():
cmd.extend(["-metadata", f"{key}={value}"])
cmd.extend(codec_args)
cmd.append(str(target))
subprocess.run(cmd, check=True, capture_output=True)
def _ffmetadata(metadata: Dict[str, str], chapter_offsets: List[Tuple[str, int, int]]) -> str:
lines = [";FFMETADATA1"]
for key, value in metadata.items():
lines.append(f"{key}={value}")
for title, start, end in chapter_offsets:
lines.extend(
[
"[CHAPTER]",
"TIMEBASE=1/1000",
f"START={start}",
f"END={end}",
f"title={title}",
]
)
return "\n".join(lines)
def _chapter_offsets(chapters: List[Dict[str, object]]) -> List[Tuple[str, int, int]]:
offsets = []
start_ms = 0
for chapter in chapters:
duration_ms = int(chapter.get("duration_seconds", 0) * 1000)
offsets.append((str(chapter["title"]), start_ms, start_ms + duration_ms))
start_ms += duration_ms
return offsets
def _tag_mp3(path: Path, metadata: Dict[str, str], cover_path: Path | None) -> None:
try:
tags = EasyID3(str(path))
except Exception:
tags = EasyID3()
tags.save(str(path))
tags = EasyID3(str(path))
if metadata.get("title"):
tags["title"] = [metadata["title"]]
if metadata.get("artist"):
tags["artist"] = [metadata["artist"]]
tags.save()
if cover_path and cover_path.exists():
audio = ID3(str(path))
audio.add(
APIC(
encoding=3,
mime="image/jpeg",
type=3,
desc="Cover",
data=cover_path.read_bytes(),
)
)
if metadata.get("title"):
audio.add(TIT2(encoding=3, text=metadata["title"]))
if metadata.get("artist"):
audio.add(TPE1(encoding=3, text=metadata["artist"]))
audio.save(str(path))
|