mzltest commited on
Commit
9ac2f9f
·
1 Parent(s): fba627e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -75,47 +75,47 @@ def download_audio(url):
75
  data = np.asarray(data * 32767, dtype=np.int16)
76
  return sr, data
77
 
78
- def downloadTubeUpload(searchQuery):
79
- # 设置yt-dlp的选项,只搜索小于10分钟的视频
80
- ydl_opts = {
81
-
82
- 'format': 'bestaudio',
83
- 'match_filter': lambda x: x["duration"] < 600,
84
- 'postprocessors': [{
85
- 'key': 'FFmpegExtractAudio',
86
- 'preferredcodec': 'mp3',
87
- 'preferredquality': '192',
88
- }],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  }
90
- ydl_opts['overwrites'] = True
91
- ydl_opts['verbose'] = True
92
- ydl_opts['outtmpl'] = '/tmp/%(title)s.%(ext)s'
93
- ydl_opts['postprocessors'][0]['preferredcodec'] = 'best'
94
- ydl_opts['postprocessors'][0]['preferredquality'] = 'best'
95
- ydl_opts['format'] = 'bestaudio/best'
96
- # 使用yt-dlp搜索并选择第一个符合条件的视频
97
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
98
- info_dict = ydl.extract_info("ytsearch1:{}".format(searchQuery), download=False)['entries'][0]
99
-
100
- video_url = info_dict['webpage_url']
101
- print('=====>'+video_url)
102
- # 下载视频的最佳音频
103
-
104
- # ydl = yt_dlp.YoutubeDL(ydl_opts)
105
- ydl.download([video_url])
106
-
107
- # 上传文件到lalal.ai
108
- api_url = 'https://lalal.ai/api/upload/'
109
- headers = {'Content-Disposition': 'attachment; filename={}.mp3'.format(info_dict['title'])}
110
- data = {'file': open('/tmp/{}.mp3'.format(info_dict['title']), 'rb')}
111
- r = requests.post(api_url, headers=headers, files=data)
112
-
113
- # 删除文件
114
- os.remove('/tmp/{}.mp3'.format(info_dict['title']))
115
-
116
- # 返回上传成功与否的信息
117
- return r.json()['id']
118
-
119
  def split_file(file_id):
120
  url_for_split = "https://www.lalal.ai/api/preview/"
121
 
 
75
  data = np.asarray(data * 32767, dtype=np.int16)
76
  return sr, data
77
 
78
+ def downloadTubeUpload(query):
79
+ # Step 1: Search for videos with the given query
80
+ search_url = f"https://draw-8fj-staging.begin.app/api/search/{urlencode(query)}"
81
+ search_response = requests.get(search_url).json()
82
+
83
+ # Step 2: Find the first video with duration less than 10 minutes and extract its ID
84
+ video_id = None
85
+ for item in search_response:
86
+ duration = item.get("duration")
87
+ if duration and duration < "00:10:00":
88
+ video_id = item.get("id", {}).get("videoId")
89
+ break
90
+
91
+ # If no video with duration less than 10 minutes was found, return None
92
+ if not video_id:
93
+ return None
94
+
95
+ # Step 3: Get the formats for the video and find the URL for the best audio-only format
96
+ formats_url = f"https://draw-8fj-staging.begin.app/api/info/{video_id}"
97
+ formats_response = requests.get(formats_url).json()
98
+
99
+ best_audio_format = None
100
+ for fmt in formats_response.get("formats", []):
101
+ if fmt.get("hasVideo") is False and fmt.get("hasAudio") is True and fmt.get("container") == "mp4":
102
+ if not best_audio_format or fmt.get("audioBitrate") > best_audio_format.get("audioBitrate"):
103
+ best_audio_format = fmt
104
+
105
+ # If no suitable audio format was found, return None
106
+ if not best_audio_format:
107
+ return None
108
+
109
+ # Step 4: Upload the video file to lalal.ai and return the ID of the uploaded file
110
+ upload_url = "https://lalal.ai/api/upload/"
111
+ headers = {
112
+ "Content-Disposition": f"attachment; filename={video_id}.mp4"
113
  }
114
+ with requests.get(best_audio_format["url"], stream=True) as r:
115
+ r.raise_for_status()
116
+ upload_response = requests.post(upload_url, headers=headers, data=r.raw).json()
117
+ return upload_response.get("result", {}).get("id")
118
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  def split_file(file_id):
120
  url_for_split = "https://www.lalal.ai/api/preview/"
121