python微信语音转发给别人_python 使用 itchat 保存群聊语音并拼接成音频课程
前言
我可愛的女朋友上了很多微課,每次整理都費心費時,所以決定用itchat寫一個腳本程序,能夠自動的保存特定群聊中特定發(fā)言人的語音,并使用pydub將保存的語音拼接成音頻課程。
使用步驟:
在電腦上運行程序:打開命令行,切換到autoRecord.py所在文件夾,然后使用python autoRecord.py
掃描二維碼登錄;【根據(jù)提示信息,有時只用確認登錄即可甚至什么都不用做】
輸入課程相關(guān)信息,按照命令行提示輸入:1. 課程名稱,用以創(chuàng)建文件夾保存語音文件;2. 群名稱;3. 發(fā)言人名稱;首先輸入發(fā)言人數(shù)量,然后依次輸入名稱;
命令行提示Start auto replying. 時,就進入自動模式了。只需要在課程結(jié)束不需要軟件運行時,在手機端的群里面輸入結(jié)束暗號#END#,電腦程序在拼接音頻完成之后,即可在課程文件夾下找到拼接結(jié)果文件result.mp3。
如有問題,歡迎來售后。
工作邏輯
通過群名和發(fā)言人名找到對應(yīng)的UserName,然后根據(jù)UserName判斷語音來源,然后根據(jù)類型執(zhí)行相應(yīng)的操作。在接收到由本微信號在群里面發(fā)出的結(jié)束暗號時,對語音進行拼接,然后退出程序【目前退出的型式不夠優(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()
總結(jié)
以上是生活随笔為你收集整理的python微信语音转发给别人_python 使用 itchat 保存群聊语音并拼接成音频课程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 下一代大数据系统和4S标准
- 下一篇: websocket python爬虫_p