deepfake_detection / analyze_debug.py
VoiceGuard Bot
Optimize: Robust M4A handling and Noise Injection (Dithering) to prevent compression artifacts false positives
65aeed7
Raw
History Blame
1.45 kB
import numpy as np
import librosa
import json
import warnings
warnings.filterwarnings("ignore")
def analyze_audio(file_path):
print(f"Analyzing {file_path}...")
try:
# Use pydub first (mimic app behavior)
from pydub import AudioSegment
import io
print(" Loading with pydub...")
audio = AudioSegment.from_file(file_path)
wav_io = io.BytesIO()
audio.export(wav_io, format="wav")
wav_io.seek(0)
y, sr = librosa.load(wav_io, sr=16000)
# 1. Silence Check
rms = librosa.feature.rms(y=y)[0]
silence_percent = np.sum(rms < 0.01) / len(rms)
# 2. Spectral Properties (AI often has lower variance)
centroid = librosa.feature.spectral_centroid(y=y, sr=sr)[0]
centroid_var = np.var(centroid)
# 3. Zero Crossing Rate
zcr = librosa.feature.zero_crossing_rate(y)[0]
zcr_var = np.var(zcr)
results = {
"duration": len(y)/sr,
"silence_percent": float(silence_percent),
"rms_mean": float(np.mean(rms)),
"centroid_var": float(centroid_var),
"zcr_var": float(zcr_var),
"sample_rate": sr
}
print(json.dumps(results, indent=2))
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
analyze_audio("SH 69 4.mp3")