我正在尝试用FFmpeg在Python中传输输出。我正在读取视频抓取卡中的图像,并成功地使用dshow从命令行将其读取到输出文件。我正在尝试从卡抓取图像到我的OpenCv代码,以便能够进一步播放数据。不幸的是,当我输出图像时,我只得到了视频的显示,如链接中所示:
链接: s000.tinyupload.com/?file_id=15940665795196022618.
我使用的代码如下所示:
import cv2
import subprocess as sp
import numpy
import sys
import os
old_stdout=sys.stdout
log_file=open("message.log","w")
sys.stdout=log_file
FFMPEG_BIN = "C:/ffmpeg/bin/ffmpeg.exe"
command = [ FFMPEG_BIN, '-y',
'-f', 'dshow', '-rtbufsize', '100M',
'-i', 'video=Datapath VisionAV Video 01' ,
'-video_size', '640x480',
'-pix_fmt', 'bgr24', '-r','25',
'-f', 'image2pipe', '-' ]
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)
while True:
# Capture frame-by-frame
raw_image = pipe.stdout.read(640*480*3)
# transform the byte read into a numpy array
image = numpy.frombuffer(raw_image, dtype='uint8')
print(image)
image = image.reshape((480,640,3))
if image is not None:
cv2.imshow('Video', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
pipe.stdout.flush()
sys.stdout=old_stdout
log_file.close()
cv2.destroyAllWindows()
请给我一些指点来解决这个问题。我们非常感谢你的帮助。
发布于 2020-06-07 14:24:45
我在控制台应用程序FFmpeg上挣扎了很久,最后放弃了。使用这个扩展名会更容易:
pip install ffmpeg-python
Kroening在这里发表了一个非常好的将FFmpeg集成到Python中的文章。有了这些例子,解决方案应该是可能的:https://github.com/kkroening/ffmpeg-python
发布于 2019-03-05 13:32:13
调用sp.Popen
之后,您已经与it.You进行了通信,可以使用以下代码:
try:
pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT, universal_newlines=True)`
ffmpeg_output, _ = pipe.communicate()
except sp.CalledProcessError as err:
print("FFmpeg stdout output on error:\n" + err.output)
最后,您可以打印输出以确保上面的命令正常工作:
print(ffmpeg_output)
上面的语句将显示与进程通信返回的输出。
发布于 2021-10-07 12:31:59
这个对我有用
import subprocess as sp
import json
import os
import numpy
import PIL
from imutils.video import FPS
import cv2
def video_frames_ffmpeg():
width = 640
height = 360
iterator = 0
cmd = ['ffmpeg', '-loglevel', 'quiet',
'-f', 'dshow',
'-i', 'video=HD USB Camera',
#'-vf','scale=%d:%d,smartblur'%(width,height),
'-preset' ,'ultrafast', '-tune', 'zerolatency',
'-f', 'rawvideo',
'-pix_fmt','bgr24',
'-']
p = sp.Popen(cmd, stdout=sp.PIPE)
while True:
arr = numpy.frombuffer(p.stdout.read(width*height*3), dtype=numpy.uint8)
iterator += 1
if len(arr) == 0:
p.wait()
print("awaiting")
#return
if iterator >= 1000:
break
frame = arr.reshape((height, width,3))
cv2.putText(frame, "frame{}".format(iterator), (75, 70),
cv2.cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2)
im = Image.fromarray(frame)
im.save("ffmpeg_test/test%d.jpeg" % iterator)
yield arr
from PIL import Image
from imutils.video import FPS
for i, frame in enumerate(video_frames_ffmpeg()):
if i == 0:
fps = FPS().start()
else: fps.update()
fps.stop()
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cv2.destroyAllWindows()
https://stackoverflow.com/questions/55001241
复制相似问题