rewrite with python3
This commit is contained in:
+19
-24
@@ -1,16 +1,13 @@
|
||||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# File: dump-audio.py
|
||||
# Author: Yuxin Wu
|
||||
|
||||
import sys
|
||||
import base64
|
||||
import argparse
|
||||
|
||||
from common.textutil import ensure_unicode
|
||||
from wechat.parser import WeChatDBParser
|
||||
from wechat.msg import TYPE_SPEAK
|
||||
from wechat.res import Resource
|
||||
from wechat.render import HTMLRender
|
||||
from wechat.libchathelper import LibChatHelper
|
||||
|
||||
def get_args():
|
||||
parser = argparse.ArgumentParser()
|
||||
@@ -23,27 +20,25 @@ def get_args():
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = get_args()
|
||||
|
||||
name = ensure_unicode(args.name)
|
||||
output_file = args.output
|
||||
|
||||
parser = WeChatDBParser(args.db)
|
||||
res = Resource(parser, args.res, '')
|
||||
|
||||
if name and name in parser.msgs_by_chat:
|
||||
msgs = parser.msgs_by_chat[name]
|
||||
else:
|
||||
sys.stderr.write(u"Valid Contacts: {}\n".format(u'\n'.join(parser.msgs_by_chat.keys())))
|
||||
sys.stderr.write(u"Couldn't find that contact {}.".format(name));
|
||||
try:
|
||||
chatid = parser.get_id_by_nickname(args.name)
|
||||
except KeyError:
|
||||
sys.stderr.write(u"Valid Contacts: {}\n".format('\n'.join(parser.all_chat_nicknames)))
|
||||
sys.stderr.write(u"Couldn't find the chat {}.".format(args.name));
|
||||
sys.exit(1)
|
||||
print "Number of Messages: ", len(msgs)
|
||||
|
||||
msgs = parser.msgs_by_chat[chatid]
|
||||
print(f"Number of Messages for {args.name}: ", len(msgs))
|
||||
assert len(msgs) > 0
|
||||
|
||||
libchat = LibChatHelper(parser, res)
|
||||
msgs = libchat.convert_msgs(msgs)
|
||||
voices = [m.sound for m in msgs if m.sound]
|
||||
for idx, v in enumerate(voices):
|
||||
p = v.find(':')
|
||||
v = v[p:]
|
||||
with open('/{}/{:04d}.mp3'.format(args.output, idx), 'wb') as f:
|
||||
f.write(v)
|
||||
voice_msgs = [m for m in msgs if m.type == TYPE_SPEAK]
|
||||
for idx, m in enumerate(voice_msgs):
|
||||
audio_str, duration = res.get_voice_mp3(m.imgPath)
|
||||
audio_bytes = base64.b64decode(audio_str)
|
||||
outf = f'/{args.output}/{idx:04d}-{duration:.1f}s.mp3'
|
||||
with open(outf, 'wb') as f:
|
||||
f.write(audio_bytes)
|
||||
print(f"Audio written to {outf}")
|
||||
|
||||
Reference in New Issue
Block a user