pierreguillou commited on
Commit
4d0fc4b
1 Parent(s): ca28bde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -4
app.py CHANGED
@@ -1,22 +1,53 @@
1
  import gradio as gr
 
 
2
  import yt_dlp
 
 
 
 
 
 
 
3
 
4
  def download_audio(url):
 
5
  ydl_opts = {
 
6
  'format': 'm4a/bestaudio/best',
 
 
7
  'postprocessors': [{
8
  'key': 'FFmpegExtractAudio',
9
  'preferredcodec': 'mp3',
 
10
  }]
11
  }
12
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
13
  info_dict = ydl.extract_info(url, download=True)
14
- video_url = info_dict['url']
15
- return video_url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  iface = gr.Interface(fn=download_audio,
18
  inputs=gr.Textbox(label="YouTube Video URL"),
19
- outputs=gr.Audio(label="Output Audio", type="filepath", format="mp3"),
20
  allow_flagging="never"
21
  )
22
- iface.launch()
 
1
  import gradio as gr
2
+ import unidecode
3
+ from unidecode import unidecode
4
  import yt_dlp
5
+ import os
6
+
7
+ # no space, punctuation, accent in lower string
8
+ def cleanString(string):
9
+ cleanString = unidecode(string)
10
+ cleanString = re.sub('\W+','_', cleanString)
11
+ return cleanString.lower()
12
 
13
  def download_audio(url):
14
+ path_to_folder_audio_mp3 = "./audio/"
15
  ydl_opts = {
16
+ # 'format': 'bestaudio/best',
17
  'format': 'm4a/bestaudio/best',
18
+ # 'outtmpl': '%(id)s.%(ext)s',
19
+ 'outtmpl': f'{path_to_folder_audio_mp3}%(title)s',
20
  'postprocessors': [{
21
  'key': 'FFmpegExtractAudio',
22
  'preferredcodec': 'mp3',
23
+ # 'preferredquality': '192',
24
  }]
25
  }
26
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
27
  info_dict = ydl.extract_info(url, download=True)
28
+ video_title = info_dict['title']
29
+ # ydl.download([video_link])
30
+
31
+ # Rename the audio file
32
+ local_link = video_title + ".mp3"
33
+ new_local_link = cleanString(video_title) + ".mp3"
34
+ for filename in os.listdir(path_to_folder_audio_mp3):
35
+ if cleanString(local_link) == cleanString(filename):
36
+ os.rename(os.path.join(path_to_folder_audio_mp3, filename),os.path.join(path_to_folder_audio_mp3, new_local_link))
37
+
38
+ return path_to_folder_audio_mp3 + new_local_link
39
+
40
+ # def video_to_audio(url):
41
+ # video_url = download_video(url)
42
+ # video = mp.VideoFileClip(video_url)
43
+ # audio = video.audio
44
+ # audio_file = "output_audio.mp3"
45
+ # audio.write_audiofile(audio_file)
46
+ # return audio_file
47
 
48
  iface = gr.Interface(fn=download_audio,
49
  inputs=gr.Textbox(label="YouTube Video URL"),
50
+ outputs=gr.File(label="Output Audio"),
51
  allow_flagging="never"
52
  )
53
+ iface.launch(debug=True)