This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main-Run
49 lines (39 loc) · 1.62 KB
/
Main-Run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
import os
import numpy as np
import pydub
import matplotlib.pyplot as plt
import textgrid
from pydub import AudioSegment
# textGrid setting :
# Minimum pitch(HZ) 20
# Mininum silent interval duration 1
# Minimum sounding interval duration 0.1
#ffmpeg bin 文件下载: https://www.gyan.dev/ffmpeg/builds/ffmpeg-git-full.7z
#拷贝下载的 bin 到py项目文件夹 否则会报错,找不到文件路径
Path_dir='C:\\Users\\pengchong\\Desktop\\新建文件夹\\'
AudioType='.mp3' # if u want read .wav,replace it to '.wav'
def GetMP3Files(TargetPath):
for parent,dirnames,filenames in os.walk(TargetPath):
dirnames[:]=[d for d in dirnames if d not in['someenv','__pycache__']]
filenames[:]= [f for f in filenames if f.endswith(AudioType)]
return filenames
def CutAudio(files):
for allname in files:
saveid = 0
Sound = AudioSegment.from_mp3(allname) # 声音文件
head=os.path.splitext(allname)[0]
textname=head +'.TextGrid' #TextGrid 分段文件
tg=textgrid.TextGrid.fromFile(Path_dir+textname)
values=len(tg[0].intervals)# 分段长度
if not Sound.empty():
for times in range(len(tg[0].intervals)):
if tg[0][times].mark=='sounding':
# pydub does things in milliseconds
start=tg[0][times].minTime*1000
end=tg[0][times].maxTime*1000
Sound[start:end].export(('./out/'+head+str(saveid)+AudioType),format='mp3')
saveid+=1
if __name__== '__main__':
files=GetMP3Files(Path_dir)
CutAudio(files)