supeng 1 month ago
parent
commit
a15cb7a55b
2 changed files with 81 additions and 2 deletions
  1. 3 2
      bitrate.py
  2. 78 0
      bitratev2.py

+ 3 - 2
bitrate.py

@@ -57,7 +57,7 @@ def process_csv(file_path):
         reader = csv.DictReader(csvfile)
         for row in reader:
             count += 1
-            if count % 10000 == 0:
+            if count % 1000 == 0:
                 print(f"count = :{count} bitrate_counter = {bitrate_counter} size_counter = {size_counter}")
 
             url1 = row['path_264']
@@ -86,7 +86,8 @@ def process_csv(file_path):
 
 
 if __name__ == "__main__":
-    csv_file_path = '/root/h265.csv'  # 替换为你的CSV文件路径
+    csv_file_path = '/Users/heyu/Downloads/h265.csv'  # 替换为你的CSV文件路径
+    # csv_file_path = '/root/h265.csv'  # 替换为你的CSV文件路径
     bitrate_count, size_count = process_csv(csv_file_path)
 
     print(f"URL1码率大于URL2的次数: {bitrate_count}")

+ 78 - 0
bitratev2.py

@@ -0,0 +1,78 @@
+import csv
+import requests
+import subprocess
+
+
+def get_filesize(url):
+    try:
+        response = requests.head(url, allow_redirects=True)
+        if response.status_code == 200:
+            content_length = response.headers.get('Content-Length')
+            return int(content_length) if content_length else None
+        print(f"Error: HTTP {response.status_code} for {url}")
+        return None
+    except Exception as e:
+        print(f"Error getting filesize for {url}: {e}")
+        return None
+
+
+def get_bitrate(url):
+    try:
+        cmd = [
+            'ffprobe',
+            '-v', 'error',
+            '-select_streams', 'v:0',
+            '-show_entries', 'stream=bit_rate',
+            '-of', 'default=noprint_wrappers=1:nokey=1',
+            url
+        ]
+        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=30)
+        bit_rate = output.decode().strip()
+        return int(bit_rate) if bit_rate else None
+    except subprocess.CalledProcessError as e:
+        print(f"ffprobe error for {url}: {e.output.decode().strip()}")
+        return None
+    except Exception as e:
+        print(f"Error getting bitrate for {url}: {e}")
+        return None
+
+
+def process_csv(csv_file):
+    bitrate_counter = 0
+    filesize_counter = 0
+
+    with open(csv_file, 'r') as file:
+        reader = csv.DictReader(file)
+        for row in reader:
+            url1, url2 = row['path_264'], row['path_265']
+
+            url1 = "http://visionularcdn.yishihui.com/" + url1
+            url2 = "http://vcdn.yishihui.com/" + url2
+
+            # 获取文件大小
+            size1, size2 = get_filesize(url1), get_filesize(url2)
+            if None in (size1, size2):
+                continue
+
+            # 获取码率
+            bitrate1, bitrate2 = get_bitrate(url1), get_bitrate(url2)
+            if None in (bitrate1, bitrate2):
+                continue
+
+            # 比较并计数
+            if bitrate1 > bitrate2:
+                bitrate_counter += 1
+            if size1 > size2:
+                filesize_counter += 1
+
+    print(f"码率大于次数: {bitrate_counter}")
+    print(f"文件大小大于次数: {filesize_counter}")
+
+
+if __name__ == "__main__":
+    import sys
+
+    if len(sys.argv) != 2:
+        print("使用方法: python script.py input.csv")
+        sys.exit(1)
+    process_csv(sys.argv[1])