PyRip - CD ripper
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

pyrip.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import argparse
  2. import config
  3. import logging
  4. import media
  5. import report
  6. import sys
  7. import os
  8. import fstools
  9. try:
  10. from config import APP_NAME as ROOT_LOGGER_NAME
  11. except ImportError:
  12. ROOT_LOGGER_NAME = 'root'
  13. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  14. def progress_callback_rip(p: float):
  15. bar_length = 40
  16. progress = int(bar_length * p)
  17. out = "\rRipping.... - [ "
  18. out += "*" * progress
  19. out += "-" * (bar_length - progress)
  20. out += " ] %5.1f%%" % (p * 100)
  21. sys.stdout.write(out)
  22. def progress_callback_enc(p: float):
  23. bar_length = 40
  24. progress = int(bar_length * p)
  25. out = "\rEncoding... - [ "
  26. out += "*" * progress
  27. out += "-" * (bar_length - progress)
  28. out += " ] %5.1f%%" % (p * 100)
  29. sys.stdout.write(out)
  30. def cddb_user_callback(what: int, info: dict):
  31. if what == media.CALLBACK_CDDB_CHOICE:
  32. keys = tuple(info.keys())
  33. print("Multiple cddb entries detected:")
  34. print("-------------------------------")
  35. for i in range(0, len(keys)):
  36. print("%2d. %s" % (i + 1, info[keys[i]]))
  37. n = None
  38. while not n:
  39. fb = input("Choose: ")
  40. try:
  41. fb = int(fb)
  42. except ValueError:
  43. n = None
  44. else:
  45. if fb > 0 and fb <= len(keys):
  46. n = fb
  47. if n is None:
  48. print("Use a choice from above!")
  49. print()
  50. return keys[n - 1]
  51. elif what == media.CALLBACK_MAN_INPUT:
  52. print("No cddb entries detected:")
  53. print("-------------------------")
  54. for key in info:
  55. info[key] = input(key[0].upper() + key[1:] + ": ")
  56. return info
  57. def main(**kwargs):
  58. disc_data = media.get_media_data(media.get_disc_device(), cddb_user_callback)
  59. if disc_data is None:
  60. logger.error("Could not reading disc_data")
  61. sys.exit(1)
  62. print("Found Disc:")
  63. print("===========")
  64. print()
  65. print(disc_data[media.common.KEY_ARTIST], "-", disc_data[media.common.KEY_ALBUM])
  66. print("-" * (len(disc_data[media.common.KEY_ARTIST]) + len(disc_data[media.common.KEY_ALBUM]) + 3))
  67. for track_info in disc_data["tracks"]:
  68. i = track_info[media.common.KEY_TRACK]
  69. print("%2d. %s" % (i, track_info[media.common.KEY_TITLE]))
  70. wavfile = media.track_to_targetpath(kwargs.get("basepath"), track_info, 'wav')
  71. try:
  72. fstools.mkdir(os.path.dirname(wavfile))
  73. except PermissionError:
  74. logger.exception("Unable to create ripping target path: %s", os.path.dirname(wavfile))
  75. sys.exit(1)
  76. rv = media.disc_track_rip(i, wavfile, progress_callback_rip)
  77. if rv == 0:
  78. print()
  79. rv = media.wav_to_mp3(wavfile, kwargs.get("basepath"), track_info, progress_callback_enc)
  80. print()
  81. os.remove(wavfile)
  82. if rv != 0:
  83. logger.error("Error while ripping or encoding...")
  84. if __name__ == "__main__":
  85. default_baspath = os.path.join(os.getenv("HOME"), "rip")
  86. parser = argparse.ArgumentParser(description='Description')
  87. parser.add_argument('-v', '--verbose', help='Description for foo argument', action='store_true')
  88. parser.add_argument('-b', '--basepath', help=f'The rip and encode basepath (default is {default_baspath})', default=default_baspath)
  89. args = vars(parser.parse_args())
  90. report.stdoutLoggingConfigure(log_name_lvl=((config.APP_NAME, logging.DEBUG if args.get('verbose') else logging.ERROR),))
  91. main(**args)