Kryptone commited on
Commit
e39b309
·
verified ·
1 Parent(s): 7c458f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -4
app.py CHANGED
@@ -1,7 +1,7 @@
1
- import gradio as gr, glob, os, auditok, zipfile, wave, pytube.exceptions, urllib.error, requests, json
2
  from pytube import YouTube
3
  from moviepy.editor import *
4
- import traceback, yt_dlp
5
 
6
 
7
  def download_video(url, download_as, use_ytdlp):
@@ -107,9 +107,9 @@ def split_wav_or_mp3_file(audiofileuploader, mindur2, maxdur2, name_for_split_fi
107
  if audiofileuploader == None:
108
  raise gr.Error("Audio file cannot be empty!")
109
  if mindur2 == maxdur2:
110
- raise gr.Error(f"Cannot split mindur={mindur} and maxdur={maxdur}, min and max are the same number.")
111
  elif mindur2 > maxdur2:
112
- raise gr.Error(f"Cannot split mindur={mindur} and maxdur={maxdur}, mindur is higher than maxdur.")
113
  elif name_for_split_files2 == None:
114
  raise gr.Error("Split files name cannot be empty!")
115
  else:
@@ -303,6 +303,19 @@ def mvsep_yt_link_request(mvsep_key2, sep_dropdown2, yt_link):
303
  json_format = r.json()
304
  hash_val = json_format['data']['hash']
305
  return f"Request sent successfully! Your hash is: {hash_val}\n\nUse the next tab to check the status of your request."
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
307
 
308
  with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") as app:
@@ -369,6 +382,20 @@ with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") a
369
  [gr.Text(label="Output"), gr.File(label="Zipped files")]
370
  )
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
  with gr.Tab("Audio only download"):
373
  gr.Markdown("If you want to download only the audio (to isolate bgm using UVR, etc), use this method, which will only extract audio and not split the audio.")
374
  gr.Markdown("# Currently broken. Use Stacher in the meantime.")
@@ -556,6 +583,7 @@ with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") a
556
  )
557
 
558
  with gr.TabItem("Changelog"):
 
559
  gr.Markdown("v0.99.8 - Added new MVSep models.")
560
  gr.Markdown("v0.99.7 - Temporarily fixed an issue causing a 400 error to happen whenever a youtube url was placed.")
561
  gr.Markdown("v0.99.6 - Added a yt link request method for MVSEP.")
 
1
+ import gradio as gr, glob, os, auditok, zipfile, wave, pytube.exceptions, urllib.error, requests, json, traceback, yt_dlp, tempfile
2
  from pytube import YouTube
3
  from moviepy.editor import *
4
+ from pydub import AudioSegment
5
 
6
 
7
  def download_video(url, download_as, use_ytdlp):
 
107
  if audiofileuploader == None:
108
  raise gr.Error("Audio file cannot be empty!")
109
  if mindur2 == maxdur2:
110
+ raise gr.Error(f"Cannot split mindur={mindur2} and maxdur={maxdur2}, min and max are the same number.")
111
  elif mindur2 > maxdur2:
112
+ raise gr.Error(f"Cannot split mindur={mindur2} and maxdur={maxdur2}, mindur is higher than maxdur.")
113
  elif name_for_split_files2 == None:
114
  raise gr.Error("Split files name cannot be empty!")
115
  else:
 
303
  json_format = r.json()
304
  hash_val = json_format['data']['hash']
305
  return f"Request sent successfully! Your hash is: {hash_val}\n\nUse the next tab to check the status of your request."
306
+
307
+ def split_by_dur(file_upload, dur_to_cut):
308
+ if file_upload == None:
309
+ raise gr.Error("Audio file cannot be empty!")
310
+ try:
311
+ audio = AudioSegment.from_file(file_upload)
312
+ trimmed_audio = audio[:dur_to_cut]
313
+ ms_to_min = round((dur_to_cut/(1000*60))%60, 1)
314
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
315
+ trimmed_audio.export(temp_file.name, format="mp3")
316
+ return f"Trimmed to {ms_to_min} minutes successfully.", temp_file.name
317
+ except Exception as e:
318
+ return gr.Error(traceback.format_exc())
319
 
320
 
321
  with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") as app:
 
382
  [gr.Text(label="Output"), gr.File(label="Zipped files")]
383
  )
384
 
385
+ with gr.Tab("Split audio file by duration"):
386
+ gr.Markdown("If you have an audio file thats too long for MVSEP (or any other use cases), use this section to split the audio by duration.")
387
+ with gr.Row():
388
+ with gr.Column():
389
+ with gr.Row():
390
+ file_upload = gr.File(file_count='single', file_types=[".wav", ".mp3"], label="WAV or MP3 file")
391
+ dur_to_cut = gr.Number(label="Duration to split audio (in ms). Cannot be larger than the duration of the audio file.", precision=0)
392
+ begin = gr.Button("Split at given duration", variant='primary')
393
+ begin.click(
394
+ split_by_dur,
395
+ [file_upload, dur_to_cut],
396
+ [gr.Text(label="Output"), gr.File(label="Trimmed audio")]
397
+ )
398
+
399
  with gr.Tab("Audio only download"):
400
  gr.Markdown("If you want to download only the audio (to isolate bgm using UVR, etc), use this method, which will only extract audio and not split the audio.")
401
  gr.Markdown("# Currently broken. Use Stacher in the meantime.")
 
583
  )
584
 
585
  with gr.TabItem("Changelog"):
586
+ gr.Markdown("v0.99.9 - Added new tool: Split audio file by duration.")
587
  gr.Markdown("v0.99.8 - Added new MVSep models.")
588
  gr.Markdown("v0.99.7 - Temporarily fixed an issue causing a 400 error to happen whenever a youtube url was placed.")
589
  gr.Markdown("v0.99.6 - Added a yt link request method for MVSEP.")