#!/usr/bin/env python3 #Youtubero - simpatico script che dato un link di youtube scarica i file rinominandoli a modino. #esempi presi da: #https://www.programcreek.com/python/example/98358/youtube_dl.YoutubeDL #https://github.com/ytdl-org/youtube-dl/blob/master/README.md from __future__ import unicode_literals import youtube_dl import shutil import sys import re import os import validators import glob import json def download_video(url, user = ""): print ('--- Inizio ---') if not os.path.exists("media"): os.makedirs("media") if not os.path.exists("temp"): os.makedirs("temp") ydl_opts = { 'format': 'bestaudio[ext=m4a]', 'outtmpl': 'temp/%(id)s.m4a', 'noplaylist': True, } url = url.strip() print (url) print (user) if not validators.url(url): print ('--- URL malformato ---') return ("Errore: url non valido") with youtube_dl.YoutubeDL(ydl_opts) as ydl: try: meta = ydl.extract_info(url, download = False) except youtube_dl.DownloadError as detail: print ('--- Errore video non disponibile ---') print(str(detail)) return (str(detail)) id = meta.get('id').strip() title = normalizetext(meta.get('title')) alt_title = normalizetext(meta.get('alt_title')) creator = normalizetext(meta.get('creator')) track = normalizetext(meta.get('track')) artist = normalizetext(meta.get('artist')) album = normalizetext(meta.get('album')) print ('id : %s' %(id)) print ('title : %s' %(title)) print ('artist : %s' %(artist)) print ('track : %s' %(track)) #scrivo il json with open(os.path.join("temp", id + ".json"), 'w') as outfile: json.dump(meta, outfile, indent=4) #ho letto le info, ora controllo se il file esiste altrimenti lo scarico #miglioria: controllare se upload_date e' uguale a quella del json gia' esistente filetemp = os.path.join("temp", id + ".m4a") if not glob.glob(filetemp): print ('--- Scarico ---') ydl.download([url]) #non ho capito perche' ma senza [] fa un carattere per volta #se il file esiste gia' in Media salto (potrebbe esserci, anche rinominato) if glob.glob(os.path.join("media/" + user, '*[[]' + id + '[]]*')): print ('--- Skippo ---') return ("Errore: %s [%s] giĆ  presente" %(title, id)) print ('--- Converto ---') #qui compone il nome del file if not artist is None: fileout = artist + " - " + track + " [" + id + "].mp3" id3title = track else: fileout = "_" + title + " [" + id + "].mp3" id3title = title print (fileout) if not os.path.exists("media/" + user): os.makedirs("media/" + user) fileout = os.path.join("media/" + user, fileout) if os.path.isfile(filetemp): #copia il file nella cartella media #implementare conversione in mp3 e metadata #shutil.copy(filetemp, fileout) cmd = "ffmpeg -i %s -y -vn -ar 44100 -ac 2 -b:a 192k -acodec libmp3lame \ -metadata title=\"%s\" -metadata artist=\"%s\" -metadata album=\"%s\" \ \"%s\"" \ %(filetemp, id3title, artist, album, fileout) os.system(cmd) print ('--- Fine ---') print ("") return ("Scaricato %s [%s]" %(title, id)) def normalizetext(s): if s is None: return None else: s = re.sub(r'[\\|/|:|*|?|"|<|>|\|]',r'',s) s = " ".join(s.split()) return s if __name__ == '__main__': url = sys.argv[1] download_video(url)