ffmpeg.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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) > 290:
  26. total_duration = 290
  27. ffmpeg_cmd = [
  28. "ffmpeg",
  29. "-i", video_url,
  30. "-c:v", "libx264",
  31. "-b:v", "1000k",
  32. "-c:a", "aac",
  33. "-t", str(total_duration),
  34. '-vf', f"scale=720x1280",
  35. "-y",
  36. duration_url
  37. ]
  38. subprocess.run(ffmpeg_cmd)
  39. return duration_url