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))
'''


版权声明:本文为tiger9991原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。