yt_download.py 980 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import unicode_literals
  2. import youtube_dl
  3. ydl_opts = {
  4. 'format': 'bestaudio/best',
  5. 'postprocessors': [{
  6. 'key': 'FFmpegExtractAudio',
  7. 'preferredcodec': 'mp3',
  8. 'preferredquality': '128',
  9. }],
  10. }
  11. def get_info_yt_video(url: str):
  12. ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})
  13. with ydl:
  14. result = ydl.extract_info(url, download=False) # We just want to extract the info
  15. if 'entries' in result:
  16. # Can be a playlist or a list of videos
  17. info = result['entries'][0]
  18. else:
  19. # Just a video
  20. info = result
  21. return {
  22. "is_live": info["is_live"],
  23. "link": url,
  24. "id": info["id"],
  25. "title": info["title"]
  26. }
  27. def get_mp3_from_ut(video: dict):
  28. url = video["link"]
  29. with youtube_dl.YoutubeDL(ydl_opts) as ydl_mp3:
  30. ydl_mp3.download([url])
  31. return video
  32. # print(get_info_yt_video("https://yewtu.be/watch?v=t6xt6Qksklk"))