前言
我可愛的女朋友上了很多微課,每次整理都費(fèi)心費(fèi)時(shí)立膛,所以決定用itchat
寫一個(gè)腳本程序掌动,能夠自動(dòng)的保存特定群聊中特定發(fā)言人的語音德澈,并使用pydub
將保存的語音拼接成音頻課程挣棕。
使用步驟:
- 在電腦上運(yùn)行程序:打開命令行译隘,切換到
autoRecord.py
所在文件夾,然后使用python autoRecord.py
- 掃描二維碼登錄洛心;【根據(jù)提示信息固耘,有時(shí)只用確認(rèn)登錄即可甚至什么都不用做】
- 輸入課程相關(guān)信息,按照命令行提示輸入:1. 課程名稱皂甘,用以創(chuàng)建文件夾保存語音文件玻驻;2. 群名稱;3. 發(fā)言人名稱偿枕;首先輸入發(fā)言人數(shù)量璧瞬,然后依次輸入名稱;
- 命令行提示
Start auto replying.
時(shí)渐夸,就進(jìn)入自動(dòng)模式了嗤锉。只需要在課程結(jié)束不需要軟件運(yùn)行時(shí),在手機(jī)端的群里面輸入結(jié)束暗號(hào)#END#墓塌,電腦程序在拼接音頻完成之后瘟忱,即可在課程文件夾下找到拼接結(jié)果文件result.mp3
。 - 如有問題苫幢,歡迎來售后访诱。
工作邏輯
通過群名和發(fā)言人名找到對(duì)應(yīng)的UserName
,然后根據(jù)UserName
判斷語音來源,然后根據(jù)類型執(zhí)行相應(yīng)的操作韩肝。在接收到由本微信號(hào)在群里面發(fā)出的結(jié)束暗號(hào)時(shí)触菜,對(duì)語音進(jìn)行拼接,然后退出程序【目前退出的型式不夠優(yōu)雅】哀峻。
代碼
import itchat
import os
from pydub import AudioSegment
@itchat.msg_register([itchat.content.RECORDING, itchat.content.TEXT], isGroupChat=True)
def download(msg):
global count, GID, UID, SelfID, FolderName
if msg['FromUserName'] == GID and msg['ActualUserName'] in UID and msg['Type']=='Recording':
print("Got Audio:#{} From {}".format(count, msg['ActualNickName']))
msg['Text']('{}/{}.mp3'.format(FolderName,count))
count += 1
if msg['FromUserName'] == SelfID and msg['ToUserName'] in GID and msg['Type']=='Text' and msg['Text'].startswith('#END#'):
print("Concat Audio.....")
AudioConcat(count, FolderName)
print("Exit! Bye Bye!")
itchat.logout()
exit(0)
def AudioConcat(count, FolderName):
if not count:
return
result = AudioSegment.from_mp3('{}/0.mp3'.format(FolderName))
for i in range(1, count):
result += AudioSegment.from_mp3('{}/{}.mp3'.format(FolderName, i))
result.export('{}/{}.mp3'.format(FolderName, 'result'), format='mp3')
def GotInfo():
print('*'*20)
while True:
FolderName = input("Please input Course Name:")
if os.path.exists(FolderName):
print("Folder Exists! Try another Name!")
else:
os.mkdir(FolderName)
break
print('*'*20)
itchat.get_chatrooms(update=True)
while True:
GName = input('Please Enter Group Name:')
G = itchat.search_chatrooms(name=GName)
if G:
break
GID = G[0]['UserName']
print('*'*20)
G = itchat.update_chatroom(userName=GID, detailedMember=True)
MList = G['MemberList']
UID = []
Unum = int(input("Please input the num of users to be listened:"))
print("Input UserName one by one!")
while len(UID)< Unum:
UName = input('Please input user name:')
aUID = 0
for m in MList:
# print(m['NickName'], m['RemarkName'])
if UName in [m['NickName'], m['RemarkName']]:
aUID = m['UserName']
if aUID:
UID.append(aUID)
print('Added Successfuly:{}'.format(UName))
else:
print('Added Failed:{}| Please Check and reinput!'.format(UName))
print('Listen to @{}@{}'.format(GName, UName))
SelfID = itchat.get_friends()[0]['UserName']
return GID, UID, FolderName, SelfID
if __name__ == '__main__':
itchat.auto_login(hotReload=True)
count = 0
GID, UID, FolderName, SelfID = GotInfo()
itchat.run()