Browse Source

Merge branch 'develop' of ssh://gitlab.fuxicarbon.com:1111/client_service/voice-gateway-service into develop

Davidliu 2 months ago
parent
commit
8f5c2b4286
1 changed files with 21 additions and 18 deletions
  1. 21 18
      src/core/callcenter/esl/handler/channel_hangup_handler.py

+ 21 - 18
src/core/callcenter/esl/handler/channel_hangup_handler.py

@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 # encoding:utf-8
-
+import subprocess
 import json
 import sys
 import os
@@ -185,26 +185,29 @@ class ChannelHangupHandler(EslEventHandler):
                 self.dataHandleServer.update_record(call_id, time_end=datetime.now(), sip_status=sip_status, sip_hangup_cause=hangup_cause,user_id=agent.user_id if agent else None, user_name=agent.agent_name if agent else None)
                 return
             merge_record = self.merge_audio_files(records) if len(records) > 1 else records[0]
-            # try:
-            #     self._ensure_path_permissions(merge_record)
-            #     os.chmod(merge_record, 0o755)  # 设置文件权限为 755
-            #     self.logger.info("成功设置文件权限: %s -> 755", merge_record)
-            # except Exception as chmod_error:
-            #     self.logger.error("设置文件权限失败: %s, error: %s", merge_record, str(chmod_error))
-
-            self.dataHandleServer.update_record(call_id, time_end=datetime.now(), url=merge_record, sip_status=sip_status, sip_hangup_cause=hangup_cause,user_id=agent.user_id if agent else None, user_name=agent.agent_name if agent else None)
-            self.logger.info("更新录音记录完成: call_id=%s", call_id)
+            # 计算录音时长
+            duration = self.get_audio_duration(merge_record)
+            self.dataHandleServer.update_record(call_id, times=int(duration), time_end=datetime.now(), url=merge_record, sip_status=sip_status, sip_hangup_cause=hangup_cause,user_id=agent.user_id if agent else None, user_name=agent.agent_name if agent else None)
+            self.logger.info("更新录音记录完成: call_id=%s, duration=%s", call_id, int(duration))
         except Exception as e:
             self.logger.error("更新录音记录失败: call_id=%s, error=%s", call_id, str(e))
 
-    def _ensure_path_permissions(self, file_path):
-        """确保文件及其父级目录的权限为 755"""
-        current_path = os.path.abspath(file_path)
-        self.logger.info("_ensure_path_permissions::%s", current_path)
-        # while current_path != "/":  # 遍历到根目录为止
-        #     if os.path.exists(current_path):
-        #         os.chmod(current_path, 0o755)  # 设置当前路径权限为 755
-        #     current_path = os.path.dirname(current_path)  # 获取父目录路径
+    def get_audio_duration(self, audio_path):
+        """使用 ffmpeg 计算音频时长"""
+        try:
+            result = subprocess.run(
+                ["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "json", audio_path],
+                stdout=subprocess.PIPE,
+                stderr=subprocess.PIPE,
+                text=True
+            )
+            info = json.loads(result.stdout)
+            duration = float(info["format"]["duration"])
+            return duration
+        except Exception as e:
+            print(f"获取音频时长失败: {e}")
+            return None
+
     def merge_audio_files(self,audio_files):
         if not audio_files:
             self.logger.info("没有可合并的音频文件")