Maximo Fernandez commited on
Commit
155317d
1 Parent(s): 5682990

Donwload youtube video with ydl

Browse files
Files changed (1) hide show
  1. download.py +17 -44
download.py CHANGED
@@ -1,4 +1,5 @@
1
  from pytube import YouTube
 
2
  import os
3
  import argparse
4
  # import twitchdl.commands as twitch_downloader
@@ -67,55 +68,27 @@ def download_twitch(url, type):
67
 
68
  def download_youtube_video(url):
69
  try:
70
- # Configurar el user agent
71
- yt = YouTube(url)
72
- yt.bypass_age_gate()
73
-
74
- # Configurar headers personalizados
75
- yt.stream_monostate.headers.update({
76
- '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',
77
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
78
- 'Accept-Language': 'en-us,en;q=0.5',
79
- 'Accept-Encoding': 'gzip,deflate',
80
- 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
81
- 'Keep-Alive': 'timeout=15, max=100',
82
- 'Connection': 'keep-alive',
83
- })
84
-
85
- # Obtener el stream con la mejor resoluci贸n en formato mp4
86
- video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
87
- if video:
88
- # Download video
89
- video.download(output_path=VIDEO_FOLDER, filename=f'{DOWNLOAD_VIDEO_NAME}.{DOWNLOAD_VIDEO_FORMAT}')
90
- else:
91
- print('No se encontr贸 un stream de video compatible')
92
  except Exception as e:
93
  print(f'Error descargando el video: {str(e)}')
94
 
95
  def download_youtube_audio(url):
96
  try:
97
- # Configurar el user agent
98
- yt = YouTube(url)
99
- yt.bypass_age_gate()
100
-
101
- # Configurar headers personalizados
102
- yt.stream_monostate.headers.update({
103
- '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',
104
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
105
- 'Accept-Language': 'en-us,en;q=0.5',
106
- 'Accept-Encoding': 'gzip,deflate',
107
- 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
108
- 'Keep-Alive': 'timeout=15, max=100',
109
- 'Connection': 'keep-alive',
110
- })
111
-
112
- # Obtener el stream de audio con la mejor calidad
113
- audio = yt.streams.filter(only_audio=True).order_by('abr').desc().first()
114
- if audio:
115
- # Download audio
116
- audio.download(output_path=AUDIO_FOLDER, filename=f'{DOWNLOAD_AUDIO_NAME}.{DOWNLOAD_AUDIO_FORMAT}')
117
- else:
118
- print('No se encontr贸 un stream de audio compatible')
119
  except Exception as e:
120
  print(f'Error descargando el audio: {str(e)}')
121
 
 
1
  from pytube import YouTube
2
+ import yt_dlp
3
  import os
4
  import argparse
5
  # import twitchdl.commands as twitch_downloader
 
68
 
69
  def download_youtube_video(url):
70
  try:
71
+ # Config options
72
+ ydl_options = {
73
+ "outtmpl": f"{VIDEO_FOLDER}/{DOWNLOAD_VIDEO_NAME}.{DOWNLOAD_VIDEO_FORMAT}",
74
+ "format": "best",
75
+ }
76
+ # Download video
77
+ with yt_dlp.YoutubeDL(ydl_options) as ydl:
78
+ ydl.download(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  except Exception as e:
80
  print(f'Error descargando el video: {str(e)}')
81
 
82
  def download_youtube_audio(url):
83
  try:
84
+ # Config options
85
+ ydl_options = {
86
+ "outtmpl": f"{AUDIO_FOLDER}/{DOWNLOAD_AUDIO_NAME}.{DOWNLOAD_AUDIO_FORMAT}",
87
+ "format": "bestaudio",
88
+ }
89
+ # Download audio
90
+ with yt_dlp.YoutubeDL(ydl_options) as ydl:
91
+ ydl.download(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  except Exception as e:
93
  print(f'Error descargando el audio: {str(e)}')
94