pierreguillou commited on
Commit
f52579e
1 Parent(s): f14a1c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -49
app.py CHANGED
@@ -1,57 +1,58 @@
1
  import gradio as gr
2
- import re, unidecode
3
- from unidecode import unidecode
4
  import yt_dlp
5
  import os
6
- import pydub
7
- import numpy as np
8
 
9
- # no space, punctuation, accent in lower string
10
  def cleanString(string):
11
- cleanString = unidecode(string)
12
- # cleanString = re.sub('\W+','_', cleanString)
13
- cleanString = re.sub(r'[^\w\s]','',cleanString)
14
- cleanString = cleanString.replace(" ", "_")
15
- return cleanString.lower()
16
 
17
- # from YouTube url to audio file path and sample rate + numpy array
18
  def download_audio(url):
19
-
20
- path_to_folder_audio_mp3 = "./"
21
- ydl_opts = {
22
- 'format': 'm4a/bestaudio/best',
23
- 'outtmpl': f'{path_to_folder_audio_mp3}%(title)s',
24
- 'postprocessors': [{
25
- 'key': 'FFmpegExtractAudio',
26
- 'preferredcodec': 'mp3',
27
- }]
28
- }
29
-
30
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
31
- info_dict = ydl.extract_info(url, download=True)
32
- video_title = info_dict['title']
33
-
34
- # Rename the audio file
35
- local_link = video_title + ".mp3"
36
- new_local_link = cleanString(video_title) + ".mp3"
37
- for filename in os.listdir(path_to_folder_audio_mp3):
38
- if cleanString(local_link) == cleanString(filename):
39
- os.rename(os.path.join(path_to_folder_audio_mp3, filename),os.path.join(path_to_folder_audio_mp3, new_local_link))
40
-
41
- # get audio file path
42
- file_path = path_to_folder_audio_mp3 + new_local_link
43
-
44
- return file_path, file_path
45
-
 
 
 
46
  with gr.Blocks() as demo:
47
- gr.Markdown("<h1><center>Free YouTube URL Video-to-Audio</center></h1>")
48
- gr.Markdown("<center>Enter the link of any YouTube video to generate its mp3 audio file.</center>")
49
-
50
- input_text_url = gr.Textbox(placeholder='Youtube video URL', label='YouTube URL')
51
- result_button_audio = gr.Button('Get Audio File')
52
- output_audio_file = gr.File(label='mp3 audio file')
53
- output_audio_play = gr.Audio(type="filepath", label="Listen to audio")
54
-
55
- result_button_audio.click(download_audio, inputs = input_text_url, outputs = [output_audio_file, output_audio_play])
56
-
57
- demo.queue().launch(debug = False)
 
 
1
  import gradio as gr
2
+ import re
3
+ import unidecode
4
  import yt_dlp
5
  import os
 
 
6
 
7
+ # Function to clean the string for file naming
8
  def cleanString(string):
9
+ clean_string = unidecode.unidecode(string)
10
+ clean_string = re.sub(r'[^\w\s]', '', clean_string)
11
+ clean_string = clean_string.replace(" ", "_")
12
+ return clean_string.lower()
 
13
 
14
+ # Function to download audio from YouTube
15
  def download_audio(url):
16
+ path_to_folder_audio_mp3 = "./"
17
+ ydl_opts = {
18
+ 'format': 'bestaudio/best',
19
+ 'outtmpl': f'{path_to_folder_audio_mp3}%(title)s.%(ext)s',
20
+ 'postprocessors': [{
21
+ 'key': 'FFmpegExtractAudio',
22
+ 'preferredcodec': 'mp3',
23
+ 'preferredquality': '192',
24
+ }],
25
+ 'noplaylist': True, # Avoid downloading playlists
26
+ }
27
+
28
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
29
+ info_dict = ydl.extract_info(url, download=True)
30
+ video_title = info_dict['title']
31
+
32
+ # Clean the title for the filename
33
+ new_local_link = cleanString(video_title) + ".mp3"
34
+ original_file_path = os.path.join(path_to_folder_audio_mp3, f"{video_title}.mp3")
35
+
36
+ # Rename the audio file if it exists
37
+ if os.path.exists(original_file_path):
38
+ os.rename(original_file_path, os.path.join(path_to_folder_audio_mp3, new_local_link))
39
+
40
+ # Get the final file path
41
+ file_path = os.path.join(path_to_folder_audio_mp3, new_local_link)
42
+
43
+ return file_path, file_path
44
+
45
+ # Gradio interface
46
  with gr.Blocks() as demo:
47
+ gr.Markdown("<h1><center>Free YouTube URL Video-to-Audio</center></h1>")
48
+ gr.Markdown("<center>Enter the link of any YouTube video to generate its mp3 audio file.</center>")
49
+
50
+ input_text_url = gr.Textbox(placeholder='YouTube video URL', label='YouTube URL')
51
+ result_button_audio = gr.Button('Get Audio File')
52
+ output_audio_file = gr.File(label='MP3 Audio File')
53
+ output_audio_play = gr.Audio(type="filepath", label="Listen to Audio")
54
+
55
+ result_button_audio.click(download_audio, inputs=input_text_url, outputs=[output_audio_file, output_audio_play])
56
+
57
+ # Launch the Gradio app
58
+ demo.queue().launch(debug=True)