bitrate.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import csv
  2. import requests
  3. import subprocess
  4. import json
  5. import os
  6. def get_video_bitrate(url):
  7. """
  8. 获取视频的码率(比特率)
  9. """
  10. try:
  11. # 使用FFmpeg获取视频信息
  12. command = [
  13. 'ffprobe',
  14. '-v', 'quiet',
  15. '-select_streams', 'v:0',
  16. '-show_entries', 'stream=bit_rate',
  17. '-of', 'json',
  18. url
  19. ]
  20. result = subprocess.run(command, capture_output=True, text=True)
  21. video_info = json.loads(result.stdout)
  22. bitrate = int(video_info['streams'][0].get('bit_rate', 0))
  23. return bitrate
  24. except Exception as e:
  25. print(f"获取视频码率失败: {url}, 错误: {e}")
  26. return 0
  27. def get_file_size(url):
  28. """
  29. 获取文件大小
  30. """
  31. try:
  32. response = requests.head(url, allow_redirects=True)
  33. if response.status_code == 200:
  34. file_size = int(response.headers.get('Content-Length', 0))
  35. return file_size
  36. else:
  37. print(f"获取文件大小失败: {url}, 状态码: {response.status_code}")
  38. return 0
  39. except Exception as e:
  40. print(f"获取文件大小失败: {url}, 错误: {e}")
  41. return 0
  42. def process_csv(file_path):
  43. """
  44. 处理CSV文件
  45. """
  46. bitrate_counter = 0
  47. size_counter = 0
  48. count = 0
  49. with open(file_path, 'r') as csvfile:
  50. reader = csv.DictReader(csvfile)
  51. for row in reader:
  52. count += 1
  53. if count % 1000 == 0:
  54. print(f"count = :{count} bitrate_counter = {bitrate_counter} size_counter = {size_counter}")
  55. url1 = row['path_264']
  56. url2 = row['path_265']
  57. url1 = "http://visionularcdn.yishihui.com/" + url1
  58. url2 = "http://vcdn.yishihui.com/" + url2
  59. # 获取视频码率
  60. bitrate1 = get_video_bitrate(url1)
  61. bitrate2 = get_video_bitrate(url2)
  62. # 比较码率
  63. if bitrate1 > bitrate2:
  64. bitrate_counter += 1
  65. # 获取文件大小
  66. size1 = get_file_size(url1)
  67. size2 = get_file_size(url2)
  68. # 比较文件大小
  69. if size1 > size2:
  70. size_counter += 1
  71. return bitrate_counter, size_counter
  72. if __name__ == "__main__":
  73. csv_file_path = '/Users/heyu/Downloads/h265.csv' # 替换为你的CSV文件路径
  74. # csv_file_path = '/root/h265.csv' # 替换为你的CSV文件路径
  75. bitrate_count, size_count = process_csv(csv_file_path)
  76. print(f"URL1码率大于URL2的次数: {bitrate_count}")
  77. print(f"URL1文件大小大于URL2的次数: {size_count}")