2023-12-03 12:19:24 +08:00
|
|
|
import os.path
|
2023-12-13 21:23:43 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-12-12 01:17:21 +08:00
|
|
|
from os import system
|
2023-12-03 12:19:24 +08:00
|
|
|
import sqlite3
|
|
|
|
import threading
|
2023-12-12 01:17:21 +08:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from pilk import decode
|
2023-12-03 12:19:24 +08:00
|
|
|
|
|
|
|
lock = threading.Lock()
|
2023-12-12 01:17:21 +08:00
|
|
|
db_path = "./app/Database/Msg/MediaMSG.db"
|
2023-12-03 12:19:24 +08:00
|
|
|
|
2023-12-13 21:23:43 +08:00
|
|
|
def get_ffmpeg_path():
|
|
|
|
# 获取打包后的资源目录
|
|
|
|
resource_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
|
2023-12-03 12:19:24 +08:00
|
|
|
|
2023-12-13 21:23:43 +08:00
|
|
|
# 构建 FFmpeg 可执行文件的路径
|
|
|
|
ffmpeg_path = os.path.join(resource_dir, 'app', 'resources', 'ffmpeg.exe')
|
|
|
|
|
|
|
|
return ffmpeg_path
|
2023-12-03 12:19:24 +08:00
|
|
|
def singleton(cls):
|
|
|
|
_instance = {}
|
|
|
|
|
|
|
|
def inner():
|
|
|
|
if cls not in _instance:
|
|
|
|
_instance[cls] = cls()
|
|
|
|
return _instance[cls]
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
@singleton
|
|
|
|
class MediaMsg:
|
|
|
|
def __init__(self):
|
|
|
|
self.DB = None
|
|
|
|
self.cursor: sqlite3.Cursor = None
|
|
|
|
self.open_flag = False
|
|
|
|
self.init_database()
|
|
|
|
|
|
|
|
def init_database(self):
|
|
|
|
if not self.open_flag:
|
|
|
|
if os.path.exists(db_path):
|
|
|
|
self.DB = sqlite3.connect(db_path, check_same_thread=False)
|
|
|
|
# '''创建游标'''
|
|
|
|
self.cursor = self.DB.cursor()
|
|
|
|
self.open_flag = True
|
|
|
|
if lock.locked():
|
|
|
|
lock.release()
|
|
|
|
|
|
|
|
def get_media_buffer(self, reserved0):
|
|
|
|
sql = '''
|
|
|
|
select Buf
|
|
|
|
from Media
|
|
|
|
where Reserved0 = ?
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
lock.acquire(True)
|
|
|
|
self.cursor.execute(sql, [reserved0])
|
2023-12-12 01:17:21 +08:00
|
|
|
return self.cursor.fetchone()[0]
|
2023-12-03 12:19:24 +08:00
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
|
2023-12-12 01:17:21 +08:00
|
|
|
def get_audio(self, reserved0, output_path):
|
|
|
|
buf = self.get_media_buffer(reserved0)
|
|
|
|
silk_path = f"{output_path}\\{reserved0}.silk"
|
|
|
|
pcm_path = f"{output_path}\\{reserved0}.pcm"
|
|
|
|
mp3_path = f"{output_path}\\{reserved0}.mp3"
|
2023-12-13 20:45:53 +08:00
|
|
|
silk_path = silk_path.replace("/", "\\")
|
2023-12-12 01:17:21 +08:00
|
|
|
pcm_path = pcm_path.replace("/", "\\")
|
|
|
|
mp3_path = mp3_path.replace("/", "\\")
|
|
|
|
if os.path.exists(mp3_path):
|
|
|
|
return mp3_path
|
2023-12-13 20:45:53 +08:00
|
|
|
with open(silk_path, "wb") as f:
|
|
|
|
f.write(buf)
|
|
|
|
# open(silk_path, "wb").write()
|
2023-12-12 01:17:21 +08:00
|
|
|
decode(silk_path, pcm_path, 44100)
|
2023-12-13 21:23:43 +08:00
|
|
|
try:
|
|
|
|
# 调用系统上的 ffmpeg 可执行文件
|
|
|
|
# 获取 FFmpeg 可执行文件的路径
|
|
|
|
ffmpeg_path = get_ffmpeg_path()
|
|
|
|
# 调用 FFmpeg
|
2023-12-13 22:40:56 +08:00
|
|
|
cmd = f'''{ffmpeg_path} -loglevel quiet -y -f s16le -i {pcm_path} -ar 44100 -ac 1 {mp3_path}'''
|
|
|
|
system(cmd)
|
|
|
|
# 源码运行的时候下面的有效
|
|
|
|
# 这里不知道怎么捕捉异常
|
|
|
|
cmd = f'''{os.path.join(os.getcwd(), 'app', 'resources', 'ffmpeg.exe')} -loglevel quiet -y -f s16le -i {pcm_path} -ar 44100 -ac 1 {mp3_path}'''
|
2023-12-13 21:23:43 +08:00
|
|
|
system(cmd)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print(f"Error: {e}")
|
|
|
|
cmd = f'''{os.path.join(os.getcwd(),'app','resources','ffmpeg.exe')} -loglevel quiet -y -f s16le -i {pcm_path} -ar 44100 -ac 1 {mp3_path}'''
|
|
|
|
system(cmd)
|
2023-12-13 20:45:53 +08:00
|
|
|
system(f'del {silk_path}')
|
|
|
|
system(f'del {pcm_path}')
|
|
|
|
print(mp3_path)
|
2023-12-12 01:17:21 +08:00
|
|
|
return mp3_path
|
|
|
|
|
|
|
|
def get_audio_text(self, content):
|
|
|
|
try:
|
|
|
|
root = ET.fromstring(content)
|
|
|
|
transtext = root.find(".//voicetrans").get("transtext")
|
|
|
|
return transtext
|
2023-12-13 16:35:09 +08:00
|
|
|
except:
|
2023-12-12 01:17:21 +08:00
|
|
|
return ""
|
2023-12-16 20:06:43 +08:00
|
|
|
def close(self):
|
|
|
|
if self.open_flag:
|
|
|
|
try:
|
|
|
|
lock.acquire(True)
|
|
|
|
self.open_flag = False
|
|
|
|
self.DB.close()
|
|
|
|
finally:
|
|
|
|
lock.release()
|
2023-12-12 01:17:21 +08:00
|
|
|
|
2023-12-16 20:06:43 +08:00
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
2023-12-03 12:19:24 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-12-12 01:17:21 +08:00
|
|
|
db_path = './Msg/MediaMSG.db'
|
2023-12-03 12:19:24 +08:00
|
|
|
media_msg_db = MediaMsg()
|
2023-12-12 01:17:21 +08:00
|
|
|
reserved = '2865682741418252473'
|
|
|
|
path = media_msg_db.get_audio(reserved, r"D:\gou\message\WeChatMsg")
|
|
|
|
print(path)
|