update_video.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import requests
  2. import cv2
  3. import sys
  4. import pandas
  5. from model.sql_models import DB
  6. from config.using_config import quchen_text, dm
  7. def update_video_info():
  8. # 1.获取到所有的video_url
  9. sql = '''
  10. select * from video_info vi
  11. where length (preview_url )>0;
  12. '''
  13. df = pandas.read_sql(sql=sql, con=db_qc.engine)
  14. print(df)
  15. for index, row in df.iterrows():
  16. # 2.获取video信息----大小,时长,格式
  17. # video_url = 'http://wxsnsdy.wxs.qq.com/131/20210/snssvpdownload/SH/reserved/ads_svp_video__0b53qybncaaciaadpfl53jqbrbqe2gdafuka.f0.mp4?dis_k=4a9de877e9ee8dffe7a12a55700c7c0e&dis_t=1618994896&m=f4ed07a998cc60ba25ec1c2425176ea8'
  18. video_url = row['preview_url']
  19. rsp = requests.get(video_url)
  20. with open('aa.mp4', 'wb') as f:
  21. f.write(rsp.content)
  22. video_size = len(rsp.content)
  23. cap = cv2.VideoCapture('aa.mp4') # 视频流
  24. if cap.isOpened():
  25. rate = cap.get(5)
  26. frame_num = cap.get(7)
  27. print(frame_num, rate)
  28. duration = frame_num / rate
  29. print(video_size, duration)
  30. # byte_rate = video_size / 1024 * 8 / duration
  31. byte_rate = (video_size/(duration/8))
  32. print(byte_rate)
  33. # 3.进行存储
  34. sql = '''
  35. update video_info
  36. set size={},video_length={},type='mp4',byte_rate={}
  37. where video_id='{}'
  38. '''.format(video_size, duration, byte_rate, row['video_id'])
  39. db_qc.session.execute(sql)
  40. db_qc.session.commit()
  41. def update_byte_rate():
  42. sql = '''
  43. select * from video_info vi
  44. where length (preview_url )>0 and not type
  45. '''
  46. df = pandas.read_sql(sql=sql, con=db_qc.engine)
  47. print(df)
  48. for index, row in df.iterrows():
  49. # 2.获取video信息----大小,时长,格式
  50. # video_url = 'http://wxsnsdy.wxs.qq.com/131/20210/snssvpdownload/SH/reserved/ads_svp_video__0b53qybncaaciaadpfl53jqbrbqe2gdafuka.f0.mp4?dis_k=4a9de877e9ee8dffe7a12a55700c7c0e&dis_t=1618994896&m=f4ed07a998cc60ba25ec1c2425176ea8'
  51. video_url = row['preview_url']
  52. video_size = row['size']
  53. duration = row['video_length']
  54. # byte_rate = video_size / 1024 * 8 / duration
  55. byte_rate = (video_size / (duration / 8))
  56. print(byte_rate)
  57. # 3.进行存储
  58. sql = '''
  59. update video_info
  60. set size={},video_length={},type='mp4',byte_rate={}
  61. where video_id='{}'
  62. '''.format(video_size, duration, byte_rate, row['video_id'])
  63. db_qc.session.execute(sql)
  64. db_qc.session.commit()
  65. def update_video_info_pro():
  66. from model.sql_models import DB
  67. from config import using_config_test, using_config
  68. db_qc = DB(config=using_config.quchen_text)
  69. db_qc_test = DB(config=using_config_test.quchen_text)
  70. sql_get = '''
  71. select
  72. video_id,size, video_length, byte_rate, video_meta_data,download_path
  73. from video_info
  74. where LENGTH (preview_url )>1 and download_path is not null;
  75. '''
  76. cursor=db_qc_test.session.execute(sql_get)
  77. id_dict={}
  78. for line in cursor.fetchall():
  79. video_id,size, video_length, byte_rate, video_meta_data, download_path=line
  80. id_dict[video_id]=(size, video_length, byte_rate, video_meta_data,download_path)
  81. for k,v in id_dict.items():
  82. video_id=k
  83. size, video_length, byte_rate, video_meta_data,download_path=v
  84. sql_update = '''
  85. update video_info
  86. set size={}, video_length={},
  87. type='mp4', byte_rate={}, video_meta_data='{}',
  88. download_path='{}'
  89. where
  90. video_id='{}'
  91. '''.format(size, video_length, byte_rate, video_meta_data, download_path, video_id)
  92. db_qc.session.execute(sql_update)
  93. db_qc.session.commit()
  94. if __name__ == '__main__':
  95. db_qc = DB(config=quchen_text)
  96. db_dm = DB(config=dm)
  97. # update_video_info()
  98. # update_byte_rate()
  99. update_video_info_pro()