Descargar vídeos de YouTube y otros sitios

Código fuente Python para la descarga de vídeos de YouTube y otros sitios soportados. Para esto es necesaria la librería externa youtube-dl





  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-


  3. import os
  4. import sys
  5. import youtube_dl
  6. from urllib2 import urlopen
  7. import StringIO
  8. import cPickle as pickle


  9. FILE_DESCARGAS "descargas.dat" #archivo donde se guardaran los links
  10. FORMAT_ASP = ["best""worst""bestaudio""worstaudio""bestvideo""worstvideo"]
  11. EXTENSIONS = ["mp4""flv""webm""3gp""m4a""mp3""ogg""aac""wav"]



  12. class VideoDownloader(object):
  13.     """
  14.     Clase para la descarga de videos.
  15.     """
  16.     

  17.     def Downloader(selfurlformat_asp=Nonecontinuedl=False):
  18.         """
  19.         Descarga el video de la url indicada, en el outpath indicado.
  20.         format_asp es opcional.
  21.         --format_asp = best, worst, bestaudio, worstaudio, bestvideo, worstvideo
  22.         --extension = mp4, flv, webm, 3gp, m4a, mp3, ogg, aac, wav
  23.         --continuedl = True|False (para continuar la descarga en donde la dejamos.
  24.         """
  25.         parameters = {"outtmpl""%(title)s_%(format_id)s.%(ext)s"}
  26.      
  27.         if format_asp:
  28.             parameters["format"] = format_asp
  29.     
  30.         if continuedl:
  31.             parameters["continuedl"] = True

  32.         ydl youtube_dl.YoutubeDLparameters )
  33.         ydl.extract_info(urlTrue)
  34.  
  35.  
  36.     def ContinueDownloader(selfurloutpathformat_asp=None):
  37.         """
  38.         Continua la descarga del video que no termino de descargarse.
  39.         """
  40.         return self.Downloader(urloutpathformat_aspTrue)
  41.      
  42.  
  43.     def ExtractInfo(selfurl):
  44.         """
  45.         Obtiene la informacion de la url. retorna info
  46.         """
  47.         ydl youtube_dl.YoutubeDL()
  48.         return ydl.extract_info(urlFalse)
  49.  
  50.  
  51.     def GetFormats(selfinfo):
  52.         """
  53.         Obtiene los distintos formatos disponibles del video 
  54.         con sus size. [ (format_id, format, size) ]
  55.         """
  56.         try:
  57.             lista info["formats"]
  58.         except KeyError:
  59.             return 
  60.                     (info.get("format_id"""), 
  61.                      info.get("format"""), 
  62.                      info.get("filesize"0), 
  63.                      info.get("ext""") )
  64.                      ]
  65.         formats = []
  66.         for dic in lista:
  67.             formats.append( (
  68.                              dic.get("format_id"""), 
  69.                              dic.get("format"""), 
  70.                              dic.get("filesize"0), 
  71.                              dic.get("ext"""
  72.                              ) )
  73.         return formats
  74.  
  75.  
  76.     def GetTitle(selfinfo):
  77.         """
  78.         Obtiene el titulo del video
  79.         """
  80.         return info.get("title""Sin_titulo")
  81.  
  82.  
  83.     def GetDescription(selfinfo):
  84.         """
  85.         Obtiene la descripcion del video
  86.         """
  87.         return info.get("description""")
  88.          
  89.          
  90.     def Thumbnails(selfinfo):
  91.         """Obtiene el link de la imagen que se muestra en el video.
  92.         """
  93.         return info.get("thumbnail"None)
  94.       

  95.     def WriteFile(selfdatafilename):
  96.         """Escribe en un archivo en disco.
  97.         """
  98.         file(filename"wb")
  99.         pickle.dump(dataf)
  100.         f.close()

  101.   
  102.     def ReadFile(selffilename):
  103.         """Lee un archivo del disco.
  104.         """
  105.         try:
  106.             file(filename"rb")
  107.             data pickle.load(f)
  108.             f.close()
  109.         except BaseException as e:
  110.             self.WriteFile({}, filename)
  111.             data = {}
  112.             print(e)
  113.         return data
  114.      
  115.      

  116. vd VideoDownloader()