demo.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/7/12
  4. import os
  5. import shutil
  6. import ffmpeg
  7. class Demo:
  8. # 获取已下载视频宽高、时长等信息
  9. @classmethod
  10. def get_video_info_from_local(cls, video_path):
  11. probe = ffmpeg.probe(video_path)
  12. # print('video_path: {}'.format(video_path))
  13. # format1 = probe['format']
  14. # bit_rate = int(format1['bit_rate']) / 1000
  15. # duration = format['duration']
  16. # size = int(format1['size']) / 1024 / 1024
  17. video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
  18. if video_stream is None:
  19. print('No video stream found!')
  20. return
  21. width = int(video_stream['width'])
  22. height = int(video_stream['height'])
  23. # num_frames = int(video_stream['nb_frames'])
  24. # fps = int(video_stream['r_frame_rate'].split('/')[0]) / int(video_stream['r_frame_rate'].split('/')[1])
  25. duration = float(video_stream['duration'])
  26. # print('width: {}'.format(width))
  27. # print('height: {}'.format(height))
  28. # print('num_frames: {}'.format(num_frames))
  29. # print('bit_rate: {}k'.format(bit_rate))
  30. # print('fps: {}'.format(fps))
  31. # print('size: {}MB'.format(size))
  32. # print('duration: {}'.format(duration))
  33. return width, height, duration
  34. # 判断视频时长是否大于 60s
  35. @classmethod
  36. def check_duration(cls):
  37. # 获取视频时长
  38. video_info = cls.get_video_info_from_local("../videos/11111/video.mp4")
  39. video_duration = video_info[2]
  40. if int(video_duration) < 60:
  41. shutil.rmtree("../videos/11111/")
  42. else:
  43. print(f"video_duration:{video_duration}")
  44. # 判断 title 长度
  45. @classmethod
  46. def check_title_len(cls):
  47. title = "🌸这首《老公赚钱给老婆花》🌸被唱的太肉麻了🌸好听好看🌸这首《老公赚钱给老婆花》🌸被唱的太肉麻了🌸好听好看"[:30]
  48. # print(len(title))
  49. # if len(title) > 30:
  50. # title = title[:30]
  51. print(title)
  52. print(len(title))
  53. # 删除某个文件夹下所有文件
  54. @classmethod
  55. def del_files(cls):
  56. filepath = "./videos/"
  57. del_list = os.listdir(filepath)
  58. print(f"del_list:{del_list}\n")
  59. for f in del_list:
  60. file_path = os.path.join(filepath, f)
  61. print(f"file_path:{file_path}")
  62. if os.path.isdir(file_path):
  63. shutil.rmtree(file_path)
  64. print(f"videos文件夹:{os.listdir(filepath)}\n")
  65. if __name__ == "__main__":
  66. demo = Demo()
  67. # demo.check_duration()
  68. # demo.check_title_len()
  69. demo.del_files()