12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import subprocess
- class FFmpeg():
- """
- 获取单个视频时长
- """
- @classmethod
- def get_video_duration(cls, video_url):
- ffprobe_cmd = [
- "ffprobe",
- "-i", video_url,
- "-show_entries", "format=duration",
- "-v", "quiet",
- "-of", "csv=p=0"
- ]
- output = subprocess.check_output(ffprobe_cmd).decode("utf-8").strip()
- return float(output)
- """
- 视频更换分辨率
- """
- @classmethod
- def video_duration(cls, video_url, video_path, video_id):
- duration_url = video_path + str(video_id) + 'duration.mp4'
- # 获取视频时长
- total_duration = cls.get_video_duration(video_url)
- if int(total_duration) > 290:
- total_duration = 290
- ffmpeg_cmd = [
- "ffmpeg",
- "-i", video_url,
- "-c:v", "libx264",
- "-b:v", "1000k",
- "-c:a", "aac",
- "-t", str(total_duration),
- '-vf', f"scale=720x1280",
- "-y",
- duration_url
- ]
- subprocess.run(ffmpeg_cmd)
- return duration_url
|