python获取视频帧的时间_如何在Windows上从ffmpeg到python同时获取实时视频帧和时间戳...

搜索OpenCV的替代方案不会为我的计算机视觉算法中需要的实时摄像机流(在

Windows上)提供时间戳,我发现了ffmpeg和这篇优秀的文章

https://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/

该解决方案使用ffmpeg,访问其标准输出(stdout)流.我将其扩展为读取标准错误(stderr)流.

在Windows上处理python代码,同时我收到来自ffmpeg stdout的视频帧,但是stderr在为第一帧提供showinfo videofilter详细信息(时间戳)后冻结.

我记得在某个地方的ffmpeg论坛上看到重定向时会绕过像showinfo这样的视频过滤器.这是为什么以下代码无法按预期工作的原因?

预期:它应该将视频帧写入磁盘以及打印时间戳详细信息.

实际:它写入视频文件但没有获取时间戳(showinfo)详细信息.

这是我试过的代码:

import subprocess as sp

import numpy

import cv2

command = [ 'ffmpeg','-i','e:\sample.wmv','-pix_fmt','rgb24','-vcodec','rawvideo','-vf','showinfo',# video filter - showinfo will provide frame timestamps

'-an','-sn',#-an,-sn disables audio and sub-title processing respectively

'-f','image2pipe','-'] # we need to output to a pipe

pipe = sp.Popen(command,stdout = sp.PIPE,stderr = sp.PIPE) # TODO someone on ffmpeg forum said video filters (e.g. showinfo) are bypassed when stdout is redirected to pipes???

for i in range(10):

raw_image = pipe.stdout.read(1280*720*3)

img_info = pipe.stderr.read(244) # 244 characters is the current output of showinfo video filter

print "showinfo output",img_info

image1 = numpy.fromstring(raw_image,dtype='uint8')

image2 = image1.reshape((720,1280,3))

# write video frame to file just to verify

videoFrameName = 'Video_Frame{0}.png'.format(i)

cv2.imwrite(videoFrameName,image2)

# throw away the data in the pipe's buffer.

pipe.stdout.flush()

pipe.stderr.flush()

那么如何将帧时间戳从ffmpeg转换为python代码,以便可以在我的计算机视觉算法中使用…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取视频流每数据的时间戳,可以使用ffmpeg库和Python的subprocess模块来实现。首先,你需要使用ffmpeg命令行工具来解析视频文件并获取时间戳信息。下面是一个示例代码: ```python import subprocess as sp # 定义ffmpeg命令 command = \['ffmpeg', '-i', 'e:\sample.wmv', '-pix_fmt', 'rgb24', '-vcodec', 'rawvideo', '-vf', 'showinfo', '-f', 'null', '-'\] # 执行ffmpeg命令并将输出重定向到管道 pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.PIPE) # ffmpeg输出的信息 output, error = pipe.communicate() # 解析输出信息,提取每时间戳 timestamps = \[\] lines = output.decode().split('\n') for line in lines: if 'pts_time' in line: timestamp = line.split('pts_time:')\[1\].split()\[0\] timestamps.append(float(timestamp)) # 打印每时间戳 for timestamp in timestamps: print(timestamp) ``` 这段代码使用ffmpeg命令行工具解析视频文件,并通过管道将输出信息取到Python中。然后,它会解析输出信息,提取每时间戳,并将时间戳打印出来。请注意,这里假设视频速率是恒定的,如果速率不恒定,可能需要进行额外的处理。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [如何在Windows上从ffmpegpython同时获取实时视频时间戳](https://blog.csdn.net/weixin_39637397/article/details/110050253)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [获取视频中每个时间戳](https://blog.csdn.net/weixin_39843986/article/details/109970550)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值