Spaces:
Runtime error
Runtime error
File size: 1,509 Bytes
e507799 24f6d4f e507799 0e5c9f0 e507799 0e5c9f0 e61d283 0e5c9f0 e61d283 |
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 |
from pydub import AudioSegment
import os
import zipfile
import tempfile
def cut_audio(input_path, output_path, interval):
audio = AudioSegment.from_file(input_path)
duration = len(audio)
cuts = [audio[i:i + interval] for i in range(0, duration, interval)]
with tempfile.TemporaryDirectory() as temp_dir:
for i, cut in enumerate(cuts):
cut.export(os.path.join(temp_dir, f'cut_{i}.wav'), format="wav")
# Create a zip file
zip_file_path = os.path.join(output_path, "cuts.zip")
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
for root, _, files in os.walk(temp_dir):
for file in files:
zipf.write(os.path.join(root, file), os.path.basename(file))
return zip_file_path
def process_audio(audio, interval):
# Convert the interval to an integer
interval = int(interval)
# Temporary directory to store the processed files
temp_output_dir = '/content' # This is writable in Hugging Face Spaces
# Process the audio file and get the path to the zip file
zip_file_path = cut_audio(audio.name, temp_output_dir, interval)
# Move the zip file to a publicly accessible location
public_zip_path = f'/content/{os.path.basename(zip_file_path)}'
os.rename(zip_file_path, public_zip_path)
# Create a download link
download_link = f'<a href="{public_zip_path}" download>Click here to download</a>'
# Return the download link as HTML
return download_link
|