dump-audio.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import base64
  5. import argparse
  6. from wechat.parser import WeChatDBParser
  7. from wechat.msg import TYPE_SPEAK
  8. from wechat.res import Resource
  9. def get_args():
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument('name', help='name of contact')
  12. parser.add_argument('--output', help='output mp3 dir', default='/tmp')
  13. parser.add_argument('--db', default='decrypted.db', help='path to decrypted database')
  14. parser.add_argument('--res', default='resource', help='reseource directory')
  15. args = parser.parse_args()
  16. return args
  17. if __name__ == '__main__':
  18. args = get_args()
  19. parser = WeChatDBParser(args.db)
  20. res = Resource(parser, args.res, '')
  21. try:
  22. chatid = parser.get_chat_id(args.name)
  23. except KeyError:
  24. sys.stderr.write(u"Valid Contacts: {}\n".format('\n'.join(parser.all_chat_nicknames)))
  25. sys.stderr.write(u"Couldn't find the chat {}.".format(args.name));
  26. sys.exit(1)
  27. msgs = parser.msgs_by_chat[chatid]
  28. print(f"Number of Messages for {args.name}: ", len(msgs))
  29. assert len(msgs) > 0
  30. voice_msgs = [m for m in msgs if m.type == TYPE_SPEAK]
  31. for idx, m in enumerate(voice_msgs):
  32. audio_str, duration = res.get_voice_mp3(m.imgPath)
  33. audio_bytes = base64.b64decode(audio_str)
  34. outf = f'/{args.output}/{idx:04d}-{duration:.1f}s.mp3'
  35. with open(outf, 'wb') as f:
  36. f.write(audio_bytes)
  37. print(f"Audio written to {outf}")