upload app
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import glob
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
import glob
|
5 |
+
import librosa
|
6 |
+
from pydub import AudioSegment
|
7 |
+
import math
|
8 |
+
import shutil
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
class SplitWavAudioMubin():
|
12 |
+
def __init__(self, folder, filename):
|
13 |
+
self.folder = folder
|
14 |
+
self.filename = filename
|
15 |
+
self.filepath = folder + '/' + filename
|
16 |
+
|
17 |
+
self.audio = AudioSegment.from_wav(self.filepath)
|
18 |
+
|
19 |
+
def get_duration(self):
|
20 |
+
return self.audio.duration_seconds
|
21 |
+
|
22 |
+
def single_split(self, from_min, to_min, split_filename):
|
23 |
+
t1 = from_min * 60 * 1000
|
24 |
+
t2 = to_min * 60 * 1000
|
25 |
+
split_audio = self.audio[t1:t2]
|
26 |
+
split_audio.export(self.folder + '/' + split_filename, format="wav")
|
27 |
+
|
28 |
+
def multiple_split(self, min_per_split):
|
29 |
+
total_mins = math.ceil(self.get_duration() / 60)
|
30 |
+
for i in range(0, total_mins, min_per_split):
|
31 |
+
split_fn = str(i) + '_' + self.filename
|
32 |
+
self.single_split(i, i+min_per_split, split_fn)
|
33 |
+
print(str(i) + ' Done')
|
34 |
+
if i == total_mins - min_per_split:
|
35 |
+
print('All splited successfully')
|
36 |
+
|
37 |
+
uploaded_file = st.sidebar.file_uploader(label = "Please upload your file ",
|
38 |
+
type=['wav', 'mp3','m4a'])
|
39 |
+
values = st.sidebar.slider(
|
40 |
+
'Number of Mins',
|
41 |
+
0, 30, 1)
|
42 |
+
submit_button = st.sidebar.button(label='Submit')
|
43 |
+
|
44 |
+
if uploaded_file is not None:
|
45 |
+
with open(os.path.join("./",uploaded_file.name),"wb") as f:
|
46 |
+
f.write(uploaded_file.getbuffer())
|
47 |
+
|
48 |
+
Path("./small").mkdir(parents=True, exist_ok=True)
|
49 |
+
length_audio = librosa.get_duration(filename=uploaded_file.name)/60
|
50 |
+
|
51 |
+
# convert to wav
|
52 |
+
format_ = uploaded_file.name.split('.')[-1]
|
53 |
+
outputname = uploaded_file.name + ".wav"
|
54 |
+
sound = AudioSegment.from_file(uploaded_file,format=format_)
|
55 |
+
sound.export(outputname, format="wav")
|
56 |
+
|
57 |
+
st.title("Audio2Many")
|
58 |
+
st.write('δΈε
±ζ', round(length_audio, 2) , "ει")
|
59 |
+
st.write('θ’«εζ', math.ceil(round(length_audio, 2)/values) , "δ»½")
|
60 |
+
|
61 |
+
|
62 |
+
# Main app engine
|
63 |
+
if __name__ == "__main__":
|
64 |
+
|
65 |
+
if submit_button:
|
66 |
+
|
67 |
+
# remove files
|
68 |
+
files = glob.glob('small/*.wav')
|
69 |
+
for f in files:
|
70 |
+
try:
|
71 |
+
os.remove(f)
|
72 |
+
except OSError as e:
|
73 |
+
print("Error: %s : %s" % (f, e.strerror))
|
74 |
+
|
75 |
+
# genrate data
|
76 |
+
shutil.move(outputname,"./small/"+outputname)
|
77 |
+
split_wav = SplitWavAudioMubin("./small", outputname)
|
78 |
+
split_wav.multiple_split(min_per_split=values)
|
79 |
+
os.remove("./small/"+outputname)
|
80 |
+
# downloadable button
|
81 |
+
shutil.make_archive("ε°η’η们", 'zip', "small")
|
82 |
+
|
83 |
+
with open("ε°η’η们.zip", 'rb') as f:
|
84 |
+
st.download_button('Download File', f, file_name="ε°η’η们.zip")
|