@auscera/cleoedits 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -2
- package/cli.mjs +149 -0
- package/lib/dependencies.mjs +384 -0
- package/lib/run.mjs +114 -0
- package/lib/workspace.mjs +167 -0
- package/package.json +240 -3
- package/workspace/.agents/skills/cleoedits/SKILL.md +29 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/audiogram.json +41 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/facebook.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/linkedin.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/podcast.json +23 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/reels.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/shorts.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/tiktok.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/x.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/youtube-shorts.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/ffmpeg_templates/youtube.json +48 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/assets/LICENSE-SimpleIcons +30 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/assets/logos.json +1 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/Anton-Regular.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/ArchivoBlack-Regular.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/InstrumentSerif-Italic.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/Inter.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/LICENSE-PermanentMarker.txt +202 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/Montserrat.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-Anton.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-ArchivoBlack.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-InstrumentSerif.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-Inter.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-Montserrat.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-PlayfairDisplay.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-Poppins.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/OFL-SpaceMono.txt +93 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/PermanentMarker-Regular.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/PlayfairDisplay-Italic.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/PlayfairDisplay.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/Poppins-Black.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/Poppins-Bold.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/Poppins-ExtraBold.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/fonts/SpaceMono-Bold.ttf +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/models/LICENSE-MediaPipe +218 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/models/LICENSE-YuNet +21 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/models/NOTICE.md +4 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/models/selfie_segmenter.tflite +0 -0
- package/workspace/.agents/skills/cleoedits/assets/reel_effects/models/yunet.onnx +0 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/LICENSE +21 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/audio-app-ui/STYLE.md +65 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/blueprint/STYLE.md +53 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/blueprint/index.html +328 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/blueprint/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/data-story/STYLE.md +65 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/data-story/index.html +575 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/design-app-ui/STYLE.md +68 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/editor-ui/STYLE.md +73 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/editorial-paper/STYLE.md +54 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/editorial-paper/index.html +189 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/editorial-paper/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/explainer-diagram/STYLE.md +64 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/explainer-diagram/index.html +620 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/graphite-ui/STYLE.md +55 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/graphite-ui/index.html +238 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/graphite-ui/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/growing-city/STYLE.md +66 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/growing-city/index.html +602 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/ink-line/STYLE.md +66 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/ink-line/index.html +777 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/midnight-grid/STYLE.md +56 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/midnight-grid/index.html +186 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/midnight-grid/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/motion-app-ui/STYLE.md +68 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/paper-cut/STYLE.md +56 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/paper-cut/index.html +244 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/paper-cut/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/paper-world/STYLE.md +67 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/paper-world/index.html +663 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/particle-field/STYLE.md +59 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/particle-field/index.html +395 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/poster-pop/STYLE.md +52 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/poster-pop/index.html +194 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/poster-pop/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/risograph/STYLE.md +58 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/risograph/index.html +242 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/risograph/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/swiss-grid/STYLE.md +53 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/swiss-grid/index.html +138 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/swiss-grid/studio.js +57 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/swiss-kinetic/STYLE.md +63 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/swiss-kinetic/index.html +599 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/time-travel/STYLE.md +65 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/time-travel/index.html +1297 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/weather-widget/STYLE.md +69 -0
- package/workspace/.agents/skills/cleoedits/assets/video_styles/weather-widget/index.html +674 -0
- package/workspace/.agents/skills/cleoedits/references/audio-clearervoice-elevenlabs.md +11 -0
- package/workspace/.agents/skills/cleoedits/references/ffmpeg-and-reels.md +15 -0
- package/workspace/.agents/skills/cleoedits/references/hyperframes-workflows.md +13 -0
- package/workspace/.agents/skills/cleoedits/references/kdenlive-cli-and-mcp.md +12 -0
- package/workspace/.agents/skills/cleoedits/references/motion-design-pipeline.md +7 -0
- package/workspace/.agents/skills/cleoedits/references/motion-taste-emil.md +5 -0
- package/workspace/.agents/skills/cleoedits/references/playwright-demo-capture.md +9 -0
- package/workspace/.agents/skills/cleoedits/references/remotion-workflows.md +13 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/LICENSE +21 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_ass_overlay.py +399 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/__init__.py +235 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/alpha.py +560 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/asr.py +1237 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/color.py +540 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/decision.py +2452 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/drawtext.py +290 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/emit.py +1309 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/emoji.py +518 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/fonts.py +875 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/pacing.py +263 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/pacing_edit.py +432 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/probe.py +1613 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/runner.py +3994 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/text.py +73 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/timeline.py +721 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_common/wrap.py +981 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_contract.py +2454 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_platforms.py +258 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_review.html +754 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_review.py +258 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/_smartcut.py +450 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/audio.py +867 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/background.py +136 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/batch.py +1409 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/broll.py +282 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/caption.py +2207 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/check.py +815 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/color.py +695 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/crop.py +91 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/cropdetect.py +214 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/cut.py +2076 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/deinterlace.py +90 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/denoise.py +94 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/export.py +1033 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/fit.py +729 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/freeze.py +197 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/graphics.py +920 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/grid.py +235 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/insert.py +142 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/join.py +1083 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/look.py +475 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/loop.py +395 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/loudness.py +397 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/metadata.py +416 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/multicam.py +414 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/overlay.py +766 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/pad.py +121 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/probe.py +145 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/proxy.py +93 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/redact.py +162 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/render.py +2424 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/report.py +354 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/reverse.py +114 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/scenes.py +439 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/sequence.py +285 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/silence.py +945 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/speedramp.py +193 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/sphere.py +137 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/stabilize.py +141 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/straighten.py +120 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/sync.py +500 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/verify.py +411 -0
- package/workspace/.agents/skills/cleoedits/scripts/ffmpeg/waveform.py +502 -0
- package/workspace/.agents/skills/cleoedits/scripts/kdenlive_builder.py +284 -0
- package/workspace/.agents/skills/cleoedits/scripts/motion/LICENSE-claude-motion +21 -0
- package/workspace/.agents/skills/cleoedits/scripts/motion/LICENSE-motion-design +21 -0
- package/workspace/.agents/skills/cleoedits/scripts/motion/_media.py +43 -0
- package/workspace/.agents/skills/cleoedits/scripts/motion/pacing_check.py +90 -0
- package/workspace/.agents/skills/cleoedits/scripts/motion/storyboard.py +77 -0
- package/workspace/.agents/skills/cleoedits/scripts/reel_effects/LICENSE +26 -0
- package/workspace/.agents/skills/cleoedits/scripts/reel_effects/fx.py +2717 -0
- package/workspace/.agents/skills/cleoedits/scripts/reel_effects/logos.py +194 -0
- package/workspace/.agents/skills/cleoedits/scripts/reel_effects/sfx.py +105 -0
- package/workspace/.agents/skills/cleoedits/scripts/reel_effects/trim.py +99 -0
- package/workspace/.agents/skills/cleoedits/scripts/reel_effects/words.py +60 -0
- package/workspace/.claude/commands/blender-3d.md +12 -0
- package/workspace/.claude/commands/business-short-form-reels.md +12 -0
- package/workspace/.claude/commands/color-grade.md +12 -0
- package/workspace/.claude/commands/cutout-grid-reel.md +12 -0
- package/workspace/.claude/commands/depth-ui-reel.md +12 -0
- package/workspace/.claude/commands/explainer-panel-reel.md +12 -0
- package/workspace/.claude/commands/freeze-frame.md +12 -0
- package/workspace/.claude/commands/glass-graph.md +12 -0
- package/workspace/.claude/commands/over-the-shoulder.md +12 -0
- package/workspace/.claude/commands/personal-short-form-reels.md +12 -0
- package/workspace/.claude/commands/ranking-format.md +12 -0
- package/workspace/.claude/commands/relight.md +12 -0
- package/workspace/.claude/commands/reverse-engineer.md +12 -0
- package/workspace/.claude/commands/text-behind.md +12 -0
- package/workspace/.claude/commands/tier-list.md +12 -0
- package/workspace/.claude-plugin/plugin.json +8 -0
- package/workspace/.npmignore +15 -0
- package/workspace/AGENTS.md +30 -0
- package/workspace/README.md +63 -0
- package/workspace/assets/fonts/TikTokSans-OFL.txt +93 -0
- package/workspace/assets/fonts/TikTokSans-Variable.ttf +0 -0
- package/workspace/assets/luts/clean_warm.cube +4917 -0
- package/workspace/assets/luts/golden_hour.cube +4917 -0
- package/workspace/assets/luts/moody_dark.cube +4917 -0
- package/workspace/assets/luts/teal_orange.cube +4917 -0
- package/workspace/assets/sfx/NOTICE.txt +1 -0
- package/workspace/assets/sfx/click.wav +0 -0
- package/workspace/assets/sfx/pop.wav +0 -0
- package/workspace/assets/sfx/riser.wav +0 -0
- package/workspace/assets/sfx/whoosh.wav +0 -0
- package/workspace/generators/hyperframes/index.html +39 -0
- package/workspace/generators/hyperframes/package.json +15 -0
- package/workspace/generators/hyperframes/pnpm-lock.yaml +1181 -0
- package/workspace/generators/hyperframes/pnpm-workspace.yaml +10 -0
- package/workspace/generators/remotion/eslint.config.mjs +3 -0
- package/workspace/generators/remotion/package.json +36 -0
- package/workspace/generators/remotion/pnpm-lock.yaml +4179 -0
- package/workspace/generators/remotion/pnpm-workspace.yaml +10 -0
- package/workspace/generators/remotion/remotion.config.ts +13 -0
- package/workspace/generators/remotion/src/Root.tsx +52 -0
- package/workspace/generators/remotion/src/index.css +1 -0
- package/workspace/generators/remotion/src/index.ts +4 -0
- package/workspace/generators/remotion/tsconfig.json +15 -0
- package/workspace/pipeline/scripts/_codec.py +28 -0
- package/workspace/pipeline/scripts/ai_media_api.py +109 -0
- package/workspace/pipeline/scripts/blender_render.py +166 -0
- package/workspace/pipeline/scripts/composite.py +49 -0
- package/workspace/pipeline/scripts/melt.sh +68 -0
- package/workspace/pipeline/scripts/normalize_audio.py +39 -0
- package/workspace/pipeline/scripts/person_matte.py +216 -0
- package/workspace/pipeline/scripts/record_demo.py +69 -0
- package/workspace/pipeline/scripts/whisper_cut.py +98 -0
- package/workspace-manifest.json +908 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Two-pass EBU R128 loudness normalisation (ffmpeg loudnorm).
|
|
3
|
+
|
|
4
|
+
Pass 1 measures integrated loudness, true peak, LRA and threshold; pass 2
|
|
5
|
+
applies loudnorm in linear mode with those measurements so the result hits
|
|
6
|
+
the target without the pumping of single-pass mode. Video is stream-copied.
|
|
7
|
+
|
|
8
|
+
Common targets: -14 LUFS (YouTube/Spotify), -16 (Apple Podcasts), -23 (EBU broadcast).
|
|
9
|
+
|
|
10
|
+
Examples:
|
|
11
|
+
python3 loudness.py input.mp4 # -14 LUFS, -1 dBTP
|
|
12
|
+
python3 loudness.py podcast.wav -I -16 --tp -1.5 -o podcast_norm.wav
|
|
13
|
+
python3 loudness.py input.mp4 --measure-only
|
|
14
|
+
python3 loudness.py music.wav --lra 7 # tighter loudness range target
|
|
15
|
+
"""
|
|
16
|
+
import argparse
|
|
17
|
+
import json
|
|
18
|
+
import os
|
|
19
|
+
import re
|
|
20
|
+
import sys
|
|
21
|
+
|
|
22
|
+
from _platforms import TP_TOLERANCE_DB
|
|
23
|
+
|
|
24
|
+
AUDIO_LENGTH_TOLERANCE = 0.03 # seconds: one AAC frame at 48 kHz is 0.021 s
|
|
25
|
+
LUFS_TOLERANCE = 1.0 # LU: a normalised file within this of the requested -I is on target
|
|
26
|
+
from _common.probe import PROBED_AUDIO, probed_maps, shorter_than_a_loudness_block
|
|
27
|
+
from _common.emit import own_length, refuse
|
|
28
|
+
from _common.runner import ffmpeg_failure_extra
|
|
29
|
+
from _common.decision import LOSSLESS_AUDIO_EXT, PAD_SOUND_FROM_ZERO, late_sound_lead
|
|
30
|
+
from _common import STATE, add_common, apply_common, emit, AUDIO_CODECS, refuse_unwritten_output, audio_codec_for, default_output, die, ffmpeg_base, info, probe, require_tool, run, run_analysis, run_keeping_subtitles, dry_run_input_pending
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# --versions' named targets: integrated loudness (LUFS) and true-peak ceiling (dBTP)
|
|
35
|
+
VERSION_TARGETS = {
|
|
36
|
+
"streaming": (-14.0, -1.0), # YouTube, Spotify and most streaming services
|
|
37
|
+
"ebu-r128": (-23.0, -1.0), # EBU R128 broadcast
|
|
38
|
+
"broadcast-jp": (-24.0, -1.0), # ARIB TR-B32 (Japanese broadcast, LKFS = LUFS)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def parse_versions(text: str) -> "list":
|
|
43
|
+
"""[(label, lufs, tp)] from --versions; a refusal names the item that did not parse."""
|
|
44
|
+
out, seen = [], set()
|
|
45
|
+
for item in (x.strip() for x in text.split(",")):
|
|
46
|
+
if not item:
|
|
47
|
+
continue
|
|
48
|
+
if item in VERSION_TARGETS:
|
|
49
|
+
label, (lufs, tp) = item, VERSION_TARGETS[item]
|
|
50
|
+
else:
|
|
51
|
+
try:
|
|
52
|
+
lufs_s, tp_s = item.split("/")
|
|
53
|
+
lufs, tp = float(lufs_s), float(tp_s)
|
|
54
|
+
except ValueError:
|
|
55
|
+
die(f"--versions item {item!r} is neither a named target ({', '.join(VERSION_TARGETS)}) nor LUFS/TP such as -16/-1.5")
|
|
56
|
+
if not (-70 <= lufs <= -5 and -9 <= tp <= 0):
|
|
57
|
+
die(f"--versions item {item!r}: LUFS must be -70..-5 and TP -9..0 (loudnorm's ranges)")
|
|
58
|
+
label = f"{lufs:g}lufs{tp:g}tp".replace("-", "m").replace(".", "_")
|
|
59
|
+
if label in seen:
|
|
60
|
+
die(f"--versions names {label!r} twice")
|
|
61
|
+
seen.add(label)
|
|
62
|
+
out.append((label, lufs, tp))
|
|
63
|
+
if not out:
|
|
64
|
+
die("--versions is empty")
|
|
65
|
+
return out
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def measure(path: str, I: float, tp: float, lra: float) -> dict:
|
|
69
|
+
if dry_run_input_pending(path):
|
|
70
|
+
return {"input_i": "-20.0", "input_tp": "-3.0", "input_lra": "8.0", "input_thresh": "-30.0", "target_offset": "0.0", "silent": False, "placeholder": True}
|
|
71
|
+
ffmpeg = require_tool("ffmpeg")
|
|
72
|
+
# the first audio track, the one pass 2 normalises and probe() describes: without a -map,
|
|
73
|
+
# FFmpeg measured the track with the default disposition or the most channels, and the gain
|
|
74
|
+
# for that track was applied to a:0 (A110)
|
|
75
|
+
cmd = [ffmpeg, "-hide_banner", "-nostdin", "-i", path, "-map", f"0:{PROBED_AUDIO}", "-vn",
|
|
76
|
+
"-af", f"aresample=async=1:min_hard_comp=0.01,loudnorm=I={I}:TP={tp}:LRA={lra}:print_format=json", "-f", "null", "-"]
|
|
77
|
+
# Pass 1 is a measurement: it runs under --dry-run too, so the planned pass-2 command and
|
|
78
|
+
# the reported input_i are real (before 1.4.6 a dry run returned a made-up -20 LUFS).
|
|
79
|
+
proc = run_analysis(cmd, check=False, record=True)
|
|
80
|
+
m = re.search(r"\{[^{}]*\"input_i\"[^{}]*\}", proc.stderr, re.S)
|
|
81
|
+
if proc.returncode != 0 or not m:
|
|
82
|
+
die(f"loudness measurement failed:\n{proc.stderr.strip()[-1500:]}", kind="ffmpeg", **ffmpeg_failure_extra(proc.stderr))
|
|
83
|
+
data = json.loads(m.group(0))
|
|
84
|
+
data["too_short"] = False
|
|
85
|
+
for k in ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset"):
|
|
86
|
+
if data.get(k) in (None, "-inf", "inf", "nan"):
|
|
87
|
+
# A257: a clip shorter than one 400 ms gating block has no integrated loudness but a
|
|
88
|
+
# true peak; that is not silence, and telling the caller to replace the audio was wrong.
|
|
89
|
+
# The length decides it, not the peak: a long track under the absolute gate has a
|
|
90
|
+
# finite peak and no integrated loudness as well, and stays "silent" as before.
|
|
91
|
+
data["too_short"] = (data.get("input_tp") not in (None, "-inf", "inf", "nan")
|
|
92
|
+
and shorter_than_a_loudness_block(audio_length(probe(path))))
|
|
93
|
+
data["silent"] = not data["too_short"]
|
|
94
|
+
return data
|
|
95
|
+
data["silent"] = False
|
|
96
|
+
return data
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def loudnorm_mode(stats: dict, lufs: float, tp: float, lra: float) -> str:
|
|
100
|
+
"""The mode loudnorm runs pass 2 in, by its own rule (af_loudnorm.c init): linear only when the
|
|
101
|
+
measured values are all usable and the linear gain keeps the true peak under the ceiling and
|
|
102
|
+
the range within --lra; otherwise it falls back to dynamic, which compresses the range and can
|
|
103
|
+
land away from the target on its own (A111). The values are the strings pass 1 printed, which
|
|
104
|
+
are the ones pass 2 is given, so the comparison is ffmpeg's own. Checked against loudnorm's
|
|
105
|
+
print_format=json normalization_type by test_loudnorm_mode_matches_what_ffmpeg_reports."""
|
|
106
|
+
try:
|
|
107
|
+
mi, mtp, mlra, mth = (float(stats[k]) for k in ("input_i", "input_tp", "input_lra", "input_thresh"))
|
|
108
|
+
except (KeyError, TypeError, ValueError):
|
|
109
|
+
return "dynamic"
|
|
110
|
+
if mtp == 99 or mth == -70 or mlra == 0 or mi == 0:
|
|
111
|
+
return "dynamic"
|
|
112
|
+
return "linear" if mtp + (lufs - mi) <= tp and mlra <= lra else "dynamic"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _normalize(args, meta: dict, stats: dict, lufs: float, tp_max: float, output: str) -> dict:
|
|
116
|
+
"""One normalised file at `lufs` / `tp_max`, measured after: the keys emit() takes for it."""
|
|
117
|
+
sr = args.sample_rate or meta["audio"].get("sample_rate") or 48000
|
|
118
|
+
ext = os.path.splitext(output)[1].lower()
|
|
119
|
+
bitrate_pinned = args.audio_bitrate is not None
|
|
120
|
+
# an AAC audio extension starts at its own rate (256k for .m4a/.aac), which is what it was
|
|
121
|
+
# written at: a pinned --audio-bitrate and the raised retries below never reached it, so the
|
|
122
|
+
# retries re-encoded at 256k and audio_bitrate_used said 192k, then 256k and 320k (A143)
|
|
123
|
+
codec = AUDIO_CODECS.get(ext, [])
|
|
124
|
+
own_rate = codec[codec.index("-b:a") + 1] if "aac" in codec and "-b:a" in codec else None
|
|
125
|
+
bitrate = args.audio_bitrate or own_rate or "192k"
|
|
126
|
+
|
|
127
|
+
dropped_streams = False
|
|
128
|
+
# loudnorm emits its last block whole, and through the AAC encoder that came out as 0.1 s more
|
|
129
|
+
# sound than the input had (a 90.000 s Reels export left at 90.100 s, which check.py fails):
|
|
130
|
+
# the sound is trimmed back to the input's own length. `duration=` counts from the first sample,
|
|
131
|
+
# so sound that starts late keeps its end (`end=` is a timestamp, and cut it). atrim follows
|
|
132
|
+
# loudnorm directly: an aresample between them fails channel-layout negotiation on FFmpeg 5.1
|
|
133
|
+
in_audio = audio_length(meta)
|
|
134
|
+
# A sound-only output of a source whose sound starts after the file's origin (late against its
|
|
135
|
+
# picture) carries that lead as silence: .wav and .flac have no start time and lost it, .m4a
|
|
136
|
+
# kept it as an edit a decoder does not pad, so the file played that much early (#539). Beside
|
|
137
|
+
# a copied picture the container holds the start, and nothing is padded.
|
|
138
|
+
sound_only = ext in AUDIO_CODECS or not meta.get("video")
|
|
139
|
+
lead = late_sound_lead(meta) if sound_only else 0.0
|
|
140
|
+
|
|
141
|
+
def encode(tp: float, bitrate: str) -> None:
|
|
142
|
+
nonlocal dropped_streams
|
|
143
|
+
af = (
|
|
144
|
+
f"aresample=async=1:min_hard_comp=0.01,loudnorm=I={lufs}:TP={tp}:LRA={args.lra}"
|
|
145
|
+
f":measured_I={stats['input_i']}:measured_TP={stats['input_tp']}:measured_LRA={stats['input_lra']}"
|
|
146
|
+
f":measured_thresh={stats['input_thresh']}:offset={stats['target_offset']}:linear=true:print_format=summary"
|
|
147
|
+
)
|
|
148
|
+
if loudnorm_mode(stats, lufs, tp, args.lra) == "dynamic":
|
|
149
|
+
# In dynamic mode loudnorm stamps the frames it buffers itself, and they jumped about
|
|
150
|
+
# 0.1 s after a stretch of digital silence (FFmpeg 6.1) while the samples ran on: every
|
|
151
|
+
# sound after the jump played 89 ms late against a copied picture, and atrim, which
|
|
152
|
+
# counts time, cut as much off the end. The samples are restamped in order from the
|
|
153
|
+
# first one's time (a late start keeps its lead). A gap in the source's sound is
|
|
154
|
+
# filled with silence before loudnorm (aresample=async=1, both modes; from 10 ms
|
|
155
|
+
# up, swresample's own threshold of 100 ms left an 80 ms gap closed): the restamp
|
|
156
|
+
# closed it, and everything after the gap played that much early (review of #636).
|
|
157
|
+
af += ",asetpts=N/SR/TB+STARTPTS"
|
|
158
|
+
if in_audio:
|
|
159
|
+
af += f",atrim=duration={float(in_audio):.6f}"
|
|
160
|
+
if lead:
|
|
161
|
+
af += "," + PAD_SOUND_FROM_ZERO # after the trim, which counts from the first sample
|
|
162
|
+
cmd = ffmpeg_base() + ["-i", args.input, "-af", af, "-ar", str(sr)]
|
|
163
|
+
if ext in AUDIO_CODECS or not meta.get("video"):
|
|
164
|
+
# the rate reaches an audio extension's codec when it was pinned or is AAC's (an
|
|
165
|
+
# .mp3/.ogg/.opus left alone keeps its own setting, as it always has)
|
|
166
|
+
cmd += ["-map", f"0:{PROBED_AUDIO}", "-vn"] + audio_codec_for(output, bitrate if ext not in AUDIO_CODECS or bitrate_pinned or own_rate else None)
|
|
167
|
+
else:
|
|
168
|
+
cmd += probed_maps(meta) + ["-c:v", "copy", "-c:a", "aac", "-b:a", bitrate]
|
|
169
|
+
if ext in (".mp4", ".mov", ".m4v"):
|
|
170
|
+
cmd += ["-movflags", "+faststart"]
|
|
171
|
+
dropped_streams = run_keeping_subtitles(cmd, output)
|
|
172
|
+
return
|
|
173
|
+
cmd.append(output)
|
|
174
|
+
run(cmd)
|
|
175
|
+
|
|
176
|
+
encode(tp_max, bitrate)
|
|
177
|
+
if STATE.dry_run:
|
|
178
|
+
# pass 1 measured the input for real; there is no output to measure
|
|
179
|
+
return {"output": output, "measured": {k: stats[k] for k in ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset", "silent")},
|
|
180
|
+
"loudnorm_mode": loudnorm_mode(stats, lufs, tp_max, args.lra)}
|
|
181
|
+
after = measure(output, lufs, tp_max, args.lra)
|
|
182
|
+
# loudnorm holds the ceiling on the float samples it outputs; the lossy encoder then adds
|
|
183
|
+
# its own overshoot. ffmpeg's native AAC at 192k turned one transient of a 12-minute film
|
|
184
|
+
# from -2.4 dBFS into +3.7 dBFS, so the file measured +1.2 dBTP after "--tp -1" and
|
|
185
|
+
# check.py's fix hint pointed straight back here. Two remedies, in this order: more bits
|
|
186
|
+
# (256k, then 320k: the overshoot is quantisation noise and shrinks with bitrate, and the
|
|
187
|
+
# loudness target is untouched) when the caller did not pin the bitrate; then a lower
|
|
188
|
+
# loudnorm ceiling by the measured overshoot, which in linear mode also lowers the
|
|
189
|
+
# integrated loudness -- reported, never hidden.
|
|
190
|
+
ceiling, rounds = tp_max, 0
|
|
191
|
+
steps = [] if bitrate_pinned or ext in AUDIO_CODECS and "aac" not in AUDIO_CODECS[ext] else [b for b in ("256k", "320k") if _kbps(b) > _kbps(bitrate)]
|
|
192
|
+
while not after.get("silent") and float(after["input_tp"]) > tp_max + TP_TOLERANCE_DB and rounds < 5:
|
|
193
|
+
rounds += 1
|
|
194
|
+
overshoot = float(after["input_tp"]) - tp_max
|
|
195
|
+
if steps:
|
|
196
|
+
bitrate = steps.pop(0)
|
|
197
|
+
info(f"true peak {float(after['input_tp']):.2f} dBTP exceeds the requested {tp_max:g} dBTP after encoding "
|
|
198
|
+
f"(codec overshoot); re-encoding at {bitrate}")
|
|
199
|
+
else:
|
|
200
|
+
ceiling -= overshoot + 0.2
|
|
201
|
+
info(f"true peak {float(after['input_tp']):.2f} dBTP exceeds the requested {tp_max:g} dBTP after encoding "
|
|
202
|
+
f"(codec overshoot); re-encoding with the loudnorm ceiling at {ceiling:.2f} dBTP")
|
|
203
|
+
encode(ceiling, bitrate)
|
|
204
|
+
after = measure(output, lufs, tp_max, args.lra)
|
|
205
|
+
if not after.get("silent"):
|
|
206
|
+
info(f"result: {float(after['input_i']):.1f} LUFS, TP {float(after['input_tp']):.1f} dBTP (target {lufs} LUFS, TP <= {tp_max:g})")
|
|
207
|
+
result = {k: after[k] for k in ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset", "silent")}
|
|
208
|
+
result["tp_ceiling_used"] = round(ceiling, 2)
|
|
209
|
+
if ext not in AUDIO_CODECS or "aac" in AUDIO_CODECS[ext] or bitrate_pinned:
|
|
210
|
+
# a pinned rate reaches an .mp3/.ogg/.opus codec too (A143); unpinned, those keep their own
|
|
211
|
+
result["audio_bitrate_used"] = bitrate
|
|
212
|
+
result["encodes"] = rounds + 1
|
|
213
|
+
# the mode of the last encode: its ceiling is the one the file was made with
|
|
214
|
+
result["loudnorm_mode"] = loudnorm_mode(stats, lufs, ceiling, args.lra)
|
|
215
|
+
if not after.get("silent") and not after.get("too_short") and abs(float(after["input_i"]) - lufs) > LUFS_TOLERANCE:
|
|
216
|
+
if result["loudnorm_mode"] == "dynamic":
|
|
217
|
+
# A111: the miss is loudnorm's dynamic mode, not an encoder overshoot (it happens on a
|
|
218
|
+
# lossless output too): a linear gain to the target would have put the true peak over
|
|
219
|
+
# the ceiling, or the range is wider than --lra
|
|
220
|
+
result["note"] = (f"integrated loudness is {float(after['input_i']) - lufs:+.1f} LU from the target because "
|
|
221
|
+
f"loudnorm could not apply one linear gain (the input's true peak {float(stats['input_tp']):.1f} dBTP "
|
|
222
|
+
f"plus the gain would pass the {ceiling:.2f} dBTP ceiling, or its range "
|
|
223
|
+
f"{float(stats['input_lra']):.1f} LU is outside 0 < LRA <= --lra {args.lra:g}) and fell back to "
|
|
224
|
+
f"dynamic mode, which compresses the range; a lower -I, a higher --tp or a wider --lra keeps it linear")
|
|
225
|
+
else:
|
|
226
|
+
result["note"] = (f"integrated loudness is {float(after['input_i']) - lufs:+.1f} LU from the target because the "
|
|
227
|
+
f"true-peak ceiling had to absorb the encoder's overshoot; a lossless delivery (wav/flac) or a pinned "
|
|
228
|
+
f"higher --audio-bitrate keeps both")
|
|
229
|
+
if not after.get("silent") and float(after["input_tp"]) > tp_max + TP_TOLERANCE_DB:
|
|
230
|
+
die(f"true peak is still {float(after['input_tp']):.2f} dBTP after {rounds + 1} encodes (requested <= {tp_max:g}); "
|
|
231
|
+
f"the encoder overshoots more than the loudnorm ceiling can absorb at this bitrate",
|
|
232
|
+
kind="verification", output=output, result=result,
|
|
233
|
+
hint="raise --audio-bitrate (e.g. 256k) or deliver a lossless format (wav/flac) and let the platform encode")
|
|
234
|
+
if after.get("silent") or after.get("too_short"):
|
|
235
|
+
# an output with no integrated loudness was not measured against the target: through
|
|
236
|
+
# 3.6 a silent one passed this step (ok: true) with -inf in its numbers
|
|
237
|
+
verification = [{"step": "loudness", "ok": False, "target_lufs": lufs, "target_tp": tp_max,
|
|
238
|
+
"property": "loudness", "expected": {"lufs": lufs, "tp_max": tp_max},
|
|
239
|
+
"reason": "not_measured: the output has no integrated loudness ("
|
|
240
|
+
+ ("silent" if after.get("silent") else "shorter than one 400 ms block") + ")",
|
|
241
|
+
"provenance": "user_specified"}]
|
|
242
|
+
else:
|
|
243
|
+
verification = [{"step": "loudness",
|
|
244
|
+
"ok": abs(float(after["input_i"]) - lufs) <= LUFS_TOLERANCE and float(after["input_tp"]) <= tp_max + TP_TOLERANCE_DB,
|
|
245
|
+
"lufs": float(after["input_i"]), "tp": float(after["input_tp"]), "target_lufs": lufs, "target_tp": tp_max,
|
|
246
|
+
"property": "loudness", "expected": {"lufs": lufs, "tp_max": tp_max},
|
|
247
|
+
"measured": {"lufs": float(after["input_i"]), "tp": float(after["input_tp"])},
|
|
248
|
+
"delta": {"lufs": round(float(after["input_i"]) - lufs, 2), "tp": round(float(after["input_tp"]) - tp_max, 2)},
|
|
249
|
+
"tolerance": {"lufs": LUFS_TOLERANCE, "tp": TP_TOLERANCE_DB}, "provenance": "user_specified"}]
|
|
250
|
+
written = probe(output, role="output")
|
|
251
|
+
out_audio = audio_length(written)
|
|
252
|
+
if in_audio and out_audio:
|
|
253
|
+
# a levels pass changes no timing: the sound is as long as the input's, to one AAC frame
|
|
254
|
+
tol = max(1024.0 / float(sr), AUDIO_LENGTH_TOLERANCE)
|
|
255
|
+
want = float(in_audio) + lead # the padded lead is part of a sound-only output's length
|
|
256
|
+
verification.append({"step": "duration", "property": "audio_duration", "ok": abs(out_audio - want) <= tol,
|
|
257
|
+
"expected": round(want, 3), "measured": round(float(out_audio), 3),
|
|
258
|
+
"delta": round(float(out_audio) - want, 3), "tolerance": round(tol, 3),
|
|
259
|
+
"provenance": "input_premeasurement"})
|
|
260
|
+
else:
|
|
261
|
+
# a length that could not be read is not a length that matched
|
|
262
|
+
verification.append({"step": "duration", "property": "audio_duration", "ok": False,
|
|
263
|
+
"reason": "not_measured: the " + ("input" if not in_audio else "output") + " reports no duration"})
|
|
264
|
+
return {"output": output, "result": result, "dropped_non_av_streams": dropped_streams,
|
|
265
|
+
"measured": {k: stats[k] for k in ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset", "silent")},
|
|
266
|
+
"targets": {"lufs": lufs, "tp": tp_max, "lra": args.lra}, "verification": verification}
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _versions(args, meta: dict, plan: list) -> int:
|
|
270
|
+
"""--versions: one file per target from the one master, each a whole two-pass normalisation
|
|
271
|
+
(pass 1 is per target: loudnorm's target_offset depends on it) measured after. `verified` is
|
|
272
|
+
true only when every version met its own target."""
|
|
273
|
+
versions, steps, dropped = [], [], False
|
|
274
|
+
for label, lufs, tp in plan:
|
|
275
|
+
out = default_output(args.input, f"loudnorm_{label}")
|
|
276
|
+
stats = measure(args.input, lufs, tp, args.lra)
|
|
277
|
+
if stats.get("too_short"):
|
|
278
|
+
refuse("input", "audio_too_short",
|
|
279
|
+
f"input audio peaks at {stats.get('input_tp')} dBTP but is shorter than one 400 ms loudness block, so it has "
|
|
280
|
+
"no integrated loudness to normalise from (this is not silence)", input_tp=stats.get("input_tp"))
|
|
281
|
+
if stats.get("silent"):
|
|
282
|
+
die("input audio is silent; loudness normalisation is meaningless (use audio.py --replace to add a track)")
|
|
283
|
+
doc = _normalize(args, meta, stats, lufs, tp, out)
|
|
284
|
+
dropped = dropped or bool(doc.get("dropped_non_av_streams"))
|
|
285
|
+
versions.append({"target": label, "output": out, "targets": {"lufs": lufs, "tp": tp, "lra": args.lra},
|
|
286
|
+
**({"result": doc["result"]} if "result" in doc else {})})
|
|
287
|
+
steps += [dict(st, version=label) for st in doc.get("verification") or []]
|
|
288
|
+
outputs = [v["output"] for v in versions]
|
|
289
|
+
emit(None, versions=versions, outputs=outputs, verification_paths=outputs, dropped_non_av_streams=dropped,
|
|
290
|
+
**({"verification": steps} if steps else {}))
|
|
291
|
+
if not STATE.json and not STATE.plan:
|
|
292
|
+
print("\n".join(v["output"] for v in versions)) # one path per line, as a single run prints its one
|
|
293
|
+
return 0
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def main() -> int:
|
|
297
|
+
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
298
|
+
ap.add_argument("input")
|
|
299
|
+
ap.add_argument("-o", "--output", help="output file (default: <name>_loudnorm.<ext>)")
|
|
300
|
+
ap.add_argument("-I", "--lufs", "--target", type=float, default=-14.0, help="integrated loudness target in LUFS (default -14)")
|
|
301
|
+
ap.add_argument("--tp", "--true-peak", type=float, default=-1.0, help="true peak ceiling in dBTP (default -1)")
|
|
302
|
+
ap.add_argument("--lra", type=float, default=11.0, help="loudness range target in LU (default 11)")
|
|
303
|
+
ap.add_argument("--measure-only", action="store_true", help="print the measured stats as JSON and exit")
|
|
304
|
+
ap.add_argument("--audio-bitrate", default=None, help="audio bitrate: AAC's in a video container (default 192k), and the codec's of a lossy audio output (.m4a/.aac default 256k, .mp3/.ogg/.opus their own setting); "
|
|
305
|
+
"refused for .wav/.flac/.aif/.aiff/.caf/.mka. Raised to 256k/320k for AAC only when the encoder overshoots the true-peak ceiling and you did not pin it")
|
|
306
|
+
ap.add_argument("--versions", metavar="LIST",
|
|
307
|
+
help="write one normalised file per target from this master, each measured after: a comma list of "
|
|
308
|
+
+ ", ".join(f"{n} ({i:g} LUFS / {t:g} dBTP)" for n, (i, t) in VERSION_TARGETS.items())
|
|
309
|
+
+ ", or LUFS/TP pairs such as -16/-1.5; files are <name>_loudnorm_<target>.<ext> (not with -o)")
|
|
310
|
+
ap.add_argument("--sample-rate", type=int, help="output sample rate (default: 48000; loudnorm upsamples internally to 192k)")
|
|
311
|
+
add_common(ap)
|
|
312
|
+
args = ap.parse_args()
|
|
313
|
+
apply_common(args)
|
|
314
|
+
# parsed before anything is measured, and an explicitly empty --versions ('' or ',') is refused
|
|
315
|
+
# by parse_versions rather than read as "not given", which wrote one default file instead
|
|
316
|
+
plan = parse_versions(args.versions) if args.versions is not None else None
|
|
317
|
+
# A124/A185: loudnorm's own ranges, checked here as --versions checks its items. Outside them
|
|
318
|
+
# the measurement pass failed inside ffmpeg (kind ffmpeg, with a page of stderr)
|
|
319
|
+
for flag, value, lo, hi, unit in (("-I/--lufs", args.lufs, -70.0, -5.0, "LUFS"), ("--tp", args.tp, -9.0, 0.0, "dBTP"),
|
|
320
|
+
("--lra", args.lra, 1.0, 50.0, "LU")):
|
|
321
|
+
if not lo <= value <= hi:
|
|
322
|
+
die(f"{flag} must be {lo:g}..{hi:g} {unit} (loudnorm's range), got {value:g}")
|
|
323
|
+
if args.sample_rate is not None and args.sample_rate <= 0:
|
|
324
|
+
die(f"--sample-rate must be a positive number of Hz, got {args.sample_rate}")
|
|
325
|
+
if plan is not None and args.output:
|
|
326
|
+
die("--versions writes one file per target, named <name>_loudnorm_<target>.<ext>; it does not take -o")
|
|
327
|
+
|
|
328
|
+
meta = probe(args.input)
|
|
329
|
+
if not meta.get("audio"):
|
|
330
|
+
die("input has no audio stream")
|
|
331
|
+
out_ext = os.path.splitext(args.output or default_output(args.input, "loudnorm"))[1].lower()
|
|
332
|
+
if not args.measure_only:
|
|
333
|
+
# loudnorm encodes the sound: a .wma name (an -o, or a .wma input's default) is refused
|
|
334
|
+
# before the measuring pass, not after it (#517)
|
|
335
|
+
refuse_unwritten_output(args.output or default_output(args.input, "loudnorm"))
|
|
336
|
+
if args.audio_bitrate is not None and not args.measure_only and out_ext in LOSSLESS_AUDIO_EXT:
|
|
337
|
+
# A143: the rate was dropped without a word; a lossless codec has none to set
|
|
338
|
+
die(f"--audio-bitrate has no effect on a lossless {out_ext} output; drop it, or write a lossy one (.m4a, .mp3, ...)")
|
|
339
|
+
if plan is not None and not args.measure_only:
|
|
340
|
+
# each version measures the master against its own target (loudnorm's offset depends on
|
|
341
|
+
# it): a pass against -I/--tp first would be one more full decode whose result is unused
|
|
342
|
+
return _versions(args, meta, plan)
|
|
343
|
+
|
|
344
|
+
stats = measure(args.input, args.lufs, args.tp, args.lra)
|
|
345
|
+
if stats.get("too_short"):
|
|
346
|
+
info(f"audio is shorter than one 400 ms loudness block: no integrated loudness (true peak {stats.get('input_tp')} dBTP)")
|
|
347
|
+
if args.measure_only:
|
|
348
|
+
measured = {"silent": False, "too_short": True, "input_i": None, "input_tp": stats.get("input_tp")}
|
|
349
|
+
if STATE.json:
|
|
350
|
+
emit(None, measured=measured)
|
|
351
|
+
else:
|
|
352
|
+
print(json.dumps(measured, indent=2))
|
|
353
|
+
return 0
|
|
354
|
+
refuse("input", "audio_too_short",
|
|
355
|
+
f"input audio peaks at {stats.get('input_tp')} dBTP but is shorter than one 400 ms loudness block, so it has "
|
|
356
|
+
"no integrated loudness to normalise from (this is not silence)", input_tp=stats.get("input_tp"))
|
|
357
|
+
if stats.get("silent"):
|
|
358
|
+
info("audio is silent (integrated loudness -inf); nothing to normalise")
|
|
359
|
+
if args.measure_only:
|
|
360
|
+
if STATE.json:
|
|
361
|
+
emit(None, measured={"silent": True, "input_i": "-inf"})
|
|
362
|
+
else:
|
|
363
|
+
print(json.dumps({"silent": True, "input_i": "-inf"}, indent=2))
|
|
364
|
+
return 0
|
|
365
|
+
die("input audio is silent; loudness normalisation is meaningless (use audio.py --replace to add a track)")
|
|
366
|
+
info(f"measured: {float(stats['input_i']):.1f} LUFS, TP {float(stats['input_tp']):.1f} dBTP, LRA {float(stats['input_lra']):.1f} LU")
|
|
367
|
+
if args.measure_only:
|
|
368
|
+
measured = {k: stats[k] for k in ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset")}
|
|
369
|
+
if STATE.json:
|
|
370
|
+
emit(None, measured=measured) # the contract's document shape (status, commands), not a bare dict
|
|
371
|
+
else:
|
|
372
|
+
print(json.dumps(measured, indent=2))
|
|
373
|
+
return 0
|
|
374
|
+
|
|
375
|
+
output = args.output or default_output(args.input, "loudnorm")
|
|
376
|
+
doc = _normalize(args, meta, stats, args.lufs, args.tp, output)
|
|
377
|
+
emit(doc.pop("output"), **doc)
|
|
378
|
+
return 0
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def audio_length(meta: dict) -> "Optional[float]":
|
|
382
|
+
"""The sound's own length: the stream's duration, else Matroska's DURATION tag. Never the
|
|
383
|
+
container's, which is the picture's when the picture runs longer (and would fail a correct
|
|
384
|
+
output against an expected length the sound never had)."""
|
|
385
|
+
return own_length(meta.get("audio"))
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def _kbps(value: str) -> int:
|
|
389
|
+
v = value.lower().rstrip("k")
|
|
390
|
+
try:
|
|
391
|
+
return int(float(v)) if value.lower().endswith("k") else int(float(v)) // 1000
|
|
392
|
+
except ValueError:
|
|
393
|
+
return 0
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
if __name__ == "__main__":
|
|
397
|
+
sys.exit(main())
|