#!/usr/bin/env python3
import sys
import time

print("Start")
print("Importing faster_whisper...")
try:
    from faster_whisper import WhisperModel

    print("Import successful")
except Exception as e:
    print(f"Import failed: {e}")
    sys.exit(1)

print("Loading model...")
try:
    model = WhisperModel("tiny", device="cpu", compute_type="int8")
    print("Model loaded")
except Exception as e:
    print(f"Model load failed: {e}")
    sys.exit(1)

import subprocess

print("Getting duration...")
cmd = [
    "ffprobe",
    "-v",
    "error",
    "-show_entries",
    "format=duration",
    "-of",
    "csv=p=0",
    "/tmp/test_audio.wav",
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(f"ffprobe output: {result.stdout}")
duration = float(result.stdout.strip())
print(f"Duration: {duration}")

# Extract first chunk
print("Extracting first chunk...")
chunk_path = "/tmp/debug_chunk.wav"
cmd = [
    "ffmpeg",
    "-i",
    "/tmp/test_audio.wav",
    "-t",
    "60",
    "-acodec",
    "pcm_s16le",
    "-ar",
    "16000",
    "-ac",
    "1",
    "-y",
    chunk_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(f"ffmpeg return code: {result.returncode}")
if result.returncode != 0:
    print(f"stderr: {result.stderr[:200]}")

import os

print(f"Chunk exists: {os.path.exists(chunk_path)}")
if os.path.exists(chunk_path):
    print(f"Chunk size: {os.path.getsize(chunk_path)}")

    print("Transcribing chunk...")
    start = time.time()
    try:
        segments, info = model.transcribe(chunk_path, beam_size=5)
        segments = list(segments)
        elapsed = time.time() - start
        print(f"Transcription succeeded in {elapsed}s, segments: {len(segments)}")
    except Exception as e:
        print(f"Transcription failed: {e}")
        import traceback

        traceback.print_exc()
else:
    print("Chunk not created")

print("Script finished")
