pengdaqian commited on
Commit
9f50b66
1 Parent(s): 134cf2b
Files changed (3) hide show
  1. app.py +21 -17
  2. music/search.py +76 -4
  3. requirements.txt +2 -1
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import os
 
2
 
3
  os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
4
 
5
- from music.search import get_random_spit, get_albums
6
  from vits.models import SynthesizerInfer
7
  import whisper.inference
8
  from omegaconf import OmegaConf
@@ -17,7 +18,6 @@ import random
17
  from spleeter.separator import Separator
18
  from spleeter.audio.adapter import AudioAdapter
19
  from pydub import AudioSegment
20
- import scipy.io.wavfile
21
  import uuid
22
 
23
  import logging
@@ -178,14 +178,6 @@ def svc_change(argswave, argsspk):
178
  return out_audio
179
 
180
 
181
- def np_to_audio_segment(fp_arr):
182
- wav_io = io.BytesIO()
183
- scipy.io.wavfile.write(wav_io, 16000, fp_arr)
184
- wav_io.seek(0)
185
- sound = AudioSegment.from_wav(wav_io)
186
- return sound
187
-
188
-
189
  def svc_main(sid, input_audio):
190
  if input_audio is None:
191
  return "You need to upload an audio", None
@@ -238,14 +230,26 @@ def auto_search(name):
238
  save_music_path = 'downloaded'
239
  if not os.path.exists(save_music_path):
240
  os.makedirs(save_music_path)
 
241
  config = {'logfilepath': 'musicdl.log', save_music_path: 'downloaded', 'search_size_per_source': 5, 'proxies': {}}
242
- albums = get_albums(keywords=name, config=config)
243
- if len(albums) == 0:
244
- return "No music found", None
245
- album = random.choice(albums)
246
- save_path = get_random_spit(album)
247
- fp = save_path
248
- signal, sampling_rate = soundfile.read(fp)
 
 
 
 
 
 
 
 
 
 
 
249
  return "Found a music", (sampling_rate, signal)
250
 
251
 
 
1
  import os
2
+ import threading
3
 
4
  os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
5
 
6
+ from music.search import get_random_spit, get_albums, search_youtube, download_youtube, get_youtube, download_random
7
  from vits.models import SynthesizerInfer
8
  import whisper.inference
9
  from omegaconf import OmegaConf
 
18
  from spleeter.separator import Separator
19
  from spleeter.audio.adapter import AudioAdapter
20
  from pydub import AudioSegment
 
21
  import uuid
22
 
23
  import logging
 
178
  return out_audio
179
 
180
 
 
 
 
 
 
 
 
 
181
  def svc_main(sid, input_audio):
182
  if input_audio is None:
183
  return "You need to upload an audio", None
 
230
  save_music_path = 'downloaded'
231
  if not os.path.exists(save_music_path):
232
  os.makedirs(save_music_path)
233
+
234
  config = {'logfilepath': 'musicdl.log', save_music_path: 'downloaded', 'search_size_per_source': 5, 'proxies': {}}
235
+ save_path = os.path.join(save_music_path, name + '.mp3')
236
+ # youtube
237
+ task1 = threading.Thread(
238
+ target=get_youtube,
239
+ args=(name, save_path)
240
+ )
241
+ task1.start()
242
+ task2 = threading.Thread(
243
+ target=download_random,
244
+ args=(name, config, save_path)
245
+ )
246
+ task2.start()
247
+ task1.join()
248
+ task2.join()
249
+
250
+ if not os.path.exists(save_path):
251
+ return "Not Found", None
252
+ signal, sampling_rate = soundfile.read(save_path)
253
  return "Found a music", (sampling_rate, signal)
254
 
255
 
music/search.py CHANGED
@@ -4,6 +4,11 @@ import random
4
  from musicdl import musicdl
5
  from musicdl.modules import Downloader
6
  from pydub import AudioSegment
 
 
 
 
 
7
 
8
  def is_integer(string):
9
  if string.isdigit():
@@ -46,6 +51,63 @@ def size_to_int(size_string):
46
  return int(size) # 转换为整数
47
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def get_albums(keywords, config):
50
  target_srcs = [
51
  'kugou', 'kuwo', 'qqmusic', 'qianqian', 'fivesing',
@@ -73,10 +135,9 @@ def get_albums(keywords, config):
73
  return valid_albums
74
 
75
 
76
- def get_random_spit(songinfo):
77
  d = Downloader(songinfo)
78
  d.start()
79
- save_path = os.path.join(songinfo["savedir"], f"{songinfo['savename']}.{songinfo['ext']}")
80
  song = AudioSegment.from_mp3(save_path)
81
  # pydub does things in milliseconds
82
  length = len(song)
@@ -91,6 +152,17 @@ def get_random_spit(songinfo):
91
  return save_path
92
 
93
 
 
 
 
 
 
 
 
 
94
  if __name__ == '__main__':
95
- config = {'logfilepath': 'musicdl.log', 'downloaded': 'downloaded', 'search_size_per_source': 5, 'proxies': {}}
96
- get_albums('tayler', config)
 
 
 
 
4
  from musicdl import musicdl
5
  from musicdl.modules import Downloader
6
  from pydub import AudioSegment
7
+ from yt_dlp import YoutubeDL
8
+ import yt_dlp
9
+ from yt_dlp.utils import download_range_func
10
+ import json
11
+
12
 
13
  def is_integer(string):
14
  if string.isdigit():
 
51
  return int(size) # 转换为整数
52
 
53
 
54
+ def search_youtube(keywords):
55
+ YDL_OPTIONS = {
56
+ 'format': 'bestaudio',
57
+ # 'noplaylist': 'True',
58
+ 'proxy': 'socks5://127.0.0.1:1089',
59
+ }
60
+ with YoutubeDL(YDL_OPTIONS) as ydl:
61
+ video = ydl.extract_info(f"ytsearch:{keywords}", download=False)['entries'][0:5]
62
+ # video = ydl.extract_info(keywords, download=False)
63
+ if len(video) > 0:
64
+ ret = random.choice(video)
65
+ return ydl.sanitize_info(ret)
66
+ else:
67
+ return None
68
+
69
+
70
+ def download_youtube(info, save_path):
71
+ url = info['original_url']
72
+ duration = info['duration']
73
+ if duration > 700:
74
+ start_second = 500
75
+ end_second = 530
76
+ else:
77
+ start_second = duration / 2 - 15
78
+ end_second = duration / 2 + 15
79
+ if start_second < 0:
80
+ start_second = 0
81
+ if end_second > duration:
82
+ end_second = duration
83
+ ydl_opts = {
84
+ 'format': 'm4a/bestaudio/best',
85
+ 'downloader': 'ffmpeg',
86
+ 'download_ranges': download_range_func(None, [(start_second, end_second)]),
87
+ # ℹ️ See help(yt_dlp.postprocessor) for a list of available Postprocessors and their arguments
88
+ 'postprocessors': [{ # Extract audio using ffmpeg
89
+ 'key': 'FFmpegExtractAudio',
90
+ 'preferredcodec': 'mp3',
91
+ }],
92
+ 'outtmpl': save_path,
93
+ 'proxy': 'http://127.0.0.1:8889',
94
+ }
95
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
96
+ info = ydl.extract_info(url, download=True)
97
+ # ℹ️ ydl.sanitize_info makes the info json-serializable
98
+ ret_info = ydl.sanitize_info(info)
99
+ ret_info['save_path'] = save_path
100
+ return ret_info
101
+
102
+
103
+ def get_youtube(keywords, save_path):
104
+ info = search_youtube(keywords)
105
+ if info is None:
106
+ return None
107
+ else:
108
+ return download_youtube(info, save_path)
109
+
110
+
111
  def get_albums(keywords, config):
112
  target_srcs = [
113
  'kugou', 'kuwo', 'qqmusic', 'qianqian', 'fivesing',
 
135
  return valid_albums
136
 
137
 
138
+ def get_random_spit(songinfo, save_path):
139
  d = Downloader(songinfo)
140
  d.start()
 
141
  song = AudioSegment.from_mp3(save_path)
142
  # pydub does things in milliseconds
143
  length = len(song)
 
152
  return save_path
153
 
154
 
155
+ def download_random(keywords, config, save_path):
156
+ albums = get_albums(keywords, config)
157
+ if len(albums) == 0:
158
+ return None
159
+ album = random.choice(albums)
160
+ get_random_spit(album, save_path=save_path)
161
+
162
+
163
  if __name__ == '__main__':
164
+ # config = {'logfilepath': 'musicdl.log', 'downloaded': 'downloaded', 'search_size_per_source': 5, 'proxies': {}}
165
+ # infos = get_albums('李荣浩', config)
166
+ # print(infos)
167
+ info = search_youtube('李荣浩')
168
+ download_youtube(info, "downloaded")
requirements.txt CHANGED
@@ -14,4 +14,5 @@ tqdm
14
  librosa
15
  pydub
16
  musicdl
17
- spleeter
 
 
14
  librosa
15
  pydub
16
  musicdl
17
+ spleeter
18
+ yt-dlp