• Python subprocess ffmpeg


    # -*- coding:utf-8 -*-
    import os, sys, getopt
    import numpy as np
    import subprocess as sp
    import cv2
    
    # command line parser
    '''
    try:
        opts, args = getopt.getopt(sys.argv[1:], "i:s:",["help"])
    except getopt.GetoptError:
        sys.exit()
    
    
    for op, value in opts:
        if op == "-i":
            input_file = value
        elif op== "-s":
            widthheight = value.split('*')
            width = np.int(widthheight[0])
            height = np.int(widthheight[1])
    
    '''
    input_file = 'rtsp://admin:hik12345@192.168.3.175/cam/realmonitor?channel=1&subtype=1'
    width = 704
    height = 576
    
    # videoIO
    FFMPEG_BIN = "E:/ffmpeg-20180227-fa0c9d6-win64-static/bin/ffmpeg.exe"
    command_in = [ FFMPEG_BIN,
                '-i', input_file,
                '-f', 'rawvideo',
                '-s', str(width) + '*' + str(height),
                '-pix_fmt', 'bgr24',
                '-']
    pipe_in = sp.Popen(command_in, stdout=sp.PIPE)
    
    command_out = ['E:/ffmpeg-20180227-fa0c9d6-win64-static/bin/ffmpeg.exe',
        '-y',
        '-f', 'rawvideo',
        '-vcodec', 'rawvideo',
        '-pix_fmt', 'bgr24',
        '-s', str(width) + '*' + str(height),
        # '-r', str(fps),
        '-i', '-',
        '-c:v', 'libx264',
        '-pix_fmt', 'yuv420p',
        # '-preset', 'ultrafast',
        # '-acodec', 'copy',
        # '-vcodec', 'copy',
        '-f', 'flv',
        'rtmp://192.168.3.56:1935/MyRed5/test']
    
    pipe_out = sp.Popen(command_out, stdin=sp.PIPE)  # ,shell=False
    
    
    # 这是处理每一帧图像的函数
    def process(img):
        cv2.imshow('image', img)
    
    
    # read width*height*3 bytes (= 1 frame)
    while True:
        raw_image = pipe_in.stdout.read(width * height * 3)
        image = np.fromstring(raw_image, dtype='uint8')
        if(len(image) == 0):
            break
        image = image.reshape((height, width, 3)).copy()
        process(image)
        # sys.stdout.write(image.tostring())
        # pipe_in.stdout.flush()
        pipe_out.stdin.write(image.tostring())  # 存入管道  
        pipe_out.stdin.flush()
        k = cv2.waitKey(25)  
        # q键退出
        if (k & 0xff == ord('q')):  
            break  
    
  • 相关阅读:
    java 语言里 遍历 collection 的方式
    struts2启动报错com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1
    mysql 查看表的类型
    memcached—向memcached中保存Java实体需注意的问题
    一个关于 UIPickerView 的 bug
    Wireshark数据抓包教程之安装Wireshark
    Mysql第四天 数据库设计
    产品经理怎样才干把一件事做出色
    Tokyo Tyrant(TTServer)系列(三)-Memcache协议
    Unity3D
  • 原文地址:https://www.cnblogs.com/gmhappy/p/11864026.html
Copyright © 2020-2023  润新知