Maximo Fernandez commited on
Commit
23816e9
1 Parent(s): 73839c5

Download from youtube with pytube

Browse files
Files changed (1) hide show
  1. download.py +22 -6
download.py CHANGED
@@ -66,14 +66,30 @@ def download_twitch(url, type):
66
  os.system(f'rm {AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{args.format}')
67
 
68
  def download_youtube_video(url):
69
- user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
70
- command = f"yt-dlp -o '{VIDEO_FOLDER}/{DOWNLOAD_VIDEO_NAME}.{DOWNLOAD_VIDEO_FORMAT}' -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]' --user-agent '{user_agent}' --no-check-certificates --force-ipv4 --geo-bypass '{url}'"
71
- os.system(command)
 
 
 
 
 
 
 
 
72
 
73
  def download_youtube_audio(url):
74
- user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
75
- command = f"yt-dlp '{url}' -o '{AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{DOWNLOAD_AUDIO_FORMAT}' --user-agent '{user_agent}' --no-check-certificates --force-ipv4 --geo-bypass --extract-audio --audio-format mp3 --audio-quality 0"
76
- os.system(command)
 
 
 
 
 
 
 
 
77
 
78
  def download_youtube(url, type):
79
  if type == DOWNLOAD_VIDEO:
 
66
  os.system(f'rm {AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{args.format}')
67
 
68
  def download_youtube_video(url):
69
+ try:
70
+ yt = YouTube(url)
71
+ # Get stream with best resolution
72
+ video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
73
+ if video:
74
+ # Download video
75
+ video.download(output_path=VIDEO_FOLDER, filename=f'{DOWNLOAD_VIDEO_NAME}.{DOWNLOAD_VIDEO_FORMAT}')
76
+ else:
77
+ print('No se encontr贸 un stream de video compatible')
78
+ except Exception as e:
79
+ print(f'Error descargando el video: {str(e)}')
80
 
81
  def download_youtube_audio(url):
82
+ try:
83
+ yt = YouTube(url)
84
+ # Get stream with best quality
85
+ audio = yt.streams.filter(only_audio=True).order_by('abr').desc().first()
86
+ if audio:
87
+ # Download audio
88
+ audio.download(output_path=AUDIO_FOLDER, filename=f'{DOWNLOAD_AUDIO_NAME}.{DOWNLOAD_AUDIO_FORMAT}')
89
+ else:
90
+ print('No se encontr贸 un stream de audio compatible')
91
+ except Exception as e:
92
+ print(f'Error descargando el audio: {str(e)}')
93
 
94
  def download_youtube(url, type):
95
  if type == DOWNLOAD_VIDEO: