fffiloni commited on
Commit
d18130c
1 Parent(s): 0b625fd

added youtube song import

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import os
3
  hf_token = os.environ.get('HF_TOKEN')
4
 
@@ -36,6 +37,67 @@ compel = Compel(
36
 
37
  from pydub import AudioSegment
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def cut_audio(input_path, output_path, max_duration):
40
  audio = AudioSegment.from_file(input_path)
41
 
@@ -274,6 +336,9 @@ with gr.Blocks(css=css) as demo:
274
  </div>""")
275
 
276
  audio_input = gr.Audio(label="Music input", type="filepath", source="upload")
 
 
 
277
 
278
  with gr.Row():
279
  has_lyrics = gr.Radio(label="Does your audio has lyrics ?", choices=["Yes", "No"], value="No", info="If yes, the image should reflect the lyrics, but be aware that because we add a step (getting lyrics), inference will take more time.")
@@ -333,6 +398,7 @@ with gr.Blocks(css=css) as demo:
333
  """)
334
 
335
  #infer_btn.click(fn=infer, inputs=[audio_input], outputs=[lpmc_cap, llama_trans_cap, img_result])
 
336
  infer_btn.click(fn=infer, inputs=[audio_input, has_lyrics], outputs=[processed_audio, img_result, llama_trans_cap, tryagain_btn, share_group])
337
  share_button.click(None, [], [], _js=share_js)
338
  tryagain_btn.click(fn=solo_xd, inputs=[llama_trans_cap], outputs=[img_result])
 
1
  import gradio as gr
2
+ import tempfile
3
  import os
4
  hf_token = os.environ.get('HF_TOKEN')
5
 
 
37
 
38
  from pydub import AudioSegment
39
 
40
+ import yt_dlp as youtube_dl
41
+ from moviepy.editor import VideoFileClip
42
+
43
+ YT_LENGTH_LIMIT_S = 480 # limit to 1 hour YouTube files
44
+
45
+ def download_yt_audio(yt_url, filename):
46
+ info_loader = youtube_dl.YoutubeDL()
47
+
48
+ try:
49
+ info = info_loader.extract_info(yt_url, download=False)
50
+ except youtube_dl.utils.DownloadError as err:
51
+ raise gr.Error(str(err))
52
+
53
+ file_length = info["duration_string"]
54
+ file_h_m_s = file_length.split(":")
55
+ file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
56
+
57
+ if len(file_h_m_s) == 1:
58
+ file_h_m_s.insert(0, 0)
59
+ if len(file_h_m_s) == 2:
60
+ file_h_m_s.insert(0, 0)
61
+ file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
62
+
63
+ if file_length_s > YT_LENGTH_LIMIT_S:
64
+ yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
65
+ file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
66
+ raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
67
+
68
+ ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
69
+
70
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
71
+ try:
72
+ ydl.download([yt_url])
73
+ except youtube_dl.utils.ExtractorError as err:
74
+ raise gr.Error(str(err))
75
+
76
+
77
+ def convert_to_mp3(input_path, output_path):
78
+ try:
79
+ video_clip = VideoFileClip(input_path)
80
+ audio_clip = video_clip.audio
81
+ print("Converting to MP3...")
82
+ audio_clip.write_audiofile(output_path)
83
+ except Exception as e:
84
+ print("Error:", e)
85
+
86
+ def load_youtube_audio(yt_link):
87
+
88
+ gr.Info("Loading your YouTube link ... ")
89
+ with tempfile.TemporaryDirectory() as tmpdirname:
90
+ filepath = os.path.join(tmpdirname, "video.mp4")
91
+ download_yt_audio(yt_link, filepath)
92
+
93
+ mp3_output_path = "video_sound.mp3"
94
+ convert_to_mp3(filepath, mp3_output_path)
95
+ print("Conversion complete. MP3 saved at:", mp3_output_path)
96
+
97
+
98
+
99
+ return mp3_output_path
100
+
101
  def cut_audio(input_path, output_path, max_duration):
102
  audio = AudioSegment.from_file(input_path)
103
 
 
336
  </div>""")
337
 
338
  audio_input = gr.Audio(label="Music input", type="filepath", source="upload")
339
+ with gr.Row():
340
+ youtube_link = gr.Textbox(show_label=False, placeholder="you can also paste YT link and load it")
341
+ yt_load_btn = gr.Button("Load YT song")
342
 
343
  with gr.Row():
344
  has_lyrics = gr.Radio(label="Does your audio has lyrics ?", choices=["Yes", "No"], value="No", info="If yes, the image should reflect the lyrics, but be aware that because we add a step (getting lyrics), inference will take more time.")
 
398
  """)
399
 
400
  #infer_btn.click(fn=infer, inputs=[audio_input], outputs=[lpmc_cap, llama_trans_cap, img_result])
401
+ yt_load_btn.click(fn=load_youtube_audio, inputs=[youtube_link], outputs=[audio_input], queue=False)
402
  infer_btn.click(fn=infer, inputs=[audio_input, has_lyrics], outputs=[processed_audio, img_result, llama_trans_cap, tryagain_btn, share_group])
403
  share_button.click(None, [], [], _js=share_js)
404
  tryagain_btn.click(fn=solo_xd, inputs=[llama_trans_cap], outputs=[img_result])