Kryptone commited on
Commit
7348eda
·
1 Parent(s): b311a81

add mp3 support for yt vid

Browse files

also fixed bug with wav files remaining when show_amount_of_files_and_file_dur was unchecked.

Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -2,7 +2,9 @@ import gradio as gr, glob, os, auditok, zipfile, wave, pytube.exceptions, libros
2
  from pytube import YouTube
3
  from moviepy.editor import VideoFileClip
4
 
5
- def download_video(url):
 
 
6
  try:
7
  yt = YouTube(url)
8
  except pytube.exceptions.RegexMatchError:
@@ -12,18 +14,24 @@ def download_video(url):
12
  video_path = f"{video.default_filename}"
13
  video_clip = VideoFileClip(video_path)
14
  audio_clip = video_clip.audio
15
- audio_clip.write_audiofile("output.wav")
 
 
 
16
  audio_clip.close()
17
  video_clip.close()
18
  for removalmp4 in glob.glob("*.mp4"):
19
  os.remove(removalmp4)
20
- return "Finished downloading! Please proceed to next tab."
 
21
 
22
  def split_audio_from_yt_video(mindur, maxdur, name_for_split_files, show_amount_of_files_and_file_dur):
23
  if show_amount_of_files_and_file_dur == True:
24
  gr.Warning(f"show_amount_of_files_and_file_dur set to True. This will take longer if your audio file is long.")
25
- if not os.path.exists("output.wav"):
26
- raise gr.Error("Output.wav does not exist! Did you do the first tab correctly or at all?")
 
 
27
  if mindur == maxdur:
28
  raise gr.Error(f"Cannot split mindur={mindur} and maxdur={maxdur}, min and max are the same number.")
29
  elif mindur > maxdur:
@@ -31,7 +39,7 @@ def split_audio_from_yt_video(mindur, maxdur, name_for_split_files, show_amount_
31
  elif name_for_split_files == None:
32
  raise gr.Error("Split files name cannot be empty! This will be replaced with an alternative naming style in the future.")
33
  else:
34
- audio_path = "output.wav"
35
  audio_regions = auditok.split(
36
  audio_path,
37
  min_dur=mindur,
@@ -49,6 +57,8 @@ def split_audio_from_yt_video(mindur, maxdur, name_for_split_files, show_amount_
49
  for audio_file in audio_files:
50
  zip_file.write(audio_file, os.path.basename(audio_file))
51
  if show_amount_of_files_and_file_dur == False:
 
 
52
  return "Files split successfully!\nCheck below for zipped files.", zip_file_name
53
  elif show_amount_of_files_and_file_dur == True:
54
  largest_file = ("", 0)
@@ -142,16 +152,18 @@ with gr.Blocks(theme='NoCrypt/miku', title="Global Dataset Maker") as app:
142
  gr.Markdown(
143
  "This Space will create a dataset for you, all automatically. **Please be warned that due to not having a GPU on this Space, some steps might take longer to complete.**"
144
  )
 
145
  with gr.Tabs():
146
  with gr.TabItem("Download Video"):
147
  with gr.Row():
148
  with gr.Column():
149
  with gr.Row():
150
  url = gr.Textbox(label="URL")
 
151
  convertion = gr.Button("Download", variant='primary')
152
  convertion.click(
153
  fn=download_video,
154
- inputs=[url],
155
  outputs=gr.Text(label="Output")
156
  )
157
  with gr.TabItem("Split audio files"):
@@ -212,5 +224,4 @@ with gr.Blocks(theme='NoCrypt/miku', title="Global Dataset Maker") as app:
212
  [upload],
213
  [gr.Text(label="Result")]
214
  )
215
-
216
  app.launch()
 
2
  from pytube import YouTube
3
  from moviepy.editor import VideoFileClip
4
 
5
+ def download_video(url, download_as):
6
+ if download_as == "mp3":
7
+ gr.Warning("MP3 support is experimental at the moment, but is in current testing.")
8
  try:
9
  yt = YouTube(url)
10
  except pytube.exceptions.RegexMatchError:
 
14
  video_path = f"{video.default_filename}"
15
  video_clip = VideoFileClip(video_path)
16
  audio_clip = video_clip.audio
17
+ if download_as == "wav":
18
+ audio_clip.write_audiofile("output.wav")
19
+ elif download_as == "mp3":
20
+ audio_clip.write_audiofile("output.mp3")
21
  audio_clip.close()
22
  video_clip.close()
23
  for removalmp4 in glob.glob("*.mp4"):
24
  os.remove(removalmp4)
25
+
26
+ #return "Finished downloading! Please proceed to next tab."
27
 
28
  def split_audio_from_yt_video(mindur, maxdur, name_for_split_files, show_amount_of_files_and_file_dur):
29
  if show_amount_of_files_and_file_dur == True:
30
  gr.Warning(f"show_amount_of_files_and_file_dur set to True. This will take longer if your audio file is long.")
31
+ if not os.path.exists("output.wav") and os.path.exists("output.mp3"):
32
+ gr.Warning("Output.wav does not exist, but output.mp3 does. Splitting MP3 file instead...")
33
+ if not os.path.exists("output.mp3") and os.path.exists("output.wav"):
34
+ gr.Warning("Output.mp3 does not exist, but output.wav does. Splitting WAV file instead...")
35
  if mindur == maxdur:
36
  raise gr.Error(f"Cannot split mindur={mindur} and maxdur={maxdur}, min and max are the same number.")
37
  elif mindur > maxdur:
 
39
  elif name_for_split_files == None:
40
  raise gr.Error("Split files name cannot be empty! This will be replaced with an alternative naming style in the future.")
41
  else:
42
+ audio_path = "output.wav" if not os.path.exists("output.mp3") else "output.mp3"
43
  audio_regions = auditok.split(
44
  audio_path,
45
  min_dur=mindur,
 
57
  for audio_file in audio_files:
58
  zip_file.write(audio_file, os.path.basename(audio_file))
59
  if show_amount_of_files_and_file_dur == False:
60
+ for file2 in glob.glob("*.wav"):
61
+ os.remove(file2)
62
  return "Files split successfully!\nCheck below for zipped files.", zip_file_name
63
  elif show_amount_of_files_and_file_dur == True:
64
  largest_file = ("", 0)
 
152
  gr.Markdown(
153
  "This Space will create a dataset for you, all automatically. **Please be warned that due to not having a GPU on this Space, some steps might take longer to complete.**"
154
  )
155
+ gr.HTML("<h1> MP3 file support does work for downloading a video only (for now).")
156
  with gr.Tabs():
157
  with gr.TabItem("Download Video"):
158
  with gr.Row():
159
  with gr.Column():
160
  with gr.Row():
161
  url = gr.Textbox(label="URL")
162
+ download_as = gr.Radio(["wav", "mp3"], label="Audio format output", value="wav", info="What should the audio format be output as?")
163
  convertion = gr.Button("Download", variant='primary')
164
  convertion.click(
165
  fn=download_video,
166
+ inputs=[url, download_as],
167
  outputs=gr.Text(label="Output")
168
  )
169
  with gr.TabItem("Split audio files"):
 
224
  [upload],
225
  [gr.Text(label="Result")]
226
  )
 
227
  app.launch()