youtubero.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. #Youtubero - simpatico script che dato un link di youtube scarica i file rinominandoli a modino.
  3. #esempi presi da:
  4. #https://www.programcreek.com/python/example/98358/youtube_dl.YoutubeDL
  5. #https://github.com/ytdl-org/youtube-dl/blob/master/README.md
  6. from __future__ import unicode_literals
  7. import youtube_dl
  8. import shutil
  9. import sys
  10. import re
  11. import os
  12. import validators
  13. import glob
  14. import json
  15. def download_video(url, user = ""):
  16. print ('--- Inizio ---')
  17. if not os.path.exists("media"):
  18. os.makedirs("media")
  19. if not os.path.exists("temp"):
  20. os.makedirs("temp")
  21. ydl_opts = {
  22. 'format': 'bestaudio[ext=m4a]',
  23. 'outtmpl': 'temp/%(id)s.m4a',
  24. 'noplaylist': True,
  25. }
  26. url = url.strip()
  27. print (url)
  28. print (user)
  29. if not validators.url(url):
  30. print ('--- URL malformato ---')
  31. return ("Errore: url non valido")
  32. with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  33. try:
  34. meta = ydl.extract_info(url, download = False)
  35. except youtube_dl.DownloadError as detail:
  36. print ('--- Errore video non disponibile ---')
  37. print(str(detail))
  38. return (str(detail))
  39. id = meta.get('id').strip()
  40. title = normalizetext(meta.get('title'))
  41. alt_title = normalizetext(meta.get('alt_title'))
  42. creator = normalizetext(meta.get('creator'))
  43. track = normalizetext(meta.get('track'))
  44. artist = normalizetext(meta.get('artist'))
  45. album = normalizetext(meta.get('album'))
  46. print ('id : %s' %(id))
  47. print ('title : %s' %(title))
  48. print ('artist : %s' %(artist))
  49. print ('track : %s' %(track))
  50. #scrivo il json
  51. with open(os.path.join("temp", id + ".json"), 'w') as outfile:
  52. json.dump(meta, outfile, indent=4)
  53. #ho letto le info, ora controllo se il file esiste altrimenti lo scarico
  54. #miglioria: controllare se upload_date e' uguale a quella del json gia' esistente
  55. filetemp = os.path.join("temp", id + ".m4a")
  56. if not glob.glob(filetemp):
  57. print ('--- Scarico ---')
  58. ydl.download([url]) #non ho capito perche' ma senza [] fa un carattere per volta
  59. #se il file esiste gia' in Media salto (potrebbe esserci, anche rinominato)
  60. if glob.glob(os.path.join("media/" + user, '*[[]' + id + '[]]*')):
  61. print ('--- Skippo ---')
  62. return ("Errore: %s [%s] già presente" %(title, id))
  63. print ('--- Converto ---')
  64. #qui compone il nome del file
  65. if not artist is None:
  66. fileout = artist + " - " + track + " [" + id + "].mp3"
  67. id3title = track
  68. else:
  69. fileout = "_" + title + " [" + id + "].mp3"
  70. id3title = title
  71. print (fileout)
  72. if not os.path.exists("media/" + user):
  73. os.makedirs("media/" + user)
  74. fileout = os.path.join("media/" + user, fileout)
  75. if os.path.isfile(filetemp):
  76. #copia il file nella cartella media
  77. #implementare conversione in mp3 e metadata
  78. #shutil.copy(filetemp, fileout)
  79. cmd = "ffmpeg -i %s -y -vn -ar 44100 -ac 2 -b:a 192k -acodec libmp3lame \
  80. -metadata title=\"%s\" -metadata artist=\"%s\" -metadata album=\"%s\" \
  81. \"%s\"" \
  82. %(filetemp, id3title, artist, album, fileout)
  83. os.system(cmd)
  84. print ('--- Fine ---')
  85. print ("")
  86. return ("Scaricato %s [%s]" %(title, id))
  87. def normalizetext(s):
  88. if s is None:
  89. return None
  90. else:
  91. s = re.sub(r'[\\|/|:|*|?|"|<|>|\|]',r'',s)
  92. s = " ".join(s.split())
  93. return s
  94. if __name__ == '__main__':
  95. url = sys.argv[1]
  96. download_video(url)