video_processing.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import logging
  2. import time
  3. import requests
  4. import hashlib
  5. import ffmpeg
  6. import os
  7. import oss2
  8. def change_format(video_url, file_md5=None):
  9. now_time = int(time.time() * 1000)
  10. input_file = '/tmp/{}.mp4'.format(now_time)
  11. output_file = '/tmp/{}.mp4'.format(now_time + 1)
  12. rsp = requests.get(video_url)
  13. with open(input_file, 'wb') as f:
  14. f.write(rsp.content)
  15. if not file_md5:
  16. file_md5 = hashlib.md5(rsp.content).hexdigest()
  17. logging.info(file_md5) # ac3ee699961c58ef80a78c2434efe0d0
  18. metadata_title = 'zx_' + file_md5
  19. ffmpeg.input(input_file).output(output_file, preset='slower', vcodec='h264',
  20. metadata='title=' + metadata_title).overwrite_output().run()
  21. video_info = ffmpeg.probe(output_file)['format']
  22. duration = video_info['duration']
  23. video_size = int(video_info['size'])
  24. bit_rate = (video_size / (float(duration) / 8))
  25. probe = ffmpeg.probe(input_file)
  26. format = probe['format']
  27. duration = format['duration']
  28. video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
  29. width = int(video_stream['width'])
  30. height = int(video_stream['height'])
  31. logging.info('视频时长:{} 视频大小:{} 视频比特率:{} 视频宽:{} 视频长:{}'.format(duration, video_size, bit_rate, width, height))
  32. cloud_filepath = 'video/{}.mp4'.format(metadata_title)
  33. video_format=video_url.split('.')[-1]
  34. format='mp4' if len(video_format)>4 else video_format
  35. # 上传视频
  36. update_to_aliyun(local_filepath=output_file, cloud_filepath=cloud_filepath)
  37. # 删除视频,上传视频
  38. os.remove(input_file)
  39. os.remove(output_file)
  40. return cloud_filepath, metadata_title, video_size, duration, bit_rate, width, height, format
  41. def update_to_aliyun(local_filepath, cloud_filepath):
  42. # TODO:之后oss相关密码信息写入config
  43. access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', 'LTAI5tGAUywhGk8xDuXosquq')
  44. access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', 'VMVfRv6DdSYuBipIMU5UdtHBYB0Jjb')
  45. bucket_name = os.getenv('OSS_TEST_BUCKET', 'zx-media-database')
  46. endpoint = os.getenv('OSS_TEST_ENDPOINT', 'http://oss-cn-hangzhou.aliyuncs.com')
  47. # 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
  48. bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
  49. # 查询文件是否已经在云上
  50. name_set = set()
  51. for i, object_info in enumerate(oss2.ObjectIterator(bucket)):
  52. name_set.add(object_info.key)
  53. if cloud_filepath not in name_set:
  54. logging.info('上传视频' + cloud_filepath)
  55. # 数据不存在于云上,则进行数据存储
  56. # 进行数据存储
  57. bucket.put_object_from_file(cloud_filepath, local_filepath)
  58. if __name__ == '__main__':
  59. import threading
  60. for i in range(100):
  61. x_thread=threading.Thread(target=change_format,args=('http://wxsnsdy.wxs.qq.com/131/20210/snssvpdownload/SH/reserved/ads_svp_video__0b53qybncaaciaadpfl53jqbrbqe2gdafuka.f110002.mp4?dis_k=2982f4fc3f191cb6199045ca072ed033&dis_t=1618995038&m=beb8a2fd5980bb6fb4f9cef486546338',))
  62. x_thread.start()
  63. change_format('http://wxsnsdy.wxs.qq.com/131/20210/snssvpdownload/SH/reserved/ads_svp_video__0b53qybncaaciaadpfl53jqbrbqe2gdafuka.f110002.mp4?dis_k=2982f4fc3f191cb6199045ca072ed033&dis_t=1618995038&m=beb8a2fd5980bb6fb4f9cef486546338')