app.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import logging
  2. import yt_dlp
  3. from flask import Flask, request
  4. app = Flask(__name__)
  5. @app.route("/extract", methods=["GET", "POST"])
  6. def extract():
  7. url = request.json.get("url")
  8. logging.info(f"url: ${url}")
  9. with yt_dlp.YoutubeDL({"flat-playlist": True, "extract_flat": "flat-playlist"}) as ydl:
  10. info = ydl.extract_info(url, download=False)
  11. thumbnails = []
  12. for item in info.get("thumbnails", []):
  13. if item.get("width"):
  14. thumbnails.append({
  15. "url": item.get("url", ""),
  16. "width": item.get("width", 0),
  17. "height": item.get("height", 0),
  18. })
  19. formats = []
  20. for item in info.get("formats", []):
  21. if item.get("resolution") != "audio only" and item.get("url") and item.get("acodec") and item.get(
  22. "acodec") != "none" and item.get("vcodec"):
  23. formats.append({
  24. "width": item.get("width", 0),
  25. "height": item.get("height", 0),
  26. "type": item.get("format", ""),
  27. "quality": f'{item.get("format_note", "")}',
  28. "itag": 0,
  29. "fps": 0,
  30. "bitrate": 0,
  31. "url": item.get("url", ""),
  32. "ext": item.get("ext"),
  33. "vcodec": item.get("vcodec", ""),
  34. "acodec": item.get("acodec", ""),
  35. "vbr": 0,
  36. "abr": 0,
  37. "container": item.get("container")
  38. })
  39. return {
  40. "code": 200,
  41. "msg": "",
  42. "data": {
  43. "videoDetails": {
  44. "isLiveContent": info.get("is_live", False),
  45. "title": info.get("title", ""),
  46. "thumbnails": thumbnails,
  47. "description": info.get("description", ""),
  48. "lengthSeconds": int(info.get("duration", 0) / 100),
  49. "viewCount": info.get("view_count", 0),
  50. "keywords": [],
  51. "author": info.get("uploader", info.get("channel", "")),
  52. "channelID": info.get("channel_id", ""),
  53. "recommendInfo": [],
  54. "channelURL": info.get("channel_url", ""),
  55. "videoId": info.get("display_id", "")
  56. },
  57. "streamingData": {
  58. "formats": formats
  59. }
  60. }
  61. }
  62. if __name__ == '__main__':
  63. app.run(host='0.0.0.0', port=80, debug=True)