zhangyong 1 year ago
parent
commit
c87e9c63a9
1 changed files with 15 additions and 19 deletions
  1. 15 19
      video_agc/agc_video_method.py

+ 15 - 19
video_agc/agc_video_method.py

@@ -6,7 +6,7 @@ import subprocess
 import sys
 import time
 import urllib.parse
-import re
+import cv2
 
 import requests
 from datetime import datetime
@@ -151,29 +151,25 @@ class AgcVidoe():
     @classmethod
     def get_audio_duration(cls, video_url):
         try:
-            result = subprocess.run(['ffmpeg', '-i', video_url], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                                    universal_newlines=True)
-            output = result.stderr
-            match = re.search(r"Duration: (\d{2}):(\d{2}):(\d{2})\.\d+", output)
-            if match:
-                hours, minutes, seconds = map(int, match.groups())
-                total_seconds = hours * 3600 + minutes * 60 + seconds
-                return float(total_seconds)
-            else:
-                return None
+            cap = cv2.VideoCapture(video_url)
+            frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
+            fps = int(cap.get(cv2.CAP_PROP_FPS))
+            duration = frame_count / fps
+            cap.release()
+            return float(duration)
         except Exception as e:
             print("Error:", e)
             return None
 
 
-    # #  获取视频文件的时长(秒)
-    # @classmethod
-    # def get_video_duration(cls, video_file):
-    #     result = subprocess.run(
-    #         ["ffprobe", "-v", "error", "-show_entries", "format=duration",
-    #          "-of", "default=noprint_wrappers=1:nokey=1", video_file],
-    #         capture_output=True, text=True)
-    #     return float(result.stdout)
+    #  获取视频文件的时长(秒)
+    @classmethod
+    def get_video_duration(cls, video_file):
+        result = subprocess.run(
+            ["ffprobe", "-v", "error", "-show_entries", "format=duration",
+             "-of", "default=noprint_wrappers=1:nokey=1", video_file],
+            capture_output=True, text=True)
+        return float(result.stdout)
 
     @classmethod
     def clear_mp4_files(cls, folder_path):