音频出声检测

AttributeError: 'AudioSegment' object has no attribute 'frequency'


import moviepy.editor as mp
from pydub import AudioSegment



clip = mp.VideoFileClip('video/22.mp4')

# 分离音频并保存
clip.audio.write_audiofile("audio/23.wav")


audio = AudioSegment.from_file("audio/23.wav", format="wav")

mono_audio = audio.set_channels(1)

mono_audio.export("audio/mono_audio.wav",format="wav")

# 读取音频文件
audio_file = AudioSegment.from_file("audio/mono_audio.wav",format="wav")

# 设置目标频率
target_frequency = 1000

# 设置目标频率的容差范围
tolerance = 50

# 找出目标频率的起始时间点和结束时间点
start_time = None
end_time = None
for i in range(len(audio_file)):
    # 获取当前时间点的频率
    frequency = audio_file[i].frequency

    # 判断当前频率是否在目标频率的容差范围内
    if abs(frequency - target_frequency) <= tolerance:
        # 如果起始时间点还没有被设置,则设置为当前时间点
        if start_time is None:
            start_time = i
        # 如果起始时间点已经被设置,则更新结束时间点为当前时间点
        else:
            end_time = i

# 输出结果
print("起始时间点:", start_time)
print("结束时间点:", end_time)
import numpy as np
import librosa
import moviepy.editor as mp


# Load the audio file
clip = mp.VideoFileClip('video/22.mp4')

# 分离音频并保存
clip.audio.write_audiofile("audio/23.wav")

# Load the audio file
y, sr = librosa.load('audio/23.wav')

# Compute Short-Time Fourier Transform (STFT)
D = np.abs(librosa.stft(y))

# Compute frequencies for STFT
frequencies = librosa.core.fft_frequencies(sr=sr, n_fft=1 + 2 * (D.shape[0] - 1))

# Frequency to look for
target_frequency = 1000

# Find the bin corresponding to the target frequency
target_index = np.abs(frequencies - target_frequency).argmin()

# Compute the time points where the frequency is present
times = np.where(D[target_index, :] > 0)[0] / D.shape[1]

print(times)

import numpy as np
import librosa

y, sr = librosa.load('/path/to/audio.wav', sr=None)  # load audio

frame_length = 1024
hop_length = 512

# compute short time fourier transform
D = librosa.stft(y, n_fft=frame_length, hop_length=hop_length)

# compute amplitude
amp = np.abs(D)

# convert frequency bin to actual frequency value
freqs = librosa.fft_frequencies(sr=sr, n_fft=frame_length)

# find closest frequency bin to 1000Hz
target_bin_idx = np.argmin(np.abs(freqs - 1000))

# find where amplitude is above a threshold
# (you might need to adjust the threshold depending on your audio)
threshold = np.median(amp) * 1.5
mask = amp[target_bin_idx, :] > threshold

# convert frame index to timestamp
timestamps = librosa.frames_to_time(np.arange(len(mask)), sr=sr, hop_length=hop_length)

# find start and end time
start_times = timestamps[1:][np.diff(mask.astype(int)) > 0]
end_times = timestamps[:-1][np.diff(mask.astype(int)) < 0]

print('Start times:', start_times)
print('End times:', end_times)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值