mirror of
https://github.com/msitarzewski/agency-agents.git
synced 2026-10-01 13:55:54 +03:00
Decode compressed audio into valid PCM chunks for transcription
This commit is contained in:
@@ -178,7 +178,7 @@ def preprocess_audio(input_path: str, output_path: str) -> str:
|
||||
|
||||
|
||||
def chunk_audio(input_path: str, chunk_dir: str,
|
||||
chunk_duration: int = 1800, overlap: int = 30) -> list[str]:
|
||||
chunk_duration: int = 1800, overlap: int = 30) -> list[dict]:
|
||||
"""
|
||||
Split long audio into overlapping chunks for model processing.
|
||||
|
||||
@@ -189,11 +189,17 @@ def chunk_audio(input_path: str, chunk_dir: str,
|
||||
overlap: overlap window in seconds (default 30s)
|
||||
"""
|
||||
import math, os
|
||||
if not math.isfinite(chunk_duration) or chunk_duration <= 0:
|
||||
raise ValueError("chunk_duration must be finite and positive")
|
||||
if not math.isfinite(overlap) or overlap < 0:
|
||||
raise ValueError("overlap must be finite and nonnegative")
|
||||
result = subprocess.run([
|
||||
"ffprobe", "-v", "quiet", "-show_entries", "format=duration",
|
||||
"-of", "default=noprint_wrappers=1:nokey=1", input_path
|
||||
], capture_output=True, text=True, check=True)
|
||||
total_duration = float(result.stdout.strip())
|
||||
if not math.isfinite(total_duration) or total_duration <= 0:
|
||||
raise ValueError("Audio duration must be finite and positive")
|
||||
|
||||
chunks = []
|
||||
start = 0
|
||||
@@ -205,10 +211,11 @@ def chunk_audio(input_path: str, chunk_dir: str,
|
||||
out_path = f"{chunk_dir}/chunk_{chunk_index:04d}.wav"
|
||||
subprocess.run([
|
||||
"ffmpeg", "-y",
|
||||
"-i", input_path,
|
||||
"-ss", str(start),
|
||||
"-to", str(end),
|
||||
"-acodec", "copy",
|
||||
"-i", input_path,
|
||||
"-t", str(end - start),
|
||||
"-map", "0:a:0", "-vn",
|
||||
"-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
|
||||
out_path
|
||||
], check=True, capture_output=True)
|
||||
chunks.append({"path": out_path, "start_offset": start, "index": chunk_index})
|
||||
|
||||
Reference in New Issue
Block a user