[pyDownloader] Descargar videos de youtube y convertirlos a otro formato

Iniciado por $francisco, Junio 06, 2015, 10:52:47 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Junio 06, 2015, 10:52:47 PM Ultima modificación: Junio 07, 2015, 12:34:39 PM por Expermicid
Que tal a todos, terminé unos scripts para poder descargar vídeos de youtube y convertirlos a otro formato, y respecto a las otras librerias la unica diferencia es que quizas necesite mejorar bastante pero te muestra el porcentaje de descarga, se puede convertir a otro formato y también te muestra el porcentaje de conversión, espero que les guste.

primero tenemos que tener una caperta llamada "pyDownloader" que contendrá los siguientes archivos.

1- __init__.py

Código: python
from main import *


2- No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Código: python
#by hackloper
#skype: hackloper
#youtube: https://www.youtube.com/user/hackdeveloper
#gmail: [email protected]
#twitter: https://twitter.com/hackloper | @hackloper

import urllib2,urlparse,urllib,threading,time,os,re,pyffmpeg

class ErrorDownload(Exception):
def __init__(self,error):
self._error = error

def __str__(self):
return repr(self._error)

class pyDownloader(object):
def __init__(self,url,convert_to=False,automatic_print=False):
self._id = urlparse.parse_qs(urlparse.urlparse(url).query)['v'][0]
self._path = "https://www.youtube.com/get_video_info?video_id="
self._content = self.get_content()
try:
self._streams = self.get_streams()
except KeyError:
raise ErrorDownload("Invalid url.")
self.convert_to = convert_to
self.automatic_print = automatic_print
self._end_download = False

def print_write_size(self,size,name,extension):
d = os.getcwd()
error = 0
total_name = str("%s\\%s.%s" %(d,name,extension))
time.sleep(3)
while True:
try:
while int(os.path.getsize(total_name)) != int(size):
print str(int(os.path.getsize(total_name))*100/int(size))+"%"
time.sleep(0.3)
print "100%"
break
except:
if error >= 6:break
error += 1
print "error"
time.sleep(1)

def get_size_write(self):
time.sleep(1)
d = os.getcwd()

try:
porcent = str(int(os.path.getsize(self.total_name))*100/int(self.size))+"%"
return porcent
except WindowsError:
return "waiting"

def get_info(self):
return self._content

def get_size_convert(self):
if self.convert_to == False:
return False
else:
if self._end_download == False:
return "waiting"
else:
progress = pyffmpeg.progress()
rest = progress.get_size_convert()
return rest

def get_content(self):
response = urllib2.urlopen(self._path+self._id)
parse_content = urlparse.parse_qs(response.read())
return parse_content

def get_streams(self):
streams = self._content['url_encoded_fmt_stream_map'][0]
streams = streams.split(',')
streams = [urlparse.parse_qs(elem) for elem in streams]
return streams

def select_url_stream(self,type_file):
for i in self._streams:
t = re.search(r'/([\w?-]+)',i['type'][0])
if t.group(1) == type_file:
return i['url'][0]

def download_manual(self,url_stream,type_f,to,obj):
urllib.urlretrieve(url_stream,'%s.%s' %(obj.title,type_f))
obj._end_download = True
if obj.convert_to == True:
ffmpeg = pyffmpeg.ffmpeg(obj.title,obj.total_name,to)
ffmpeg.convert()


def download(self,type_f='mp4',to='mp3'):
self.title = self._content['title'][0]
site = urllib.urlopen(self.select_url_stream(type_f))
self.size =  site.info().getheaders('Content-Length')[0]
self.extension = type_f
d = os.getcwd()
self.total_name = str("%s\\%s.%s" %(d,self.title,type_f))
if int(self.size) == 0:
raise ErrorDownload("This video can not be downloaded by copyright")
else:
if self.automatic_print == True:
th = threading.Thread(target=self.print_write_size,args=(self.size,self.title,type_f))
th.start()
urllib.urlretrieve(self.select_url_stream(type_f),'%s.%s' %(self.title,type_f))
if self.convert_to == True:
ffmpeg = pyffmpeg.ffmpeg(self.title,self.total_name,to)
ffmpeg.convert()
else:
url_stream = self.select_url_stream(type_f)
thread = threading.Thread(target=self.download_manual,\
args=(url_stream,type_f,to,self))
thread.start()


Dentro de esta carpeta tendrá que habe rotra llamada pyffmpeg que contiene los siguientes archivos.

3- __init__.py

Código: python
from main import *


4- No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
Código: python

#by hackloper
#skype: hackloper
#youtube: https://www.youtube.com/user/hackdeveloper
#gmail: [email protected]
#twitter: https://twitter.com/hackloper | @hackloper
#blog: http://hackloper.blogspot.com.es/

import threading,os,re,time

class progress(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def get_seconds(self,times):
list_time = [360,60,0]
seconds = 0
split_time = times.split(":")
for i in range(3):
seconds += int(split_time[i])*list_time[i]

return seconds

def get_total_time(self):
times = None

f = self.open_file()
for line in f.readlines():
if "Duration" in line:
times = re.search(r"Duration: ([\w?:]+)",line).group(1)

f.close()
return int(self.get_seconds(times))


def get_actual_time(self,line):
times = re.findall(r"time=([\w?:]+)",line)
return self.get_seconds(times[-1])

def open_file(self):
while True:
if os.path.exists("log.txt"):
f = open("log.txt","r")
return f
else:
time.sleep(1)
continue

def get_size_convert(self):
total_time = self.get_total_time()

f = self.open_file()
last_line = f.readlines()[-1]
if "time=" in last_line:
actual_time = self.get_actual_time(last_line)
f.close()
porcentaje = (100*actual_time)/total_time
return str(porcentaje)+"%"
time.sleep(1)
elif "muxing overhead" in last_line:
return "100%"
else:
return "waiting"

def run(self):
total_time = self.get_total_time()

while True:
f = self.open_file()
last_line = f.readlines()[-1]
if "time=" in last_line:
actual_time = self.get_actual_time(last_line)
f.close()
porcentaje = (100*actual_time)/total_time
print str(porcentaje)+"%"
time.sleep(1)
elif "muxing overhead":
print "100%"
break
else:
break

class ffmpeg(object):

def __init__(self,name,total_dir,fmt_out='mp3',automatic_print=False):
self._fmt_out = fmt_out
self._name = name
self.total_dir = total_dir
self.automatic_print = automatic_print

def convert(self):
if os.path.exists("log.txt"): os.remove("log.txt")
if self.automatic_print == True:
print "entra automatico"
p = progress()
p.daemon = True
p.start()
command = 'pyffmpeg\\ffmpeg.exe -i "%s" -y -f %s "%s.%s" 2> log.txt' %(self.total_dir,self._fmt_out,self._name,self._fmt_out)
os.system(command)


5- Dentro de esta carpeta tendrá que estar el archivo ffmpeg.exe que se puede descargar de su página oficial.
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Metodo de uso

Código: python
import pyDownloader
#"convert_to" y "automatic_print" por defecto es False
obj = pyDownloader.pyDownloader("url_completa_de_youtube",convert_to=True,automatic_print=False)
#"type_f" es el formate de descarga que por defecto es "mp4" y "to" es el formato al que se va a convertir
#que por defecto es "mp3"
obj.download(type_f="ogg",to="flv")

def downloading():
while True:
info_download = obj.get_size_write()
if info_download == "waiting":
time.sleep(1)
elif info_download == "100%":
print info_download
break
else:
print info_download
time.sleep(1)

def convert():
while True:
info_convert = obj.get_size_convert()
if info_convert == False:
break
elif info_convert == "waiting":
time.sleep(1)
elif info_convert == "100%":
print info_convert
break
else:
print info_convert
time.sleep(1)

downloading()
convert()

Bueno, pues lo dicho, espero que les guste y que les sea útil.

¡ Excelente trabajo $francisco !
Una vez quise hacerlo, pero no avancé nada. Me distraigo fácilmente xD :v

Saludos!


Gracias, cambiare el post y pondré mas información de como funciona todo.