首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用python从ffmpeg输出管道?

如何使用python从ffmpeg输出管道?
EN

Stack Overflow用户
提问于 2019-03-05 10:58:31
回答 4查看 9.7K关注 0票数 0

我正在尝试用FFmpegPython中传输输出。我正在读取视频抓取卡中的图像,并成功地使用dshow从命令行将其读取到输出文件。我正在尝试从卡抓取图像到我的OpenCv代码,以便能够进一步播放数据。不幸的是,当我输出图像时,我只得到了视频的显示,如链接中所示:

链接: s000.tinyupload.com/?file_id=15940665795196022618.

我使用的代码如下所示:

代码语言:javascript
运行
复制
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()

请给我一些指点来解决这个问题。我们非常感谢你的帮助。

EN

回答 4

Stack Overflow用户

发布于 2020-06-07 14:24:45

我在控制台应用程序FFmpeg上挣扎了很久,最后放弃了。使用这个扩展名会更容易:

代码语言:javascript
运行
复制
pip install ffmpeg-python

Kroening在这里发表了一个非常好的将FFmpeg集成到Python中的文章。有了这些例子,解决方案应该是可能的:https://github.com/kkroening/ffmpeg-python

票数 1
EN

Stack Overflow用户

发布于 2019-03-05 13:32:13

调用sp.Popen之后,您已经与it.You进行了通信,可以使用以下代码:

代码语言:javascript
运行
复制
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)

最后,您可以打印输出以确保上面的命令正常工作:

代码语言:javascript
运行
复制
print(ffmpeg_output)

上面的语句将显示与进程通信返回的输出。

票数 0
EN

Stack Overflow用户

发布于 2021-10-07 12:31:59

这个对我有用

代码语言:javascript
运行
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55001241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档