dl_voices.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import json, os, re, requests, time
  2. with open('test/all_voices.json', 'r', encoding='utf-8') as f:
  3. raw = f.read()
  4. m = re.search(r'"(\[.*\])"', raw, re.DOTALL)
  5. data = json.loads(m.group(1).replace(r'\"', '"').replace(r'\\\\', '\\'))
  6. print(f'共 {len(data)} 个音色')
  7. os.makedirs('voices', exist_ok=True)
  8. dl = skip = fail = 0
  9. for i, v in enumerate(data):
  10. url, voice = v['url'], v['voice']
  11. if not url or not voice:
  12. skip += 1; continue
  13. fpath = f'voices/{voice}.mp3'
  14. if os.path.exists(fpath):
  15. skip += 1; continue
  16. try:
  17. r = requests.get(url, timeout=30)
  18. if r.status_code == 200 and len(r.content) > 100:
  19. open(fpath, 'wb').write(r.content); dl += 1
  20. else: fail += 1
  21. except: fail += 1
  22. if (i+1) % 30 == 0:
  23. print(f' 进度: {i+1}/{len(data)} (下载{dl} 跳过{skip} 失败{fail})')
  24. time.sleep(0.15)
  25. print(f'\n完成! 下载{dl} 跳过{skip} 失败{fail}')
  26. # 列表
  27. files = sorted(os.listdir('voices'))
  28. print(f'voices/ 共 {len(files)} 个文件:')
  29. for f in files[:20]:
  30. print(f' {f}')
  31. if len(files) > 20:
  32. print(f' ... 等 {len(files)} 个')