Чтобы получить общее количество кадров в видео с помощью FFmpeg в Python, вы можете использовать различные методы. Вот несколько возможных подходов:
Метод 1: использование модуля подпроцесса:
import subprocess
def get_total_frames(video_path):
command = ['ffmpeg', '-i', video_path]
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
output = output.decode('utf-8')
for line in output.split('\n'):
if 'Duration' in line:
duration = line.split(',')[0].split('Duration: ')[1].strip()
duration = duration.split(':')
total_seconds = int(duration[0]) * 3600 + int(duration[1]) * 60 + float(duration[2])
if 'fps' in line:
fps = line.split('fps')[0].split(',')[4].strip()
total_frames = int(total_seconds * float(fps))
return total_frames
video_path = 'path/to/video.mp4'
frames_count = get_total_frames(video_path)
print(frames_count)
Метод 2. Использование исполняемого файла ffprobe:
import subprocess
import json
def get_total_frames(video_path):
command = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=nb_frames', '-of', 'default=nokey=1:noprint_wrappers=1', video_path]
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
total_frames = int(output.strip())
return total_frames
video_path = 'path/to/video.mp4'
frames_count = get_total_frames(video_path)
print(frames_count)
Метод 3. Использование библиотеки Moviepy:
from moviepy.editor import VideoFileClip
def get_total_frames(video_path):
clip = VideoFileClip(video_path)
total_frames = int(clip.fps * clip.duration)
return total_frames
video_path = 'path/to/video.mp4'
frames_count = get_total_frames(video_path)
print(frames_count)