python音频智能分割

import auditok
import os
import sys

dir = os.getcwd()
fileName = sys.argv[1]
fileAllName = sys.argv[1] + ".mp4"
filePath = dir + "\\" + fileAllName
outMp3Name = fileName + '.wav'

def getVideoWav():
    exeCmdLine = f"ffmpeg -i {fileAllName} -vn {outMp3Name}"
    print("exeCmdLine:" + exeCmdLine)
    os.system(exeCmdLine)
    print("get mp3 successfully!")

def splitWav():
    print("start split wav")
    audio_regions = auditok.split(
        outMp3Name,
        min_dur=0.2,  # minimum duration of a valid audio event in seconds
        max_dur=4,  # maximum duration of an event
        max_silence=0.3,  # maximum duration of tolerated continuous silence within an event
        energy_threshold=55  # threshold of detectiony
    )

    #print("audio_regions count:" + sum(audio_regions))

    for i, r in enumerate(audio_regions):
        # Regions returned by `split` have 'start' and 'end' metadata fields
        print("Region {i}: {r.meta.start:.3f}s -- {r.meta.end:.3f}s".format(i=i, r=r))

        # play detection
        #r.play(progress_bar=True)

        # region's metadata can also be used with the `save` method
        # (no need to explicitly specify region's object and `format` arguments)
        filename = r.save("region_{meta.start:.3f}-{meta.end:.3f}.wav")
        print("region saved as: {}".format(filename))


#main start


#check file existed
print("fileName:" + fileName)
print("fileAllName:" + fileAllName)
print("filePath:" + filePath)
if not(os.path.isfile(filePath)):
    print("file not exsited!")
    sys.exit()

#second
getVideoWav()
splitWav()

'''
# split returns a generator of AudioRegion objects
audio_regions = auditok.split(
    "audio.wav",
    min_dur=0.2,     # minimum duration of a valid audio event in seconds
    max_dur=4,       # maximum duration of an event
    max_silence=0.3, # maximum duration of tolerated continuous silence within an event
    energy_threshold=55 # threshold of detection
)


for i, r in enumerate(audio_regions):

    # Regions returned by `split` have 'start' and 'end' metadata fields
    print("Region {i}: {r.meta.start:.3f}s -- {r.meta.end:.3f}s".format(i=i, r=r))

    # play detection
    # r.play(progress_bar=True)

    # region's metadata can also be used with the `save` method
    # (no need to explicitly specify region's object and `format` arguments)
    filename = r.save("region_{meta.start:.3f}-{meta.end:.3f}.wav")
    print("region saved as: {}".format(filename))
'''

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值