Youtube to MP3 - Apollo15 by ac0

ljuskaArchive
//archivePosts: 37Member, Moderator in Perl / Python / Shell scripting

Da skine video koristi curl, a da ga convertuje u MP3 koristi ffmpeg.

Primjer korištenja:

python ime.py http://www.youtube.com/watch?v=EEYatm2UkwM


import urllib,re,commands,subprocess,os,sys 
print "###################" 
print " TRY NOT TO HAVE AMPERSAND (&) IN THE ARGUMENT " 
print "###################" 
""" 
This is youtube-to-mp3 script. It uses curl to download video 
from youtube and ffmpeg to extract audio and convert it to 
mp3 format. 

Reason why I created this script: I used some web services to 
do this job for me. Then they got a lot of banners and they weren't 
always offline. Then I got borred and I made this :). 

Use it, change it, make it better, make it free :D. 
Open source ftw. 

crazy4magic at gmail.com 
""" 
####CONSTANTS###### 
save_path = "/Users/viz/Desktop" 
################### 



class youtube: 
   url="" 
   flv_location="" 
   mp3_location="" 
   name = "" 
   content ="" 
   url_to_download="" 

   def load(self,url): 
      if self.is_valid(url): 
         self.url = url 
         if self.get_content(): 
            if self.get_title(): 
               if self.url_to_dl(): 
                  self.download() 
                  self.convert() 
                  print "Mp3 file saved to " + self.mp3_location 
               else: 
                  pass 
            else: 
               pass 
         else: 
            pass 
      else: 
         pass 
   def is_valid(self,url): 
      if re.search(r"http:\/\/(?:www\.)youtube.com/watch\S*v=\w+",url): 
         return True ##nije bas perfektan ali radi 
      else: 
         print url + " ne izgleda kao youtube link" 
         return False 

   def get_title(self): 
      if self.content=="":return False 
      try: 
         self.name = re.search("<title>\s+(.+)\n",self.content).group(1) 
         self.flv_location = save_path + "/" + self.name + ".flv" 
         self.mp3_location = save_path + "/" + self.name + ".mp3" 
         print "Ubrao sam title" 
         return True 
      except: 
         print "Nisam ubrao title" 
         return False 
   def get_content(self): 
      try: 
         urlf = urllib.urlopen(self.url) 
         self.content = urlf.read() 
         print "Imam content" 
         return True 
      except: 
         print "Nemam content" 
         return False 
   def url_to_dl(self): 
      try: 
         urls1 = re.search("id=\\\"movie_player\\\".+?flashvars=\\\"(.+?)\\\"",self.content, re.M | re.DOTALL) 
         urls1 = urls1.group(1) 
         urls1 = urllib.unquote(urllib.unquote(urllib.unquote(urls1))) 
         urls1 = re.split("http",urls1) 
         urls = [] 
         for url in urls1: 
            if re.search("(?=.*videoplayback)(?=.*medium)",url): 
               url = url.split("fallback_host")[0] 
               temp = "http"+url 
               urls.append(temp) 
               #medium quality 
         self.url_to_download = urls[0]; 
         print "Imam urlove za dl" 
         return True 
      except: 
         print "Nemam url-ove" 
         return False 
   def download(self): 
      try: 
         print '\033[92m' + "Downloading " + self.name + '\033[0m' 
         print "curl \""+self.url_to_download+"\" -# -o \""+self.flv_location+"\"" 
         proc =  os.popen("curl \""+self.url_to_download+"\" -# -o \""+self.flv_location+"\"") 
         r = proc.readline(); 
         print r 
      except: 
         self.__exception() 
         pass 
   def convert(self): 
      print '\033[92m' + "Converting to mp3" + '\033[0m' 
      #Ovo si po zelji promijenite :) 
      proc =  os.popen("ffmpeg -i \""+self.flv_location+"\" -acodec libmp3lame -ab 128k \""+self.mp3_location+"\"") 
      r = proc.readline(); 
      self.remove() 
   def remove(self): 
      os.popen("rm \""+self.flv_location+"\"") 

   def __exception(self): 
      print '\033[93m' + "Something got wrong :(" + '\033[0m' 
      self.remove() 

one = 1 
youtube = youtube() 
for arg in sys.argv: 
   if one==1: 
      one=1337 ## I just had to ^^ 
      continue 
   youtube.load(arg) 

print '\033[92m' + "Done :)" + '\033[0m'
Sign In or Register to comment.

Welcome

It looks like you're new here. If you want to get involved, click one of these buttons!

Discussions

In this Discussion