feat: more optional options
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
@@ -33,7 +34,13 @@ def resolve_path(value: str) -> Path:
|
||||
|
||||
def media_duration(path: Path) -> float:
|
||||
result = subprocess.run(
|
||||
["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", str(path)],
|
||||
[
|
||||
"ffprobe",
|
||||
"-v", "error",
|
||||
"-show_entries", "format=duration",
|
||||
"-of", "default=noprint_wrappers=1:nokey=1",
|
||||
str(path),
|
||||
],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
@@ -46,6 +53,15 @@ def run(command: list[str]) -> None:
|
||||
subprocess.run(command, check=True)
|
||||
|
||||
|
||||
def remove_temp_dir(path: Path) -> None:
|
||||
try:
|
||||
shutil.rmtree(path, ignore_errors=True)
|
||||
except PermissionError:
|
||||
pass
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Create a timed voiceover WAV/MP3 from an SRT file on macOS.")
|
||||
parser.add_argument("--srt", required=True, help="Input SRT file")
|
||||
@@ -53,54 +69,80 @@ def main() -> None:
|
||||
parser.add_argument("--rate", type=int, default=185, help="Speech rate. Higher is faster. Example: 180-200")
|
||||
parser.add_argument("--wav", required=True, help="Output WAV file")
|
||||
parser.add_argument("--mp3", default=None, help="Optional output MP3 file")
|
||||
parser.add_argument("--audio-output", default=None, help="Optional extra copy of the generated WAV file")
|
||||
parser.add_argument("--keep-temp", action="store_true", help="Keep temporary voice parts for debugging")
|
||||
parser.add_argument("--volume", type=float, default=1.0, help="Voiceover volume multiplier. Default: 1.0")
|
||||
parser.add_argument("--silence-padding", type=float, default=0.0, help="Seconds of silence after each caption, if the SRT slot allows it. Default: 0.0")
|
||||
parser.add_argument("--sample-rate", type=int, default=44100, help="Output sample rate. Default: 44100")
|
||||
parser.add_argument("--channels", type=int, default=2, choices=[1, 2], help="Output audio channels. Default: 2")
|
||||
args = parser.parse_args()
|
||||
|
||||
srt_path = resolve_path(args.srt)
|
||||
wav_output = resolve_path(args.wav)
|
||||
mp3_output = resolve_path(args.mp3) if args.mp3 else None
|
||||
audio_output = resolve_path(args.audio_output) if args.audio_output else None
|
||||
work_dir = Path(__file__).resolve().parent / "voice_parts"
|
||||
|
||||
if not srt_path.exists():
|
||||
raise FileNotFoundError(f"SRT not found: {srt_path}")
|
||||
if args.volume <= 0:
|
||||
raise ValueError("--volume must be greater than 0")
|
||||
if args.silence_padding < 0:
|
||||
raise ValueError("--silence-padding must be 0 or greater")
|
||||
|
||||
work_dir.mkdir(parents=True, exist_ok=True)
|
||||
wav_output.parent.mkdir(parents=True, exist_ok=True)
|
||||
if mp3_output:
|
||||
mp3_output.parent.mkdir(parents=True, exist_ok=True)
|
||||
if audio_output:
|
||||
audio_output.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
captions = read_srt(srt_path)
|
||||
segment_files = []
|
||||
|
||||
for i, (start, end, text) in enumerate(captions, 1):
|
||||
duration = end - start
|
||||
raw_aiff = work_dir / f"raw_{i:03d}.aiff"
|
||||
segment_wav = work_dir / f"seg_{i:03d}.wav"
|
||||
try:
|
||||
for i, (start, end, text) in enumerate(captions, 1):
|
||||
duration = end - start
|
||||
raw_aiff = work_dir / f"raw_{i:03d}.aiff"
|
||||
segment_wav = work_dir / f"seg_{i:03d}.wav"
|
||||
|
||||
run(["say", "-v", args.voice, "-r", str(args.rate), "-o", str(raw_aiff), text])
|
||||
run(["say", "-v", args.voice, "-r", str(args.rate), "-o", str(raw_aiff), text])
|
||||
|
||||
spoken_duration = media_duration(raw_aiff)
|
||||
if spoken_duration > duration:
|
||||
print(f"Warning: caption {i} speech is {spoken_duration:.2f}s but slot is {duration:.2f}s; it will be trimmed.")
|
||||
spoken_duration = media_duration(raw_aiff)
|
||||
padding = min(args.silence_padding, max(duration - spoken_duration, 0))
|
||||
effective_duration = duration if args.silence_padding == 0 else min(duration, spoken_duration + padding)
|
||||
|
||||
run([
|
||||
"ffmpeg", "-y",
|
||||
"-i", str(raw_aiff),
|
||||
"-af", f"apad,atrim=0:{duration}",
|
||||
"-ar", "44100",
|
||||
"-ac", "2",
|
||||
str(segment_wav),
|
||||
])
|
||||
segment_files.append(segment_wav)
|
||||
if spoken_duration > duration:
|
||||
print(f"Warning: caption {i} speech is {spoken_duration:.2f}s but slot is {duration:.2f}s; it will be trimmed.")
|
||||
|
||||
concat_list = work_dir / "list.txt"
|
||||
concat_list.write_text("".join(f"file '{os.path.abspath(file)}'\n" for file in segment_files), encoding="utf-8")
|
||||
audio_filter = f"volume={args.volume},apad,atrim=0:{effective_duration}"
|
||||
|
||||
run(["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", str(concat_list), "-c:a", "pcm_s16le", str(wav_output)])
|
||||
print(f"Done: {wav_output}")
|
||||
run([
|
||||
"ffmpeg", "-y",
|
||||
"-i", str(raw_aiff),
|
||||
"-af", audio_filter,
|
||||
"-ar", str(args.sample_rate),
|
||||
"-ac", str(args.channels),
|
||||
str(segment_wav),
|
||||
])
|
||||
segment_files.append(segment_wav)
|
||||
|
||||
if mp3_output:
|
||||
run(["ffmpeg", "-y", "-i", str(wav_output), "-codec:a", "libmp3lame", "-b:a", "192k", str(mp3_output)])
|
||||
print(f"Done: {mp3_output}")
|
||||
concat_list = work_dir / "list.txt"
|
||||
concat_list.write_text("".join(f"file '{os.path.abspath(file)}'\n" for file in segment_files), encoding="utf-8")
|
||||
|
||||
run(["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", str(concat_list), "-c:a", "pcm_s16le", str(wav_output)])
|
||||
print(f"Done: {wav_output}")
|
||||
|
||||
if audio_output:
|
||||
run(["ffmpeg", "-y", "-i", str(wav_output), "-c:a", "pcm_s16le", str(audio_output)])
|
||||
print(f"Done: {audio_output}")
|
||||
|
||||
if mp3_output:
|
||||
run(["ffmpeg", "-y", "-i", str(wav_output), "-codec:a", "libmp3lame", "-b:a", "192k", str(mp3_output)])
|
||||
print(f"Done: {mp3_output}")
|
||||
finally:
|
||||
if not args.keep_temp:
|
||||
remove_temp_dir(work_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user