Hev832 commited on
Commit
9255174
1 Parent(s): 7008bd8
Files changed (1) hide show
  1. run.py +18 -6
run.py CHANGED
@@ -1,7 +1,9 @@
1
  import gradio as gr
2
  import yt_dlp
3
  import os
4
- os.system("pip yt_dlp -U")
 
 
5
  def download_media(url, media_type):
6
  ydl_opts = {
7
  'format': 'bestaudio/best' if media_type == 'audio' else 'bestvideo+bestaudio',
@@ -22,23 +24,33 @@ def download_media(url, media_type):
22
 
23
  return file_path
24
 
25
- def show_media(url, media_type):
 
 
 
 
 
 
 
26
  file_path = download_media(url, media_type)
27
  if media_type == 'audio':
28
- return None, file_path
 
29
  else:
30
  return file_path, None
31
 
32
  with gr.Blocks() as demo:
33
- gr.Markdown(f"<h1><center> Media Downloader")
34
- gr.Markdown("please `❤` this spaces 🤗 if helpfull")
35
  url = gr.Textbox(label="YouTube URL")
36
  media_type = gr.Radio(label="Media Type", choices=["audio", "video"])
 
 
37
  download_button = gr.Button("Download")
38
 
39
  video_output = gr.Video()
40
  audio_output = gr.Audio()
41
 
42
- download_button.click(show_media, inputs=[url, media_type], outputs=[video_output, audio_output])
43
 
44
  demo.launch()
 
1
  import gradio as gr
2
  import yt_dlp
3
  import os
4
+ from pydub import AudioSegment
5
+ os.system("pip install yt_dlp pydub")
6
+
7
  def download_media(url, media_type):
8
  ydl_opts = {
9
  'format': 'bestaudio/best' if media_type == 'audio' else 'bestvideo+bestaudio',
 
24
 
25
  return file_path
26
 
27
+ def split_audio(file_path, start_time, end_time):
28
+ audio = AudioSegment.from_file(file_path)
29
+ split_audio = audio[start_time*1000:end_time*1000]
30
+ split_file_path = file_path.rsplit('.', 1)[0] + f'_{start_time}-{end_time}.mp3'
31
+ split_audio.export(split_file_path, format="mp3")
32
+ return split_file_path
33
+
34
+ def show_media(url, media_type, start_time, end_time):
35
  file_path = download_media(url, media_type)
36
  if media_type == 'audio':
37
+ split_file_path = split_audio(file_path, start_time, end_time)
38
+ return None, split_file_path
39
  else:
40
  return file_path, None
41
 
42
  with gr.Blocks() as demo:
43
+ gr.Markdown("<h1><center>Media Downloader</center></h1>")
44
+ gr.Markdown("please `❤` this space 🤗 if helpful")
45
  url = gr.Textbox(label="YouTube URL")
46
  media_type = gr.Radio(label="Media Type", choices=["audio", "video"])
47
+ start_time = gr.Number(label="Start Time (seconds)", value=0)
48
+ end_time = gr.Number(label="End Time (seconds)", value=10)
49
  download_button = gr.Button("Download")
50
 
51
  video_output = gr.Video()
52
  audio_output = gr.Audio()
53
 
54
+ download_button.click(show_media, inputs=[url, media_type, start_time, end_time], outputs=[video_output, audio_output])
55
 
56
  demo.launch()