Repository navigation
Expand file tree
/
Copy pathencoder.py
More file actions
468 lines (390 loc) · 14.6 KB
/
Copy pathencoder.py
File metadata and controls
468 lines (390 loc) · 14.6 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
"""FFmpeg encoding logic for audio-transcode-watcher."""
from __future__ import annotations
import logging
import os
import subprocess
from collections.abc import Callable
import mutagen
from mutagen.apev2 import APETextValue
from mutagen.mp4 import MP4, MP4FreeForm
from .config import OutputConfig
from .utils import nfc_path
logger = logging.getLogger(__name__)
# Exit code returned when ffmpeg exits 0 but reported a decode error.
DECODE_ERROR_RC = 69
# A single encode that runs longer than this (seconds) is killed and counts
# as a failure, so a hung ffmpeg cannot stall the sync for good.
FFMPEG_TIMEOUT = 1800
TIMEOUT_RC = 124
# stderr text that means the audio did not decode cleanly, even when
# ffmpeg exits 0 (a corrupt FLAC used to come out seconds short).
_DECODE_ERROR_HINTS = ("decode_frame() failed", "invalid", "error while decoding")
# stderr text that points at the attached picture (cover art) stream.
# Only these trigger the retry without artwork.
_ARTWORK_ERROR_HINTS = (
"could not find tag for codec",
"attached pic",
"video stream",
"mjpeg",
"png",
"vist#",
"vf#",
)
# Tags ffmpeg drops on an ALAC or AAC encode, copied from the source after
# it: source tag name -> MP4 atom. The atom names are the ones MusicBrainz
# Picard writes, so Picard, TagLib (Navidrome, Jellyfin), foobar2000 and
# iTunes all recognise them. Sort names go to the standard sort atoms.
_FREEFORM = "----:com.apple.iTunes:"
MP4_TAG_ATOMS = {
"REPLAYGAIN_TRACK_GAIN": _FREEFORM + "replaygain_track_gain",
"REPLAYGAIN_TRACK_PEAK": _FREEFORM + "replaygain_track_peak",
"REPLAYGAIN_ALBUM_GAIN": _FREEFORM + "replaygain_album_gain",
"REPLAYGAIN_ALBUM_PEAK": _FREEFORM + "replaygain_album_peak",
"MUSICBRAINZ_TRACKID": _FREEFORM + "MusicBrainz Track Id",
"MUSICBRAINZ_ALBUMID": _FREEFORM + "MusicBrainz Album Id",
"MUSICBRAINZ_ARTISTID": _FREEFORM + "MusicBrainz Artist Id",
"MUSICBRAINZ_ALBUMARTISTID": _FREEFORM + "MusicBrainz Album Artist Id",
"MUSICBRAINZ_RELEASEGROUPID": _FREEFORM + "MusicBrainz Release Group Id",
"ISRC": _FREEFORM + "ISRC",
"LABEL": _FREEFORM + "LABEL",
"CATALOGNUMBER": _FREEFORM + "CATALOGNUMBER",
"ARTISTSORT": "soar",
"ALBUMARTISTSORT": "soaa",
"ALBUMSORT": "soal",
}
# How the same tags are stored in ID3 (WAV, AIFF and MP3 sources).
_ID3_FRAMES = {
"ISRC": "TSRC",
"LABEL": "TPUB",
"ARTISTSORT": "TSOP",
"ALBUMARTISTSORT": "TSO2",
"ALBUMSORT": "TSOA",
}
_ID3_TXXX = {
"MUSICBRAINZ_ALBUMID": "MusicBrainz Album Id",
"MUSICBRAINZ_ARTISTID": "MusicBrainz Artist Id",
"MUSICBRAINZ_ALBUMARTISTID": "MusicBrainz Album Artist Id",
"MUSICBRAINZ_RELEASEGROUPID": "MusicBrainz Release Group Id",
}
def build_ffmpeg_command(
source: str,
dest: str,
output_config: OutputConfig,
) -> list[str]:
"""
Build an FFmpeg command for transcoding.
Args:
source: Path to source audio file
dest: Path to destination file
output_config: Output configuration
Returns:
FFmpeg command as list of arguments
"""
source = nfc_path(source)
dest = nfc_path(dest)
# Common arguments
# -err_detect crccheck+explode makes the decoder verify each frame's
# checksum and stop on a mismatch. Without it ffmpeg 7.1 (the image's)
# decodes a FLAC with one flipped bit silently.
cmd = [
"ffmpeg",
"-loglevel",
"error",
"-xerror",
"-y",
"-err_detect",
"crccheck+explode",
"-i",
source,
"-map",
"0:a:0", # First audio stream
]
# Add video/artwork mapping if enabled
if output_config.include_artwork:
cmd.extend(["-map", "0:v:0?"]) # First video/image stream (optional)
# Copy metadata
cmd.extend(["-map_metadata", "0"])
# Codec-specific options
codec = output_config.codec
if codec == "alac":
cmd.extend(["-c:a", "alac"])
if output_config.include_artwork:
cmd.extend(["-c:v", "copy"])
# Ensure album_artist is mapped correctly for M4A (aART tag)
cmd.extend(["-movflags", "+faststart", "-f", "mp4"])
elif codec == "aac":
cmd.extend(["-c:a", "aac", "-b:a", output_config.bitrate])
if output_config.include_artwork:
cmd.extend(["-c:v", "copy"])
cmd.extend(["-movflags", "+faststart", "-f", "mp4"])
elif codec == "mp3":
cmd.extend(["-c:a", "libmp3lame", "-b:a", output_config.bitrate])
if output_config.include_artwork:
# MP3 needs mjpeg for ID3 APIC artwork
cmd.extend(["-c:v", "mjpeg"])
cmd.extend(["-id3v2_version", "3", "-write_id3v2", "1", "-f", "mp3"])
elif codec == "opus":
cmd.extend(["-c:a", "libopus", "-b:a", output_config.bitrate])
cmd.extend(["-f", "opus"])
elif codec == "flac":
cmd.extend(["-c:a", "flac"])
if output_config.include_artwork:
cmd.extend(["-c:v", "copy"])
cmd.extend(["-f", "flac"])
elif codec == "wav":
cmd.extend(["-c:a", "pcm_s16le", "-f", "wav"])
else:
raise ValueError(f"Unsupported codec: {codec}")
cmd.extend(portable_limit_args(source, output_config))
cmd.append(dest)
return cmd
def target_sample_rate(source_rate: int, max_rate: int) -> int | None:
"""
Return the rate to resample *source_rate* to under *max_rate*, or None.
Never upsamples. A rate above the cap drops to the highest rate of its
own family that fits: 88.2 and 176.4 kHz go to 44.1 kHz, 96 and 192 kHz
to 48 kHz. A rate from neither family, or a family with no rate under
the cap, goes to the cap itself.
"""
if not max_rate or not source_rate or source_rate <= max_rate:
return None
for base in (44100, 48000):
if source_rate % base == 0:
rate = base
while rate * 2 <= max_rate:
rate *= 2
while rate > max_rate and rate % 2 == 0:
rate //= 2
return min(rate, max_rate)
return max_rate
def limit_args(
source_rate: int | None, source_channels: int | None, output: OutputConfig
) -> list[str]:
"""ffmpeg arguments that apply the output's channel and sample-rate limits."""
args: list[str] = []
if output.channels and source_channels and source_channels > output.channels:
args.extend(["-ac", str(output.channels)])
rate = target_sample_rate(source_rate or 0, output.max_sample_rate or 0)
if rate:
args.extend(["-ar", str(rate)])
return args
def _probe_shape(source: str) -> tuple[int | None, int | None]:
"""Return (sample rate, channels) of *source*, or Nones if unreadable."""
try:
info = mutagen.File(source).info
except Exception: # noqa: BLE001 - any unreadable file means "unknown"
return None, None
return getattr(info, "sample_rate", None), getattr(info, "channels", None)
def portable_limit_args(source: str, output: OutputConfig) -> list[str]:
"""Probe *source* only when *output* has a limit, and return its arguments."""
if not output.channels and not output.max_sample_rate:
return []
rate, channels = _probe_shape(source)
return limit_args(rate, channels, output)
def _remove_artwork_from_command(cmd: list[str]) -> list[str]:
"""
Remove artwork-related options from an FFmpeg command.
Used for retry when artwork encoding fails.
"""
filtered = []
i = 0
while i < len(cmd):
arg = cmd[i]
# Skip -map 0:v:0? pair
if arg == "-map" and i + 1 < len(cmd) and cmd[i + 1] == "0:v:0?":
i += 2
continue
# Skip -c:v and its value
if arg == "-c:v":
i += 2
continue
# Skip -vf and its value
if arg.startswith("-vf"):
i += 2
continue
filtered.append(arg)
i += 1
return filtered
def _source_from_cmd(cmd: list[str]) -> str:
"""Return the input file of an ffmpeg command, for log lines."""
try:
return cmd[cmd.index("-i") + 1]
except ValueError, IndexError:
return "?"
def _decode_error(stderr: str) -> str | None:
"""Return the first stderr line that reports a decode error, if any."""
for line in stderr.splitlines():
low = line.lower()
if any(h in low for h in _DECODE_ERROR_HINTS):
return line.strip()
return None
def _is_artwork_error(stderr: str) -> bool:
"""True when stderr points at the attached picture stream."""
low = stderr.lower()
return any(h in low for h in _ARTWORK_ERROR_HINTS)
def tolerant_command(cmd: list[str]) -> list[str]:
"""*cmd* without -xerror and -err_detect, so ffmpeg conceals decode errors."""
out: list[str] = []
skip = False
for i, arg in enumerate(cmd):
if skip:
skip = False
continue
if arg == "-xerror":
continue
if arg == "-err_detect" and i + 1 < len(cmd):
skip = True
continue
out.append(arg)
return out
def _run_ffmpeg(cmd: list[str], strict: bool = True) -> tuple[int, str]:
"""
Run ffmpeg and return (rc, stderr).
Strict: a decode error on stderr is a failure even with rc 0, and any
failure that reports a decode error returns DECODE_ERROR_RC. Not strict:
only the exit code counts (concealed errors still print to stderr).
"""
try:
proc = subprocess.run(
cmd, capture_output=True, timeout=FFMPEG_TIMEOUT, check=False
)
except subprocess.TimeoutExpired:
return TIMEOUT_RC, f"ffmpeg timed out after {FFMPEG_TIMEOUT} s"
stderr = proc.stderr.decode("utf-8", errors="ignore") if proc.stderr else ""
rc = proc.returncode
if strict and _decode_error(stderr):
rc = DECODE_ERROR_RC
return rc, stderr
def atomic_ffmpeg_encode(
cmd: list[str],
final_dest: str,
retry_without_artwork: bool = True,
finalize: Callable[[str], None] | None = None,
strict: bool = True,
) -> int:
"""
Run FFmpeg with atomic output (write to temp, then rename).
Args:
cmd: FFmpeg command (last element is destination)
final_dest: Final destination path
retry_without_artwork: If True, retry without artwork when the
failure points at the attached picture stream
finalize: Optional callable run on the finished temp file before
the rename (used to copy extra tags)
strict: If True, a decode error reported on stderr fails the encode
and the result is DECODE_ERROR_RC. False for the tolerant retry.
Returns:
Return code (0 for success, DECODE_ERROR_RC when the source did not
decode cleanly). No output is written on failure.
"""
final_dest = nfc_path(final_dest)
dest_dir = os.path.dirname(final_dest)
os.makedirs(dest_dir, exist_ok=True)
tmp_dest = final_dest + ".tmp__ff"
# Clean up any stale temp file
try:
if os.path.exists(tmp_dest):
os.remove(tmp_dest)
except Exception:
pass
# Replace destination with temp path
cmd = list(cmd)
cmd[-1] = tmp_dest
source = _source_from_cmd(cmd)
logger.info("► %s", " ".join(cmd))
rc, stderr = _run_ffmpeg(cmd, strict)
if rc != 0 and retry_without_artwork and _is_artwork_error(stderr):
_cleanup_temp(tmp_dest)
logger.warning("Retrying without cover art for %s", final_dest)
cmd = _remove_artwork_from_command(cmd)
cmd[-1] = tmp_dest
logger.info("► (retry) %s", " ".join(cmd))
rc, stderr = _run_ffmpeg(cmd, strict)
if rc != 0:
detail = _decode_error(stderr) or (stderr.strip().splitlines() or [""])[-1]
logger.error(
"FFmpeg failed (rc=%s) for %s → %s: %s", rc, source, final_dest, detail
)
_cleanup_temp(tmp_dest)
return rc
if finalize is not None:
try:
finalize(tmp_dest)
except Exception as e:
logger.warning("Post-encode step failed for %s: %s", final_dest, e)
try:
os.replace(tmp_dest, final_dest)
return 0
except Exception as e:
logger.error("Atomic replace failed for %s: %s", final_dest, e)
_cleanup_temp(tmp_dest)
return 1
def _source_tag_values(source: str) -> dict[str, list[str]]:
"""
Read the MP4_TAG_ATOMS source tags from *source*, whatever its tag format.
Vorbis comments (FLAC, Ogg, Opus) and APEv2 (APE, WavPack, TAK) store
them under the same names; ID3 (WAV, AIFF, MP3) uses TXXX and a few
standard frames.
"""
audio = mutagen.File(source)
if audio is None or audio.tags is None:
return {}
tags = audio.tags
# Name -> values, with keys upper-cased for case-insensitive lookup.
plain: dict[str, list[str]] = {}
is_id3 = hasattr(tags, "getall")
if is_id3:
for frame in tags.getall("TXXX"):
plain[frame.desc.upper()] = [str(v) for v in frame.text]
for name, frame_id in _ID3_FRAMES.items():
frame = tags.get(frame_id)
if frame is not None:
plain[name] = [str(v) for v in frame.text]
ufid = tags.get("UFID:http://musicbrainz.org")
if ufid is not None:
plain["MUSICBRAINZ_TRACKID"] = [ufid.data.decode("ascii", "ignore")]
for name, desc in _ID3_TXXX.items():
if desc.upper() in plain:
plain[name] = plain[desc.upper()]
else:
for key, value in tags.items():
if isinstance(value, list):
values = value
elif isinstance(value, APETextValue):
values = list(value)
else:
values = [value]
plain[str(key).upper()] = [str(v) for v in values]
out: dict[str, list[str]] = {}
for name in MP4_TAG_ATOMS:
values = [v for v in plain.get(name, []) if v.strip()]
if values:
out[name] = values
return out
def copy_mp4_tags(source: str, dest: str) -> int:
"""
Copy the MP4_TAG_ATOMS tags from *source* into the MP4 file *dest*.
Freeform atoms hold UTF-8 bytes; the sort atoms hold text.
Returns the number of tags written.
"""
values = _source_tag_values(source)
if not values:
return 0
mp4 = MP4(dest)
if mp4.tags is None:
mp4.add_tags()
for name, vals in values.items():
atom = MP4_TAG_ATOMS[name]
if atom.startswith(_FREEFORM):
mp4.tags[atom] = [MP4FreeForm(v.encode("utf-8")) for v in vals]
else:
mp4.tags[atom] = vals
mp4.save()
return len(values)
def _cleanup_temp(path: str) -> None:
"""Clean up a temporary file."""
try:
if os.path.exists(path):
os.remove(path)
except Exception:
pass