是否有任何工具(例如FFmpeg
或OpenCV
)可以根据某些特定的开始和结束帧索引将视频分成多个块?例如,给定(0,20),(21,33),(40,60)...
,每个元组表示一个块的起始和结束帧索引对,并且相邻块可能不相邻。
请注意,块的数量可能很大。例如,给定一个 1 小时的视频,可能有 1500 个分割。
答案1
根据“使用 OpenCV 创建视频”,我使用python(2.7)和OpenCV(3.0.0-beta)来分割它:(注意这种方法中音频是损失的)
import cv2
if __name__ == '__main__':
vidPath = '/path/foo/video.mp4'
shotsPath = '/path/foo/video/%d.avi' # output path (must be avi, otherwize choose other codecs)
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
for idx,(begFidx,endFidx) in enumerate(segRange):
writer = cv2.VideoWriter(shotsPath%idx,fourcc,fps,size)
cap.set(cv2.CAP_PROP_POS_FRAMES,begFidx)
ret = True # has frame returned
while(cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()