简体   繁体   中英

ARGB images to gstreamer pipeline

In the attempt of speeding up the animation of a series of matplotlib produced images in a video, I want to convert the following (working) example to gstreamer , to be able to use the hardware encoding features of the Raspberry Pi 2 GPU.

The starting point is the code below, which uses stdin to stream the ARGB video frames to ffmpeg . How can I replace the command string to work with gst-launch-1.0 instead?

import numpy as np
import matplotlib.pylab as plt
import time
import subprocess

# Number of frames
nframes = 200

# Generate data
x = np.linspace(0, 100, num=nframes)
y = np.random.random_sample(np.size(x))

def testSubprocess(x, y):

    start_time = time.time()

    #set up the figure
    fig = plt.figure(figsize=(15, 9))
    canvas_width, canvas_height = fig.canvas.get_width_height()

    # First frame
    ax0 = plt.plot(x,y)
    pointplot, = plt.plot(x[0], y[0], 'or')

    def update(frame):
        # your matplotlib code goes here
        pointplot.set_data(x[frame],y[frame])

    # Open an ffmpeg process
    outf = 'testSubprocess.mp4'
    cmdstring = ('ffmpeg', 
                 '-y', '-r', '1', # overwrite, 1fps
                 '-s', '%dx%d' % (canvas_width, canvas_height), # size of image string
                 '-pix_fmt', 'argb', # format
                 '-f', 'rawvideo',  '-i', '-', # tell ffmpeg to expect raw video from the pipe
                 '-vcodec', 'mpeg4', outf) # output encoding
    p = subprocess.Popen(cmdstring, stdin=subprocess.PIPE)

    # Draw frames and write to the pipe
    for frame in range(nframes):
        # draw the frame
        update(frame)
        fig.canvas.draw()

        # extract the image as an ARGB string
        string = fig.canvas.tostring_argb()

        # write to pipe
        p.stdin.write(string)

    # Finish up
    p.communicate()

    print("Movie written in %s seconds" % (time.time()-start_time))

if __name__ == "__main__":
    testSubprocess(x, y)

The component to be used in the pipeline is omxh264enc , but that can also be achieved as a second step once I understand how to feed data to a pipeline. An answer based on gst-python is also completely acceptable.

"videoparse" looks like the key thing you want to use here, to tell GStreamer what format your input is.

gst-launch-1.0 fdsrc ! videoparse width=128 height=128 format=argb framerate=5/1 ! videorate ! videoconvert ! omxh264enc ! h264parse ! mp4mux ! filesink location=OUTPUTFILE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM