slimshadow commited on
Commit
658aea7
·
verified ·
1 Parent(s): 78a0a79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -9
app.py CHANGED
@@ -1,21 +1,45 @@
1
  import streamlit as st
2
  from pydub import AudioSegment
 
3
 
4
  st.title("Audio Cutter")
5
 
6
  uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav"])
7
 
8
  if uploaded_file is not None:
 
9
  st.audio(uploaded_file, format='audio/wav')
10
 
11
- start_time = st.number_input("Start time (in seconds)", value=0)
12
- end_time = st.number_input("End time (in seconds)", value=10)
 
13
 
14
- if start_time < end_time:
15
- st.warning("Start time must be less than end time.")
16
- else:
17
- st.success("Start time must be less than end time.")
18
- audio = AudioSegment.from_file(uploaded_file)
19
- clipped_audio = audio[start_time * 1000:end_time * 1000]
 
 
 
 
20
 
21
- st.audio(clipped_audio.export(), format='audio/wav')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from pydub import AudioSegment
3
+ from io import BytesIO
4
 
5
  st.title("Audio Cutter")
6
 
7
  uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav"])
8
 
9
  if uploaded_file is not None:
10
+ # Display the uploaded audio file
11
  st.audio(uploaded_file, format='audio/wav')
12
 
13
+ # Inputs for start and end time
14
+ start_time = st.number_input("Start time (in seconds)", value=0, min_value=0)
15
+ end_time = st.number_input("End time (in seconds)", value=10, min_value=0)
16
 
17
+ # Check if the end time is greater than start time
18
+ if st.button("Cut Audio"):
19
+ if start_time >= end_time:
20
+ st.error("End time must be greater than start time.")
21
+ else:
22
+ # Load the audio file
23
+ audio = AudioSegment.from_file(uploaded_file)
24
+
25
+ # Ensure the end time does not exceed the length of the audio
26
+ end_time = min(end_time, len(audio) / 1000.0)
27
 
28
+ # Clip the audio
29
+ clipped_audio = audio[start_time * 1000:end_time * 1000]
30
+
31
+ # Export the clipped audio to a BytesIO object
32
+ buf = BytesIO()
33
+ clipped_audio.export(buf, format='wav')
34
+ buf.seek(0)
35
+
36
+ # Display the clipped audio
37
+ st.audio(buf, format='audio/wav')
38
+
39
+ # Option to download the clipped audio
40
+ st.download_button(
41
+ label="Download Clipped Audio",
42
+ data=buf,
43
+ file_name="clipped_audio.wav",
44
+ mime="audio/wav"
45
+ )