ffmpeg.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import subprocess
  2. class FFmpeg():
  3. """
  4. 获取单个视频时长
  5. """
  6. @classmethod
  7. def get_video_duration(cls, video_url):
  8. ffprobe_cmd = [
  9. "ffprobe",
  10. "-i", video_url,
  11. "-show_entries", "format=duration",
  12. "-v", "quiet",
  13. "-of", "csv=p=0"
  14. ]
  15. output = subprocess.check_output(ffprobe_cmd).decode("utf-8").strip()
  16. return float(output)
  17. """
  18. 视频更换分辨率
  19. """
  20. @classmethod
  21. def video_duration(cls, video_url, video_path, video_id):
  22. duration_url = video_path + str(video_id) + 'duration.mp4'
  23. # 获取视频时长
  24. total_duration = cls.get_video_duration(video_url)
  25. if int(total_duration) > 300:
  26. total_duration = 300
  27. ffmpeg_cmd = [
  28. "ffmpeg",
  29. "-i", video_url,
  30. "-c:v", "libx264",
  31. "-c:a", "aac",
  32. "-t", str(total_duration),
  33. '-vf', f"scale=720x1280",
  34. "-y",
  35. duration_url
  36. ]
  37. subprocess.run(ffmpeg_cmd)
  38. return duration_url