python_检测音频人声片段

python_检测音频人声片段

对声源有要求,不一定所有音频的识别结果都是我们想要的

python 复制代码
# 使用此指令前,请确保安装必要的Python库,例如使用以下命令安装:
# pip install librosa numpy

import librosa
import numpy as np

from typing import *
try:
    from xbot.app.logging import trace as print
except:
    from xbot import print


def detect_voice_segments(audio_path):
    """
    title: 检测音频人声片段
    description: 检测音频文件中所有人声片段的起止时间,基于短时能量算法识别有效人声区域,过滤噪声干扰,返回以微秒为单位的时间段列表。
    inputs:
        - audio_path (file): 音频文件路径,eg: "audio.mp3"
    outputs:
        - voice_segments (list): 人声片段时间列表,每个元素为[起始微秒, 结束微秒],eg: "[[1000000, 3500000], [5000000, 8200000]]"
    """
    
    def _seconds_to_microseconds(seconds):
        """
        将秒数转换为微秒(1秒 = 1000000微秒)
        """
        seconds = float(seconds)
        return int(round(seconds * 1000000))
    
    # 检测参数配置
    threshold = 0.05        # 能量阈值,低于则视为静音
    min_voice_dur = 0.1     # 最小有效人声时长(秒),过滤噪声

    try:
        # 1. 加载音频
        y, sr = librosa.load(audio_path, sr=None)
        total_samples = len(y)
        total_seconds = total_samples / sr

        # 2. 计算短时能量(RMS)
        frame_length = 2048
        hop_length = 512
        energy = librosa.feature.rms(y=y, frame_length=frame_length, hop_length=hop_length).flatten()
        times = librosa.frames_to_time(np.arange(len(energy)), sr=sr, hop_length=hop_length)

        # 3. 判断每帧是否为人声
        is_voice = energy > threshold

        # 4. 合并连续人声段(简单状态机)
        segments = []
        in_voice = False
        start_frame = None

        for i, voice in enumerate(is_voice):
            if voice and not in_voice:
                # 进入人声
                in_voice = True
                start_frame = i
            elif not voice and in_voice:
                # 离开人声
                end_frame = i - 1
                segments.append((start_frame, end_frame))
                in_voice = False

        # 处理结尾仍为人声的情况
        if in_voice:
            segments.append((start_frame, len(is_voice) - 1))

        # 5. 转换为时间并过滤太短的段
        valid_segments = []

        for start_f, end_f in segments:
            start_t = times[start_f]
            end_t = times[end_f] + hop_length / sr  # 补偿最后一帧的时间偏移
            # 确保不超出总时长
            end_t = min(end_t, total_seconds)
            
            if (end_t - start_t) >= min_voice_dur:
                start_us = _seconds_to_microseconds(start_t)
                end_us = _seconds_to_microseconds(end_t)
                valid_segments.append([start_us, end_us])

        return valid_segments

    except Exception as e:
        raise Exception(f"处理音频文件时出错:{str(e)}")
相关推荐
apocelipes12 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户83562907805114 小时前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent18 小时前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6251 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
SelectDB2 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码2 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵2 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li2 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸3 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学3 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员