mirror of
https://github.com/Nezumi-2711/json-server-vercel.git
synced 2026-09-22 13:38:40 +00:00
Update main.py
This commit is contained in:
+14
-27
@@ -1,7 +1,6 @@
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
from pytube import YouTube
|
from pytube import YouTube
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
import youtube_dl
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app, origins='*')
|
CORS(app, origins='*')
|
||||||
@@ -18,36 +17,24 @@ def convert():
|
|||||||
video_url = f'https://www.youtube.com/watch?v={youtubeId}'
|
video_url = f'https://www.youtube.com/watch?v={youtubeId}'
|
||||||
yt = YouTube(video_url)
|
yt = YouTube(video_url)
|
||||||
|
|
||||||
options = {
|
# Get the audio stream URL
|
||||||
'format': 'bestaudio/best',
|
audio_stream_url = yt.streams.filter(only_audio=True).first().url
|
||||||
'extractaudio': True, # only keep the audio
|
|
||||||
'audioformat': 'mp3', # convert to mp3
|
|
||||||
'outtmpl': '%(id)s', # save file as video ID
|
|
||||||
'noplaylist': True, # only download single song, not playlist
|
|
||||||
}
|
|
||||||
|
|
||||||
with youtube_dl.YoutubeDL(options) as ydl:
|
# Get all audio stream URLs
|
||||||
info_dict = ydl.extract_info(video_url, download=False)
|
all_audio_stream_urls = [stream.url for stream in yt.streams.filter(only_audio=True)]
|
||||||
audio_url_youtubedl = info_dict['formats'][0]['url']
|
|
||||||
|
|
||||||
#only audio
|
# Get video stream URL
|
||||||
audio_url_pytube = yt.streams.get_audio_only().url
|
video_stream_url = yt.stream.filter(file_extension='mp4').first().url
|
||||||
# Get captions
|
|
||||||
captions = yt.captions['en'] if 'en' in yt.captions else None
|
|
||||||
captions_srt = captions.generate_srt_captions() if captions else "No captions available"
|
|
||||||
captions_vtt = captions.generate_vtt_captions() if captions else "No captions available"
|
|
||||||
|
|
||||||
# Return the audio stream URL and captions
|
# Return the audio stream URL and captions
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"status": True,
|
"status": True,
|
||||||
"audio_url_youtubedl": audio_url_youtubedl,
|
"audio_stream_url": audio_stream_url,
|
||||||
"audio_url_pytube": audio_url_pytube,
|
"all_audio_stream_urls": all_audio_stream_urls,
|
||||||
"captions_srt": captions_srt,
|
"video_stream_url": video_stream_url
|
||||||
"captions_vtt": captions_vtt
|
})
|
||||||
})
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"status": False, "error": str(e)})
|
return jsonify({"status": False, "error": str(e)})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host="0.0.0.0", port=8888)
|
app.run(host="0.0.0.0", port=8888)
|
||||||
|
|||||||
Reference in New Issue
Block a user