使用FFmpeg改变python中不同部分的视频速度,我使用opencv和subprocess,但它';It’太慢了,还有别的更快的办法吗?

使用FFmpeg改变python中不同部分的视频速度,我使用opencv和subprocess,但它';It’太慢了,还有别的更快的办法吗?,python,opencv,video,ffmpeg,subprocess,Python,Opencv,Video,Ffmpeg,Subprocess,有一个视频文件,需要这样处理:[0,13,2]表示第0帧到第13帧需要2倍的速度,根据下面的列表,更改不同视频部分的速度,并使用H264压缩将结果写入输出文件 [0, 13, 2] [13, 20, 1] [20, 25, 2] [25, 135, 1] [135, 164, 2] [164, 250, 1] [250, 254, 2] [254, 309, 1] [309, 354, 2] [354, 438, 1] [438, 465, 2] [465, 540, 1] [540, 625,

有一个视频文件,需要这样处理:[0,13,2]表示第0帧到第13帧需要2倍的速度,根据下面的列表,更改不同视频部分的速度,并使用H264压缩将结果写入输出文件

[0, 13, 2]
[13, 20, 1]
[20, 25, 2]
[25, 135, 1]
[135, 164, 2]
[164, 250, 1]
[250, 254, 2]
[254, 309, 1]
[309, 354, 2]
[354, 438, 1]
[438, 465, 2]
[465, 540, 1]
[540, 625, 2]
我希望整个过程能够由FFmpeg和python(我所知道的唯一语言)完成

现在我以这种方式实现:

Fitsr使用
subprocess.Popen(cmd,stdin=subprocess.PIPI)
创建FFmpeg线程,从stdin接收输入数据,使用opencv读取帧,如果帧编号合适,则将帧数据写入子进程管道

代码如下:

inputCapture=cv2.VideoCapture(inputFile)#获取输入捕获
inputFrameCount=int(inputCapture.get(cv2.CAP\u PROP\u FRAME\u COUNT))
inputhweight=int(inputCapture.get(cv2.CAP\u PROP\u FRAME\u HEIGHT))
inputWidth=int(inputCapture.get(cv2.CAP\u PROP\u FRAME\u WIDTH))
inputFrameRate=int(inputCapture.get(cv2.CAP\u PROP\u FPS))
ffmpeg_命令='ffmpeg-y-vsync 0-f rawvideo-pix_fmt bgr24-s%sx%s-i--an-r%s-vf“setpts=N/(%s*TB)”%s”%s/FinalVideo.mp4'%(输入宽度、输入八、输入帧速率、输入帧速率、ffmpegOutputOption、tempFolderPath)
FNULL=open(os.devnull,'w')
process=subprocess.Popen(ffmpeg_命令,stdin=subprocess.PIPE,stdout=FNULL,stderr=FNULL)#打开一个ffmpeg线程
inputFrameNumber=0
inputEquivalentNumber=0
outputEquivalentNumber=0
startTime=time.time()
对于区块中的区块:#从区块列表中获取[0,13,2]
当inputFrameNumberoutputEquivalentNumber:#检查帧是否应写入ffmpeg输入
process.stdin.write(inputFrameData)#将帧数据写入ffmpeg stdin
outputEquivalentNumber+=1
inputCapture.release()
cv2.destroyAllWindows()
process.stdin.close()
process.wait()
但是对于每一帧,它需要使用opencv读取帧数据,使用子进程将数据写入ffmpeg stdin,因此速度非常慢,大约是ffmpeg libx264压缩的0.5倍

[0, 13, 2]
[13, 20, 1]
[20, 25, 2]
[25, 135, 1]
[135, 164, 2]
[164, 250, 1]
[250, 254, 2]
[254, 309, 1]
[309, 354, 2]
[354, 438, 1]
[438, 465, 2]
[465, 540, 1]
[540, 625, 2]
所以我希望有一种更快的方法来改变视频中指定部分的速度

你们能帮忙吗