如何解决使用FFmpeg更改python中不同部分的视频速度,我使用opencv和子进程,但是它很慢,还有另一种更快的方法吗?
有一个视频文件,需要这样处理:[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线程,接收标准输入的输入数据,使用opencv读取帧,如果帧号合适,则将帧数据写入子进程PIPE。
代码如下:
inputCapture = cv2.VideoCapture(inputFile) # get the input Capture
inputFrameCount = int(inputCapture.get(cv2.CAP_PROP_FRAME_COUNT))
inputHeight = int(inputCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
inputWidth = int(inputCapture.get(cv2.CAP_PROP_FRAME_WIDTH))
inputFrameRate = int(inputCapture.get(cv2.CAP_PROP_FPS))
ffmpeg_command = '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"' % (inputWidth,inputHeight,inputFrameRate,ffmpegOutputOption,tempFolderPath)
FNULL = open(os.devnull,'w')
process = subprocess.Popen(ffmpeg_command,stdin=subprocess.PIPE,stdout=FNULL,stderr=FNULL) # Open a ffmpeg thread
inputFrameNumber = 0
inputEquivalentNumber = 0
outputEquivalentNumber = 0
startTime = time.time()
for chunk in chunks: # get the [0,2] from the chunks list
while inputFrameNumber < chunk[1]:
successGettingFrame,inputFrameData = inputCapture.read() # read one frame from inputCapture
inputFrameNumber += 1
inputEquivalentNumber += (1 / chunk[2])
print(successGettingFrame)
if not successGettingFrame:
break
while inputEquivalentNumber > outputEquivalentNumber: # check if the frame should be writen to ffmpeg-input
process.stdin.write(inputFrameData) # write the frame data to ffmpeg stdin
outputEquivalentNumber += 1
inputCapture.release()
cv2.destroyAllWindows()
process.stdin.close()
process.wait()
但是对于每个帧,它需要使用opencv读取帧数据,然后使用子进程将数据写入ffmpeg stdin,因此速度确实很慢,与ffmpeg libx264压缩相比大约是0.5倍。
因此,我希望可以有一种更快的方法来更改视频中指定部分的速度。
你们能提供一些帮助吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。