Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
import gradio as gr, glob, os, auditok, zipfile, wave, pytube.exceptions,
|
| 2 |
from pytube import YouTube
|
| 3 |
-
from moviepy.editor import
|
|
|
|
| 4 |
|
| 5 |
def download_video(url, download_as, use_ytdlp):
|
| 6 |
if use_ytdlp == True:
|
|
@@ -101,7 +102,7 @@ def split_audio_from_yt_video(mindur, maxdur, name_for_split_files, show_amount_
|
|
| 101 |
os.remove(file2)
|
| 102 |
return f"Files split successfully!\nCheck below for zipped files.\n\n{total_files} files created, {length_mins:.2f} minutes total.", zip_file_name
|
| 103 |
|
| 104 |
-
def split_wav_or_mp3_file(audiofileuploader, mindur2, maxdur2, name_for_split_files2):
|
| 105 |
if audiofileuploader == None:
|
| 106 |
raise gr.Error("Audio file cannot be empty!")
|
| 107 |
if mindur2 == maxdur2:
|
|
@@ -117,7 +118,8 @@ def split_wav_or_mp3_file(audiofileuploader, mindur2, maxdur2, name_for_split_fi
|
|
| 117 |
min_dur=mindur2,
|
| 118 |
max_dur=maxdur2,
|
| 119 |
max_silence=0.3,
|
| 120 |
-
energy_threshold=45
|
|
|
|
| 121 |
)
|
| 122 |
os.remove(audio_path)
|
| 123 |
for i, r in enumerate(audio_regions):
|
|
@@ -132,63 +134,7 @@ def split_wav_or_mp3_file(audiofileuploader, mindur2, maxdur2, name_for_split_fi
|
|
| 132 |
os.remove(file2)
|
| 133 |
return f"File split successfully!\nCheck below for zipped files.\nAmount created: {len(audio_files)}", zip_file_name2
|
| 134 |
|
| 135 |
-
def
|
| 136 |
-
if download_as == "mp3":
|
| 137 |
-
gr.Warning("MP3 is experimental, especially with this, so caution is advised.")
|
| 138 |
-
if min_duration == max_duration:
|
| 139 |
-
raise gr.Error(f"Cannot split mindur={min_duration} and maxdur={max_duration}, min and max are the same number.")
|
| 140 |
-
elif min_duration > max_duration:
|
| 141 |
-
raise gr.Error(f"Cannot split mindur={min_duration} and maxdur={max_duration}, mindur is higher than maxdur.")
|
| 142 |
-
elif name_for_outputted_split_files == None:
|
| 143 |
-
raise gr.Error("Split files name cannot be empty!")
|
| 144 |
-
else:
|
| 145 |
-
try:
|
| 146 |
-
progress(0, "Downloading video...")
|
| 147 |
-
yt = YouTube(ytvideo)
|
| 148 |
-
except pytube.exceptions.RegexMatchError:
|
| 149 |
-
raise gr.Error("URL not valid or was left empty! Please fix the link or enter one.")
|
| 150 |
-
except urllib.error.HTTPError as not_ok:
|
| 151 |
-
raise gr.Error(f"Recieved {not_ok}")
|
| 152 |
-
except pytube.exceptions.AgeRestrictedError:
|
| 153 |
-
raise gr.Error("The video you inputted is age-restricted! Please try another link.")
|
| 154 |
-
video = yt.streams.get_highest_resolution()
|
| 155 |
-
video.download()
|
| 156 |
-
video_path = f"{video.default_filename}"
|
| 157 |
-
video_clip = VideoFileClip(video_path)
|
| 158 |
-
audio_clip = video_clip.audio
|
| 159 |
-
if download_yt_video_as == "wav":
|
| 160 |
-
audio_clip.write_audiofile("output.wav")
|
| 161 |
-
elif download_yt_video_as == "mp3":
|
| 162 |
-
audio_clip.write_audiofile("output.mp3")
|
| 163 |
-
audio_clip.close()
|
| 164 |
-
video_clip.close()
|
| 165 |
-
for removemp4 in glob.glob("*.mp4"):
|
| 166 |
-
os.remove(removemp4)
|
| 167 |
-
progress(0.5, "Video downloaded! Starting split process...")
|
| 168 |
-
audio_path = "output.wav" if not os.path.exists("output.mp3") else "output.mp3"
|
| 169 |
-
audio_regions = auditok.split(
|
| 170 |
-
audio_path,
|
| 171 |
-
min_dur=min_duration,
|
| 172 |
-
max_dur=max_duration,
|
| 173 |
-
max_silence=0.3,
|
| 174 |
-
energy_threshold=45
|
| 175 |
-
)
|
| 176 |
-
os.remove(audio_path)
|
| 177 |
-
for i, r in enumerate(audio_regions):
|
| 178 |
-
filename = r.save(f"{name_for_outputted_split_files}-{i+1}.wav")
|
| 179 |
-
for f in sorted(glob.glob("*.wav")):
|
| 180 |
-
audio_files = glob.glob("*.wav")
|
| 181 |
-
zip_file_name = "audio_files.zip"
|
| 182 |
-
with zipfile.ZipFile(zip_file_name, 'w') as zip_file:
|
| 183 |
-
for audio_file in audio_files:
|
| 184 |
-
zip_file.write(audio_file, os.path.basename(audio_file))
|
| 185 |
-
for file2 in glob.glob("*.wav"):
|
| 186 |
-
os.remove(file2)
|
| 187 |
-
progress(1, "Done! Cleaning up...")
|
| 188 |
-
time.sleep(2)
|
| 189 |
-
return "Process done successfully! Check below for zipped files!", zip_file_name
|
| 190 |
-
|
| 191 |
-
def download_video_as_audio_only(yt_video, audio_output_format):
|
| 192 |
try:
|
| 193 |
yt = YouTube(yt_video)
|
| 194 |
except pytube.exceptions.RegexMatchError:
|
|
@@ -223,24 +169,32 @@ def download_video_as_audio_only(yt_video, audio_output_format):
|
|
| 223 |
os.remove(outputmp3removal)
|
| 224 |
return f"Done! Download the zip file below! This only contains the audio file.\n\nYou have downloaded {yt.title} by {yt.author}.", single_zip_name
|
| 225 |
|
| 226 |
-
def
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
def display_audio_spectrogram(audio_file):
|
| 231 |
-
if audio_file == None:
|
| 232 |
-
raise gr.Error("Cannot leave WAV field empty! Please insert a WAV file.")
|
| 233 |
else:
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") as app:
|
| 246 |
gr.HTML(
|
|
@@ -258,9 +212,6 @@ with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") a
|
|
| 258 |
gr.HTML(
|
| 259 |
"<h2> This Space's storage is ephemeral, meaning once you reload this space, all audio files will be lost. </h2>"
|
| 260 |
)
|
| 261 |
-
gr.HTML(
|
| 262 |
-
"<h3> Colab version in development, this text will be updated when it's fully ready. </h3>"
|
| 263 |
-
)
|
| 264 |
with gr.Tabs():
|
| 265 |
with gr.TabItem("Download Video"):
|
| 266 |
with gr.Row():
|
|
@@ -272,7 +223,7 @@ with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") a
|
|
| 272 |
convertion = gr.Button("Download", variant='primary')
|
| 273 |
convertion.click(
|
| 274 |
fn=download_video,
|
| 275 |
-
inputs=[url, download_as],
|
| 276 |
outputs=gr.Text(label="Output")
|
| 277 |
)
|
| 278 |
with gr.TabItem("Split audio files"):
|
|
@@ -299,28 +250,14 @@ with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") a
|
|
| 299 |
mindur2 = gr.Number(label="Min duration", minimum=1, maximum=10, value=1)
|
| 300 |
maxdur2 = gr.Number(label="Max duration", minimum=1, maximum=10, value=5)
|
| 301 |
name_for_split_files2 = gr.Textbox(label="Name for split files")
|
|
|
|
| 302 |
audiofileuploadbtn = gr.Button("Split", variant='primary')
|
| 303 |
audiofileuploadbtn.click(
|
| 304 |
split_wav_or_mp3_file,
|
| 305 |
-
[audiofileuploader, mindur2, maxdur2, name_for_split_files2],
|
| 306 |
[gr.Text(label="Output"), gr.File(label="Zipped files")]
|
| 307 |
)
|
| 308 |
-
|
| 309 |
-
gr.Markdown("This is very experimental and may break or change in the future. This essentially combines both the first 2 tabs into an all-in-one script.")
|
| 310 |
-
with gr.Row():
|
| 311 |
-
with gr.Column():
|
| 312 |
-
with gr.Row():
|
| 313 |
-
ytvideo = gr.Textbox(label="URL")
|
| 314 |
-
download_yt_video_as = gr.Radio(["wav", "mp3"], value="wav", label="Audio output format")
|
| 315 |
-
min_duration = gr.Number(label="Min duration", minimum=1, maximum=10, value=1)
|
| 316 |
-
max_duration = gr.Number(label="Max duration", minimum=1, maximum=10, value=5)
|
| 317 |
-
name_for_outputted_split_files = gr.Textbox(label="Name for split files")
|
| 318 |
-
download_and_split_btn = gr.Button("Download and split", variant='primary')
|
| 319 |
-
download_and_split_btn.click(
|
| 320 |
-
all_in_one_inator,
|
| 321 |
-
[ytvideo, download_yt_video_as, min_duration, max_duration, name_for_outputted_split_files],
|
| 322 |
-
[gr.Text(label="Result"), gr.File(label="Zipped files")]
|
| 323 |
-
)
|
| 324 |
with gr.Tab("Audio only download"):
|
| 325 |
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.")
|
| 326 |
with gr.Row():
|
|
@@ -334,36 +271,30 @@ with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") a
|
|
| 334 |
[yt_video, audio_output_format],
|
| 335 |
[gr.Text(label="Output"), gr.File(label="Zipped audio file")]
|
| 336 |
)
|
| 337 |
-
with gr.Tab("
|
| 338 |
-
gr.Markdown("
|
| 339 |
with gr.Row():
|
| 340 |
with gr.Column():
|
| 341 |
with gr.Row():
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
| 348 |
)
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
analyzebtn.click(
|
| 356 |
-
display_audio_spectrogram,
|
| 357 |
-
filetoanalyze,
|
| 358 |
-
gr.Image(label="Spectrogram result", show_download_button=False, scale=2)
|
| 359 |
-
)
|
| 360 |
with gr.TabItem("Changelog"):
|
| 361 |
-
gr.Markdown("v0.
|
|
|
|
|
|
|
| 362 |
gr.Markdown("v0.98.2 - Added new upcoming features tab.")
|
| 363 |
-
|
| 364 |
-
gr.HTML(
|
| 365 |
-
"""
|
| 366 |
-
<a href="https://imgbb.com/"><img src="https://i.ibb.co/4Vt9K3c/image-2024-01-26-052541742.png" border="0"></a><br />
|
| 367 |
-
"""
|
| 368 |
-
)
|
| 369 |
app.launch()
|
|
|
|
| 1 |
+
import gradio as gr, glob, os, auditok, zipfile, wave, pytube.exceptions, urllib.error
|
| 2 |
from pytube import YouTube
|
| 3 |
+
from moviepy.editor import *
|
| 4 |
+
import traceback, yt_dlp
|
| 5 |
|
| 6 |
def download_video(url, download_as, use_ytdlp):
|
| 7 |
if use_ytdlp == True:
|
|
|
|
| 102 |
os.remove(file2)
|
| 103 |
return f"Files split successfully!\nCheck below for zipped files.\n\n{total_files} files created, {length_mins:.2f} minutes total.", zip_file_name
|
| 104 |
|
| 105 |
+
def split_wav_or_mp3_file(audiofileuploader, mindur2, maxdur2, name_for_split_files2, strict):
|
| 106 |
if audiofileuploader == None:
|
| 107 |
raise gr.Error("Audio file cannot be empty!")
|
| 108 |
if mindur2 == maxdur2:
|
|
|
|
| 118 |
min_dur=mindur2,
|
| 119 |
max_dur=maxdur2,
|
| 120 |
max_silence=0.3,
|
| 121 |
+
energy_threshold=45,
|
| 122 |
+
strict_min_dur=True if strict == True else False
|
| 123 |
)
|
| 124 |
os.remove(audio_path)
|
| 125 |
for i, r in enumerate(audio_regions):
|
|
|
|
| 134 |
os.remove(file2)
|
| 135 |
return f"File split successfully!\nCheck below for zipped files.\nAmount created: {len(audio_files)}", zip_file_name2
|
| 136 |
|
| 137 |
+
def download_video_as_audio_only(yt_video, audio_output_format):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
try:
|
| 139 |
yt = YouTube(yt_video)
|
| 140 |
except pytube.exceptions.RegexMatchError:
|
|
|
|
| 169 |
os.remove(outputmp3removal)
|
| 170 |
return f"Done! Download the zip file below! This only contains the audio file.\n\nYou have downloaded {yt.title} by {yt.author}.", single_zip_name
|
| 171 |
|
| 172 |
+
def mp4_to_wav_or_mp3(mp4fileuploader, file_format):
|
| 173 |
+
if mp4fileuploader == None:
|
| 174 |
+
raise gr.Error("Input cannot be empty!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
else:
|
| 176 |
+
try:
|
| 177 |
+
if file_format == "wav":
|
| 178 |
+
videoinput = AudioFileClip(mp4fileuploader)
|
| 179 |
+
videoinput.write_audiofile("convertedaudio.wav")
|
| 180 |
+
videoinput.close()
|
| 181 |
+
elif file_format == "mp3":
|
| 182 |
+
videoinput = AudioFileClip(mp4fileuploader)
|
| 183 |
+
videoinput.write_audiofile("convertedaudio.mp3")
|
| 184 |
+
videoinput.close()
|
| 185 |
+
what_to_return = "convertedaudio.wav" if file_format == "wav" else "convertedaudio.mp3"
|
| 186 |
+
os.remove(mp4fileuploader)
|
| 187 |
+
return "Converted mp4 file successfully!", what_to_return
|
| 188 |
+
except:
|
| 189 |
+
raise gr.Error(traceback.format_exc())
|
| 190 |
+
|
| 191 |
+
def remove_audio_file_from_directory():
|
| 192 |
+
for r in glob.glob("*.wav"):
|
| 193 |
+
os.remove(r)
|
| 194 |
+
for w in glob.glob("*.mp3"):
|
| 195 |
+
os.remove(w)
|
| 196 |
+
return gr.Info("File removed.")
|
| 197 |
+
|
| 198 |
|
| 199 |
with gr.Blocks(theme='sudeepshouche/minimalist', title="Global Dataset Maker") as app:
|
| 200 |
gr.HTML(
|
|
|
|
| 212 |
gr.HTML(
|
| 213 |
"<h2> This Space's storage is ephemeral, meaning once you reload this space, all audio files will be lost. </h2>"
|
| 214 |
)
|
|
|
|
|
|
|
|
|
|
| 215 |
with gr.Tabs():
|
| 216 |
with gr.TabItem("Download Video"):
|
| 217 |
with gr.Row():
|
|
|
|
| 223 |
convertion = gr.Button("Download", variant='primary')
|
| 224 |
convertion.click(
|
| 225 |
fn=download_video,
|
| 226 |
+
inputs=[url, download_as, use_ytdlp],
|
| 227 |
outputs=gr.Text(label="Output")
|
| 228 |
)
|
| 229 |
with gr.TabItem("Split audio files"):
|
|
|
|
| 250 |
mindur2 = gr.Number(label="Min duration", minimum=1, maximum=10, value=1)
|
| 251 |
maxdur2 = gr.Number(label="Max duration", minimum=1, maximum=10, value=5)
|
| 252 |
name_for_split_files2 = gr.Textbox(label="Name for split files")
|
| 253 |
+
strict = gr.Checkbox(True, label="Enable strict duration?", info="Use this option if you want to minimize the '(audio_file) is less than 0.76 seconds' warning on Colab. Keep in mind that this only applies for min duration, max is ignored.")
|
| 254 |
audiofileuploadbtn = gr.Button("Split", variant='primary')
|
| 255 |
audiofileuploadbtn.click(
|
| 256 |
split_wav_or_mp3_file,
|
| 257 |
+
[audiofileuploader, mindur2, maxdur2, name_for_split_files2, strict],
|
| 258 |
[gr.Text(label="Output"), gr.File(label="Zipped files")]
|
| 259 |
)
|
| 260 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
with gr.Tab("Audio only download"):
|
| 262 |
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.")
|
| 263 |
with gr.Row():
|
|
|
|
| 271 |
[yt_video, audio_output_format],
|
| 272 |
[gr.Text(label="Output"), gr.File(label="Zipped audio file")]
|
| 273 |
)
|
| 274 |
+
with gr.Tab("MP4 to mp3/wav converter"):
|
| 275 |
+
gr.Markdown("If you have an mp4 file, you can convert it to mp3 or wav here. Only click the 'Remove file' button when done.")
|
| 276 |
with gr.Row():
|
| 277 |
with gr.Column():
|
| 278 |
with gr.Row():
|
| 279 |
+
mp4fileuploader = gr.File(file_count='single', file_types=[".mp4"], label="mp4 file")
|
| 280 |
+
file_format = gr.Radio(["wav", "mp3"], value="mp3", label="Convert mp4 file to:")
|
| 281 |
+
convert_btn = gr.Button("Convert", variant='primary')
|
| 282 |
+
remove_file_btn = gr.Button("Remove file from directory", variant='secondary')
|
| 283 |
+
convert_btn.click(
|
| 284 |
+
mp4_to_wav_or_mp3,
|
| 285 |
+
[mp4fileuploader, file_format],
|
| 286 |
+
[gr.Text(label="Output"), gr.File(label="Converted audio file")]
|
| 287 |
)
|
| 288 |
+
remove_file_btn.click(
|
| 289 |
+
remove_audio_file_from_directory,
|
| 290 |
+
None,
|
| 291 |
+
None
|
| 292 |
+
)
|
| 293 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
with gr.TabItem("Changelog"):
|
| 295 |
+
gr.Markdown("v0.99.2 - Added an mp4 file converter.")
|
| 296 |
+
gr.Markdown("v0.99.1 - Removed very old tools (including the 'Upcoming Features' tab) that did not fit with the nature of the program.")
|
| 297 |
+
gr.Markdown("v0.99 - Added 'Strict Duration' mode for the file splitter.")
|
| 298 |
gr.Markdown("v0.98.2 - Added new upcoming features tab.")
|
| 299 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
app.launch()
|