首页 > 其他 > 详细

pydub分割音频文件

时间:2020-04-02 20:40:54      阅读:96      评论:0      收藏:0      [点我收藏+]

首先安装需要的库
技术分享图片
然后安装ffmpeg
ffmpeg
安装完成后将bin配进环境变量

# -*- coding: utf-8 -*-
from pydub import AudioSegment
from pydub.silence import detect_silence
import os
import uuid


# 生成guid
def GUID():
    return str(uuid.uuid1()).replace("-", "")


# 分割文件
def SplitSound(filename, save_path, save_file_name, start_time, end_time, audio_type=‘mp3‘):
    if not os.path.exists(save_path):
        try:
            os.mkdir(save_path)
        except Exception as e:
            print(e)

    sound = AudioSegment.from_file(filename, format=audio_type)
    result = sound[start_time:end_time]
    final_name = savePath
    if not savePath.endswith("/"):
        final_name = final_name + "/"
    final_name = final_name + save_file_name

    result.export(final_name, format=audio_type)
    # AudioSegment.export(result, format=audioType)


def SplitSilence(file_name, save_path, audio_type=‘mp3‘):
    sound = AudioSegment.from_file(file_name, format=audio_type)
    # print(len(sound))
    # print(sound.max_possible_amplitude)
    # start_end = detect_silence(sound,800,-57,1)
    start_end = detect_silence(sound, 300, -35, 1)

    # print(start_end)
    start_point = 0
    index = 1

    for item in start_end:
        if item[0] != 0:
            # 取空白部分的中位数
            end_point = (item[0] + item[1]) / 2
            print("%d-%d" % (start_point, end_point))
            SplitSound(file_name, save_path, str(index) + ".mp3", start_point, end_point)
            index = index + 1
        start_point = item[1]

    # 处理最后一段音频
    # sound.len
    SplitSound(file_name, save_path, str(index) + ".mp3", start_point, len(sound))
    # len(sound)


audioPath = "C:/test/source.mp3"
savePath = "C:/formatoutput/save"
SplitSilence(audioPath, savePath)

pydub分割音频文件

原文:https://www.cnblogs.com/ives/p/12622344.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!