Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pydub import AudioSegment
|
| 3 |
+
import math
|
| 4 |
+
import os
|
| 5 |
+
import zipfile
|
| 6 |
+
|
| 7 |
+
output_dir = "chunks"
|
| 8 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
def speed_change(sound, speed=1.0):
|
| 11 |
+
sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={
|
| 12 |
+
"frame_rate": int(sound.frame_rate * speed)
|
| 13 |
+
})
|
| 14 |
+
return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)
|
| 15 |
+
|
| 16 |
+
def get_audio_info(file):
|
| 17 |
+
if file is None:
|
| 18 |
+
return "Bạn chưa upload file!"
|
| 19 |
+
audio = AudioSegment.from_file(file.name)
|
| 20 |
+
file_size_bytes = os.path.getsize(file.name)
|
| 21 |
+
file_size_mb = file_size_bytes / 1024 / 1024
|
| 22 |
+
duration_sec = len(audio) / 1000
|
| 23 |
+
duration_min = int(duration_sec // 60)
|
| 24 |
+
duration_remain_sec = int(duration_sec % 60)
|
| 25 |
+
bitrate_kbps = round((file_size_bytes * 8) / duration_sec / 1000)
|
| 26 |
+
info = (
|
| 27 |
+
f"Thông tin file '{os.path.basename(file.name)}':\n"
|
| 28 |
+
f"- Kích thước: {file_size_mb:.2f} MB\n"
|
| 29 |
+
f"- Độ dài: {duration_min} phút {duration_remain_sec} giây\n"
|
| 30 |
+
f"- Bitrate ước tính: {bitrate_kbps} kbps"
|
| 31 |
+
)
|
| 32 |
+
return info
|
| 33 |
+
|
| 34 |
+
def process_audio(file, parts, bitrate, speed):
|
| 35 |
+
if file is None:
|
| 36 |
+
return "Bạn chưa upload file!", None
|
| 37 |
+
|
| 38 |
+
audio = AudioSegment.from_file(file.name)
|
| 39 |
+
base_name = os.path.splitext(os.path.basename(file.name))[0]
|
| 40 |
+
|
| 41 |
+
audio_to_cut = speed_change(audio, speed) if speed != 1.0 else audio
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
num_chunks = int(parts)
|
| 45 |
+
except:
|
| 46 |
+
return "Số phần phải là số nguyên!", None
|
| 47 |
+
if num_chunks < 2 or num_chunks > 10:
|
| 48 |
+
return "Số phần phải là số nguyên từ 2 đến 10.", None
|
| 49 |
+
|
| 50 |
+
chunk_length_ms = len(audio_to_cut) // num_chunks
|
| 51 |
+
|
| 52 |
+
paths = []
|
| 53 |
+
for i in range(num_chunks):
|
| 54 |
+
start_ms = i * chunk_length_ms
|
| 55 |
+
end_ms = min((i + 1) * chunk_length_ms, len(audio_to_cut))
|
| 56 |
+
chunk = audio_to_cut[start_ms:end_ms]
|
| 57 |
+
|
| 58 |
+
if i == num_chunks - 1:
|
| 59 |
+
out_name = f"{base_name}end.mp3"
|
| 60 |
+
else:
|
| 61 |
+
out_name = f"{base_name}{i+1}.mp3"
|
| 62 |
+
out_path = os.path.join(output_dir, out_name)
|
| 63 |
+
|
| 64 |
+
if bitrate is None:
|
| 65 |
+
chunk.export(out_path, format="mp3")
|
| 66 |
+
else:
|
| 67 |
+
chunk.export(out_path, format="mp3", bitrate=bitrate)
|
| 68 |
+
|
| 69 |
+
paths.append(out_path)
|
| 70 |
+
|
| 71 |
+
# Tạo file zip để tải tất cả
|
| 72 |
+
zip_path = os.path.join(output_dir, f"{base_name}_parts.zip")
|
| 73 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 74 |
+
for p in paths:
|
| 75 |
+
zipf.write(p, arcname=os.path.basename(p))
|
| 76 |
+
|
| 77 |
+
info = get_audio_info(file)
|
| 78 |
+
# Trả về info và file zip
|
| 79 |
+
return info, zip_path
|
| 80 |
+
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("## Audio Cutter - Cắt file Âm Thanh thành các file nhỏ")
|
| 83 |
+
|
| 84 |
+
file_input = gr.File(label="Upload File Âm Thanh")
|
| 85 |
+
out_info = gr.Textbox(label="Thông tin file", interactive=False)
|
| 86 |
+
|
| 87 |
+
parts_input = gr.Number(value=2, label="Chia thành mấy phần?", precision=0, minimum=2, maximum=10)
|
| 88 |
+
|
| 89 |
+
bitrate_select = gr.Dropdown(
|
| 90 |
+
choices=[("Không đổi", None), ('64k', '64k'), ('96k', '96k'), ('128k', '128k'), ('192k', '192k'), ('256k', '256k')],
|
| 91 |
+
value=None,
|
| 92 |
+
label="Bit rate của file mới"
|
| 93 |
+
)
|
| 94 |
+
speed_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="Tốc độ")
|
| 95 |
+
|
| 96 |
+
btn_process = gr.Button("Cắt & Tải File")
|
| 97 |
+
out_zip = gr.File(label="Download All (zip)")
|
| 98 |
+
|
| 99 |
+
file_input.change(get_audio_info, inputs=file_input, outputs=out_info)
|
| 100 |
+
btn_process.click(
|
| 101 |
+
process_audio,
|
| 102 |
+
inputs=[file_input, parts_input, bitrate_select, speed_slider],
|
| 103 |
+
outputs=[out_info, out_zip]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
demo.launch()
|