File size: 2,937 Bytes
944de9c ba5d53e 944de9c 90dcff8 944de9c ba5d53e 944de9c ba5d53e 944de9c ba5d53e 944de9c b51fe37 944de9c 135c3d8 1bb4ed2 944de9c 1bb4ed2 944de9c |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import glob
import os
import streamlit as st
import glob
import librosa
from pydub import AudioSegment
import math
import shutil
from pathlib import Path
class SplitWavAudioMubin():
def __init__(self, folder, filename):
self.folder = folder
self.filename = filename
self.filepath = folder + '/' + filename
self.audio = AudioSegment.from_mp3(self.filepath)
def get_duration(self):
return self.audio.duration_seconds
def single_split(self, from_min, to_min, split_filename):
t1 = from_min * 60 * 1000
t2 = to_min * 60 * 1000
split_audio = self.audio[t1:t2]
split_audio.export(self.folder + '/' + split_filename, format="mp3")
def multiple_split(self, min_per_split):
total_mins = math.ceil(self.get_duration() / 60)
for i in range(0, total_mins, min_per_split):
split_fn = str(i) + '_' + self.filename
self.single_split(i, i+min_per_split, split_fn)
print(str(i) + ' Done')
if i == total_mins - min_per_split:
print('All splited successfully')
uploaded_file = st.sidebar.file_uploader(label = "Please upload your file ",
type=['wav', 'mp3','m4a'])
values = st.sidebar.slider(
'Number of Mins',
0, 30, 1)
submit_button = st.sidebar.button(label='Submit')
if uploaded_file is not None:
with open(os.path.join("./",uploaded_file.name),"wb") as f:
f.write(uploaded_file.getbuffer())
Path("./small").mkdir(parents=True, exist_ok=True)
length_audio = librosa.get_duration(filename=uploaded_file.name)/60
# convert to mp3
format_ = uploaded_file.name.split('.')[-1]
outputname = uploaded_file.name + ".mp3"
sound = AudioSegment.from_file(uploaded_file,format=format_)
sound.export(outputname, format="mp3")
st.title("Audio2Many")
st.write('一共有', round(length_audio, 2) , "分钟")
st.write('被分成', math.ceil(round(length_audio, 2)/values) , "份")
# Main app engine
if __name__ == "__main__":
if submit_button:
# remove files
files = glob.glob('small/*.mp3')
for f in files:
try:
os.remove(f)
except OSError as e:
print("Error: %s : %s" % (f, e.strerror))
try:
os.remove("小碎片们.zip")
except:
print("There is not zip file, continue")
pass
# genrate data
with st.spinner('Wait for cutting...'):
shutil.move(outputname,"./small/"+outputname)
split_wav = SplitWavAudioMubin("./small", outputname)
split_wav.multiple_split(min_per_split=values)
os.remove("./small/"+outputname)
# downloadable button
shutil.make_archive("小碎片们", 'zip', "small")
with open("小碎片们.zip", 'rb') as f:
st.download_button('Download File', f, file_name="小碎片们.zip") |